- 1 year ago
WinSCP: SFTP script to transfer then move local files to another folder
To transfer files using WinSCP's SFTP protocol and then move the local files to another folder, you can create a script using WinSCP's scripting capabilities. Here's an example script that accomplishes this task:
batch@echo off
REM Set the paths and file names
set "winscpPath=C:\Path\to\winscp.exe"
set "sessionName=MySession"
set "sourcePath=C:\Path\to\local\files"
set "destinationPath=/remote/directory"
set "movePath=C:\Path\to\destination\folder"
REM Generate a temporary script file
set "scriptFile=%temp%\winscp_script.txt"
echo open %sessionName% > "%scriptFile%"
echo cd %destinationPath% >> "%scriptFile%"
echo lcd %movePath% >> "%scriptFile%"
echo put %sourcePath%\*.* >> "%scriptFile%"
echo mv %sourcePath%\*.* %movePath% >> "%scriptFile%"
echo exit >> "%scriptFile%"
REM Execute the script using WinSCP
"%winscpPath%" /script="%scriptFile%"
REM Delete the temporary script file
del "%scriptFile%"
Here's how the script works:
Set the necessary paths and file names at the beginning of the script. Update
winscpPath
with the path to your WinSCP executable,sessionName
with the name of your configured session in WinSCP,sourcePath
with the path to the local files you want to transfer, anddestinationPath
with the remote directory where you want to upload the files. UpdatemovePath
with the path to the local destination folder where you want to move the files after the transfer.The script generates a temporary script file (
winscp_script.txt
) with the necessary WinSCP commands. It opens the specified session, changes the remote directory to the destination path, uploads the files from the local source path to the remote directory, and then moves the local files to the specified move path.The WinSCP executable is invoked with the
/script
parameter, which specifies the temporary script file to execute.Finally, the temporary script file is deleted.
Make sure to replace the placeholder paths and session name with your actual values. Save the script with a .bat
or .cmd
extension and execute it to perform the SFTP transfer and local file move operation using WinSCP.