Nancy K
Nancy K

Reputation: 33

Batch file IF statement syntax in WinSCP commands

I am trying to update a batch file that uses WinSCP to download files. But I am unfamiliar with the syntax. In the following code, the IF statement is not recognized.
First question, what is the correct syntax of the IF statement? I tried several combinations of arrangements of the quote characters, but have removed them for this example. Second question, what is the purpose of the ^ at the end of some of the lines? Third question, what is the purpose of the indent in the lines following the first line?

F:\Tools\WinSCP\WinSCP /command="option batch on" ^ "option confirm off" ^ "open WinSCP-ProfileName" ^ 
 "lcd F:\Data\TargetDirectory" ^
 "get ""*_SourceFileName.txt"" %TargetFileName%" ^
 IF %computername%==xxx GOTO LabelElse ^
 "echo do something here" ^
 :LabelElse ^
 "exit" /log=F:\Logs\LogFileName.log

Thanks!

Upvotes: 1

Views: 375

Answers (3)

Martin Prikryl
Martin Prikryl

Reputation: 202292

Your whole "batch file" is actually a single WinSCP command-line. It just broken into multiple physical lines using the ^ and the indentation.

You cannot use batch commands (like the if) within other command (the WinSCP command-line in this case).

If you need to conditionally modify the commands passed to WinSCP, you either:

  • Have to assemble the command-line to a variable upfront, based on your criteria, and then pass it at once to WinSCP.

  • Or, more straightforward is to assemble a WinSCP script file instead.

    set SCRIPT=%TEMP%\winscp.txt
    
    (
       echo option batch on
       echo option confirm off
       echo open WinSCP-ProfileName
       echo lcd F:\Data\TargetDirectory
       echo get "*_SourceFileName.txt" %TargetFileName%
       IF not %computername%==xxx (
         echo some computer-specific commands here
       )
       echo exit
    ) > %SCRIPT%
    
    F:\Tools\WinSCP\WinSCP /script=%SCRIPT%  /log=F:\Logs\LogFileName.log
    

Upvotes: 2

Nancy K
Nancy K

Reputation: 33

Thanks to the suggestions here, I got it to work! What I was really trying to do, was to not delete the file from the sFTP site when executing in Dev or UAT. Works great!

IF %computername%==DevServer GOTO LabelElse
IF %computername%==UatServer GOTO LabelElse
  set "DisplayText=WinSCP command - INCLUDES DELETE from sFTP site - for PRODUCTION"
  set "DeleteOption=-delete"
  GOTO LabelEndif
:LabelElse
  set "DisplayText=WinSCP command - DOES NOT DELETE from sFTP site - for *NON-PROD*"
  set "DeleteOption="
:LabelEndif

rem other stuff here

ECHO %DisplayText%
F:\Tools\WinSCP\WinSCP /command="option batch on" ^ "option confirm off" ^ "open WinSCP-ProfileName" ^ "lcd F:\Data\TargetDirectory" ^
"get %DeleteOption% ""*_SourceFileName.txt"" %TargetFileName%" ^
"exit" /log=F:\Logs\LogFileName.log

Upvotes: 1

SomethingDark
SomethingDark

Reputation: 14305

This appears to be a template that you'd adjust to suit your needs.

The syntax of the if statement is arguably correct, although best practices would have it look like if "%computername%"=="xxx" goto LabelElse in case %computername% had a space or somehow didn't have a value. The xxx bit would be the name of a computer that you don't want to run the echo do something here bit on, like some post-processing or something.

The ^ is the escape character in batch, and at the end of a line, it acts as a line continuation character. I suspect that "option confirm off" ^ and "open WinSCP-ProfileName" ^ are also meant to be on their own lines. The fact that there's a label as part of the line continuation (:LabelElse) is surprising to me, as labels have to be on their own line in order to work. The fact that this is a command being passed to WinSCP may make my concerns irrelevant, but the fact that you're getting an error suggests that this entire thing is coded incorrectly. I'd usually write a separate script for the WinSCP bit and pass that as an argument instead.

The indentations are related to the fact that ^ is a line continuation character and is meant to demonstrate that all of the below lines are related to the first line.

Upvotes: 1

Related Questions