My requirement is used to create a ftp batch script to transfer files from Unix to Windows through the WinSCP command line. So, I pass file name to the script and file is transferred from Unix to Windows. However, when I want to transfer multiple files, the challenge here is to take all the file names from the user and run the WinSCP command to get all the files. How to loop the input for the different file names and construct the WinSCP command for the same?
Can someone help me with the approach as I am new to batch scripting?
Sample command to transfer a single file
call C:\Progra~2\WinSCP\WinSCP.exe /console /timeout="120" /command "option batch continue" "option confirm off" "open sftp://%userid%:%passw%@%host%" "get %/file/filename.txt%" "exit"
Sample command to transfer a multiple files
call C:\Progra~2\WinSCP\WinSCP.exe /console /timeout="120" /command "option batch continue" "option confirm off" "open sftp://%userid%:%passw%@%host%" "get %/file/filename.txt%" "get %/file/filename2.txt%" "get %/file/filename3.txt%" "exit"
In this case you do not have to take multiple inputs at all. The WinSCP command get
can take multiple source files as arguments. You just have to use the target path as the last argument. Use the .\
to download to the current working directory (what is a default, when using a single argument only, just as in your example).
So you can prompt user only once to enter a space-separated list of files to download
set /p "list=Enter space separated list of files to download: "
winscp.com /command ^
"option batch continue" ^
"option confirm off" ^
"open sftp://%userid%:%passw%@%host%" ^
"get %list% .\" ^
"exit"
Side notes:
When running WinSCP from a batch file, use the winscp.com
instead of the winscp.exe
to avoid additional console window opening for WinSCP.
The command-line parameter /timeout=120
won't apply to sessions opened using the open
command in the script. Use the -timeout=120
switch of the the open
command:
open sftp://%userid%:%passw%@%host% -timeout=120
No point in using the call
command to run WinSCP