I have a script that is executing perfectly on command prompt.
If I copy and paste the script directly to the command window then it is working fine, but when save it like Windows batch file and run it it is hanging.
option batch abort
cd \program files\winscp\
winscp.com /command "open HennTest:Nate82@data.test.com" /privatekey=C:\HennTest\VCS\HennTest_key_putty.ppk
option confirm off
get /*.csv C:\HennTest\VCS\
close
exit
It is connecting to the server but not executing the get
statement when using like batch but it is working correctly when I copy/paste it directly.
I need to use it like batch since I want to automate the script to download the files.
Your are combining WinSCP commands and Windows commands into one file. That cannot work. The batch file stops on a call to the winscp.com
and waits for it to finish. WinSCP on the contrary does not know that the batch file even exist, so it cannot read its commands from there.
See WinSCP FAQ Why are some WinSCP scripting commands specified in a batch file not executed/failing?
This works:
cd \program files\winscp\
winscp.com /command ^
"open sftp://HennTest:Nate82@data.test.com -privatekey=C:\HennTest\VCS\HennTest_key_putty.ppk" ^
"get /*.csv C:\HennTest\VCS\" ^
"close" ^
"exit"
It makes use of the WinSCP command-line switch /command
that you can use to specify WinSCP commands on its command-line.
Note that I've corrected few other mistakes:
- The
option batch abort
is WinSCP command, not Windows command, so you cannot execute it before WinSCP. Anyway, the latest versions of WinSCP default to the batch abort
, when running commands specified in script or command-line. So you do not need the command at all.
- The command-line switch
/privatekey
should not be combined with scripting. Use the -privatekey
switch of the open
command.
- The
option confirm off
is also default in the recent version of WinSCP.
- While WinSCP defaults to the SFTP protocol, it's advisable to explicitly mention the
sftp://
prefix in the session URL anyway.
Think of the command prompt as a sequence of inputs to prompts, while a batch script is a sequence of commands to execute. The batch script does not know that the get, close, and exit statements are actually inputs to the winscp prompts but is waiting till winscp finishes to run them individually.
You can try piping the input to winscp in a batch file like so:
cd \program files\winscp\
(
echo.option batch abort
echo.option confirm off
echo.get /*.csv C:\HennTest\VCS\
echo.close
echo.exit
) | winscp.com /command "open HennTest:Nate82@data.test.com" /privatekey=C:\HennTest\VCS\HennTest_key_putty.ppk
Note: This piping method may not work depending on how the program implements the input buffer.