1

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"

0

2 Answers 2

3

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

0
1

I am assuming you are going to prompt the user input manually in the console. This will prompt for the user input repeatedly until it sees a dot .

@echo off       
setlocal enabledelayedexpansion     
:prompt     
set /p "input=Enter the filename:"      
if "!input!"=="." (     
    goto :begin ) else (    
        set input_all=!input_old! "!input!" 
        set input_old=!input_all!
        goto :prompt
    )   
:begin      
echo consolidated_input=!input_all!     

Add your winscp command line after the label :begin

tested sample-

D:\Scripts>draft.bat
Enter the filename:c:\sample\test1.txt
Enter the filename:c:\sample\test2.log
Enter the filename:c:\sample\test3.ini
Enter the filename:.
consolidated_input= "c:\sample\test1.txt"  "c:\sample\test2.log"  "c:\sample\test3.ini"

Cheers, G

4
  • Thanks gbabu. This works for me. But I am facing one issue. At the run time, the winscp command does not take the consolidated input value. It runs as -- call C:\Progra~2\WinSCP\WinSCP.exe /console /timeout="120" /command "option batch continue" "option confirm off" "open sftp://%userid%:%passw%@%host%" "" Aug 24, 2014 at 7:50
  • Probably because winscp requires the input in the form of get %/file/filename.txt%. Above script does not add the word get in the input. If you want to add get as you said in the example, replace the line get input_all=!input_old! "!input!" with get input_all=!input_old! "get !input!" and try.
    – gbabu
    Aug 24, 2014 at 7:56
  • I already tried with that option only.In the begin label: set /p consolidated_input=!input_all! @echo ON echo %consolidated_input% cd C:\Users\ebavkar\Documents\Projects\VMware\Scripts call C:\Progra~2\WinSCP\WinSCP.exe /console /timeout="120" /command "option batch continue" "option confirm off" "open sftp://%userin%:%pasw%@%host%" "%consolidated_input%" Aug 24, 2014 at 7:59
  • Your example does not show how you add the get part. Assuming you have that right... You also need to remove the double-qoutes that you have around the %consolidated_input%. And you need to make sure you add double-quotes around every get filename pair in your loop. So that the resulting syntax is as you have it in your question, i.e. "get /xxx/filename1" "get /xxx/filename2". Aug 24, 2014 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.