Differences

This shows you the differences between the selected revisions of the page.

2018-11-01 2019-01-15
wiki link (martin) Mixing WinSCP and cmd commands (martin)
Line 1: Line 1:
-====== Why are some scripting commands specified on WinSCP command-line in a batch file not executed/failing? ======+====== Why are some WinSCP scripting commands specified in a batch file not executed/failing? ====== 
 + 
 +===== [[cmd]] Mixing WinSCP and cmd commands ===== 
 + 
 +A common misconception is that in a batch file, you can paste commands as if you typed them in a console window. So after a line that executes [[executables|''winscp.com'']], you can add [[scripting#commands|WinSCP scripting commands]]. But that's not possible. The batch file can contain only commands that you can execute on Windows command prompt -- ''cmd.exe''. You cannot directly use sub-commands of those commands (like WinSCP scripting commands). 
 + 
 +For example this batch file: 
 + 
 +<code batch> 
 +echo Starting WinSCP 
 +winscp.com /log=winscp.log /ini=nul 
 +open sftp://username:password@example.com/ 
 +put d:\examplefile.txt  
 +exit 
 +echo WinSCP finished 
 +</code> 
 + 
 +It will execute ''winscp.com'', entering an interactive WinSCP scripting prompt. Now WinSCP will //wait// for you to type your commands (WinSCP is not aware of the batch file). On the other hand ''cmd.exe'' (which interprets the batch file), waits for ''winscp.com'' to close, before it even checks the following batch file lines. Only after you close ''winscp.exe'' (e.g. by typing WinSCP ''exit'' command), ''cmd.exe'' will read the line with the ''open'' command. And ''cmd.exe'' will fail executing it, as it does not know any ''open'' command. 
 + 
 +To execute WinSCP commands in a batch file, you need to use [[commandline#scripting|''/script'' or ''/command'' switches]]. 
 + 
 +  * With ''/script'' switch, you need to move your WinSCP commands into a separate text file, e.g. ''script.txt'': \\ <code winscp> 
 +open sftp://username:password@example.com/ 
 +put d:\examplefile.txt     
 +exit 
 +</code> And execute it from the batch file like: \\ <code batch> 
 +echo Starting WinSCP 
 +winscp.com /script=script.txt /log=winscp.log /ini=nul  
 +echo WinSCP finished 
 +</code> 
 + 
 +  * With ''/command'' switch, you can specify the commands directly on WinSCP command-line. Such command-line tends to be too long. Being in a batch file, you can use use [[wp>Batch_file#Escaped_characters_in_strings|new-line escaping]] using the caret symbol ''^'' to allow wrapping a long command line to multiple lines for a better readability: \\ <code batch> 
 +echo Starting WinSCP 
 +winscp.com /log=winscp.log /ini=nul /command ^ 
 +    "open sftp://username:password@example.com/" ^ 
 +    "put d:\examplefile.txt" ^ 
 +    "exit" 
 +echo WinSCP finished 
 +</code>
===== New-line Escaping ===== ===== New-line Escaping =====
-Many examples on this site, as well as [[ui_generateurl#script|batch file template]] generated by WinSCP, use [[https://en.wikipedia.org/wiki/Batch_file#Escaped_characters_in_strings|new-line escaping]] using caret sign ''^'' to allow wrapping a long command line to multiple lines for a better readability:+Many examples on this site (including the one above), as well as [[ui_generateurl#script|batch file template]] generated by WinSCP, use [[wp&gt;Batch_file#Escaped_characters_in_strings|new-line escaping]] using the caret symbol ''^'' to allow wrapping a long command line to multiple lines for a better readability:
<code batch> <code batch>
-winscp.com /command ^+winscp.com /log=winscp.log /ini=nul /command ^
    "open sftp://username:password@example.com/" ^     "open sftp://username:password@example.com/" ^
    "get *" ^     "get *" ^
Line 14: Line 52:
When reusing/modifying such script, users frequently find that WinSCP seemingly stops processing the commands in a middle or behaves strangely. When reusing/modifying such script, users frequently find that WinSCP seemingly stops processing the commands in a middle or behaves strangely.
-This is commonly caused by an improper syntax of the new-line escaping in the batch file (and as such is not WinSCP issue at all). For the new-line escaping to work as expected, the caret sign ''^'' has to be the very last character on the line (no spaces after it) and the next line needs to be indented (at least one space at the front).+This is commonly caused by an improper syntax of the new-line escaping in the batch file (and as such is not WinSCP issue at all). For the new-line escaping to work as expected, the caret symbol ''^'' has to be the very last character on the line (no spaces after it) and the next line needs to be indented (at least one space at the front).
===== Double Double-quotes ===== ===== Double Double-quotes =====

Last modified: by martin