| 1. |
Solve : if statement in a for statement? |
|
Answer» Is is possible to put an if statement in a for statement? Here is the BATCH file... Is is possible to put an if statement in a for statement? Sure is! One problem I see is that the :20 construct either gets executed directly from a goto or ELSE the logic falls into the construct after wsftppro.exe executes. The other problem is that %%f variable is referenced outside the for loop. Code: [Select]for %%f in (%file1% %file2%) do if not exist %source%\%%f goto 20 "C:\Program Files\Ipswitch\WS_FTP Professional\wsftppro.exe" -s local:%source%\%%f -d interfaces!kidneykonditioning:%dest% -quietd If both the if statement and the wsftppro.exe program are part of the for loop, you'll need to use parenthesis: Code: [Select]for %%f in (%file1% %file2%) do ( if not exist %source%\%%f goto 20 "C:\Program Files\Ipswitch\WS_FTP Professional\wsftppro.exe" -s local:%source%\%%f -d interfaces!kidneykonditioning:%dest% -quietd ) You have access to goto :eof which I think should be the last statement in the phase1 construct. Of course you may want to issue a message that all is well. I've modified it as per your suggestion but doesn't seem to get past the first file. What am I missing??? Thanks!!! @echo on SET file1=test1.txt SET file2=test2.txt SET source=U:\gendl SET dest=/home/asolutions/ SET logfile="U:\BatchFiles\IPSwitch Log Files\test.log" :phase1 cd /d %source% for %%f in (%file1% %file2%) do ( if not exist %source%\%%f goto 20 "C:\Program Files\Ipswitch\WS_FTP Professional\wsftppro.exe" -s local:%source%\%%f -d interfaces!kidneykonditioning:%dest% -quiet ) goto end :20 @ECHO ON DATE /t >> %logfile% TIME /t >> %logfile% echo "No file to send!!!" >> %logfile% ECHO =================================================== >> %logfile% :end Quote I've modified it as per your suggestion but doesn't seem to get past the first file. What am I missing??? Probably nothing. Did you try to debug the code? I suspect this line is the problem: Code: [Select]if not exist %source%\%%f goto 20 If expanded, it should look like this: if not exist U:\gendl\test1.txt Does the file exist? If not, the logic goes to :20, never to return to check for file2.txt Consider rewriting the for loop to use an either/or approach. if not exist filename (call :20) else (execute wsftppro.exe) I'll leave it to you to fill in the paths and parameters. That's correct, if expanded that's how it should look. How do I make it return to check for additional files (i.e. file2, file3, etc.) logging each entry for "No file to send" if file does not exist. If files do exist to run the wsftp program? Would if be better to just have multiple if STATEMENTS & not use the for loop? Ex. if exist %file1% do ("C:\Program Files\Ipswitch\WS_FTP Professional\wsftppro.exe" -s local:%source%\%%f -d interfaces!kidneykonditioning:%dest% -quiet) else ( @ECHO ON DATE /t >> %logfile% TIME /t >> %logfile% echo "No %file1% to send!!!" >> %logfile% ECHO =================================================== >> %logfile% ) if exist %file2% do ("C:\Program Files\Ipswitch\WS_FTP Professional\wsftppro.exe" -s local:%source%\%%f -d interfaces!kidneykonditioning:%dest% -quiet) else ( @ECHO ON DATE /t >> %logfile% TIME /t >> %logfile% echo "No %file2% to send!!!" >> %logfile% ECHO =================================================== >> %logfile% ) if exist %file3% do ("C:\Program Files\Ipswitch\WS_FTP Professional\wsftppro.exe" -s local:%source%\%%f -d interfaces!kidneykonditioning:%dest% -quiet) else ( @ECHO ON DATE /t >> %logfile% TIME /t >> %logfile% echo "No %file3% to send!!!" >> %logfile% ECHO =================================================== >> %logfile% ) Quote Would if be better to just have multiple if statements & not use the for loop? You decide. I think this snippet is what you're trying to do: Code: [Select]@echo on SET file1=test1.txt SET file2=test2.txt SET source=U:\gendl SET dest=/home/asolutions/ SET logfile="U:\BatchFiles\IPSwitch Log Files\test.log" :phase1 cd /d %source% for %%f in (%file1% %file2%) do ( if not exist %source%\%%f (call :20) else ( "C:\Program Files\Ipswitch\WS_FTP Professional\wsftppro.exe" -s local:%source%\%%f -d interfaces!kidneykonditioning:%dest% -quiet) ) goto :eof :20 @ECHO ON DATE /t >> %logfile% TIME /t >> %logfile% echo "No %%f to send!!!" >> %logfile% ECHO =================================================== >> %logfile% Quote How do I make it return to check for additional files Use the call statement instead of goto. Try directly NAMING the files in the for statement. This will reduce the number of changes for maintenance: Not this: Code: [Select]SET file1=test1.txt SET file2=test2.txt for %%f in (%file1% %file2%) do ( Try this instead: Code: [Select]for %%f in (test1.txt test2.txt) do ( You can easily add file names in the for list without having to define new variables. Variables are useful, but unless the values are generated at run time, there is little reason not to hardcode them. Good luck. Thank you for all your guidance. I finally got it to work. |
|