Page 1 of 1

Using If ... Else within a Batch file - FTP Script

Posted: 15 Jun 2011 03:08
by aown61
Hi all,
I've got a batch which creates a script to run against FTP, which counts the number of .csv files in a given directory. If the count is correct the batch will create a file (.done)
What i would like to do is add an ELSE statement after this. So if :count: < 10 (for example) DO :this command:

Code: Select all

>  c:\test2\script2.txt echo open ******
>> c:\test2\script2.txt echo *****
>> c:\test2\script2.txt echo *****
>> c:\test2\script2.txt echo cd ***
>> c:\test2\script2.txt echo cd ***
>> c:\test2\script2.txt echo cd ***
>> c:\test2\script2.txt echo SETLOCAL
>> c:\test2\script2.txt echo SETLOCAL ENABLEDELAYEDEXPANSION
>> c:\test2\script2.txt echo SET count=0
>> c:\test2\script2.txt echo for %%o IN ("\\*****\****\****\****\*.csv") DO (
>> c:\test2\script2.txt echo %%o
>> c:\test2\script2.txt echo SET /A count=count + 1
>> c:\test2\script2.txt   echo )
>> c:\test2\script2.txt echo echo %count%
>> c:\test2\script2.txt echo IF "%count%"=="10" ECHO Done >> "\\******\*****\***\****\****.done"    I WOULD LIKE TO ADD 'ELSE' HERE
>> c:\test2\script2.txt echo ENDLOCAL ENABLEDELAYEDEXPANSION
>> c:\test2\script2.txt echo ENDLOCAL
>> c:\test2\script2.txt echo bye
start /w ftp.exe -s:c:\test2\script2.txt
del c:\test2\script2.txt



As i've no experience of using the ELSE command in DOS, could anyone give me some pointers?

Many Thanks,

Andy

Re: Using If ... Else within a Batch file - FTP Script

Posted: 15 Jun 2011 05:02
by aown61
Solved! Thanks anyway

Re: Using If ... Else within a Batch file - FTP Script

Posted: 15 Jun 2011 05:50
by orange_batch
I sent you mind bullets. You're welcome.

Easiest thread ever.

Re: Using If ... Else within a Batch file - FTP Script

Posted: 17 Jun 2011 14:15
by mid_life_crisis
I find that If Else in batch files is a pain in the neck. Half the time I cannot get it to work.
I usually end up doing

If test command
If Not sametest newcommand

Re: Using If ... Else within a Batch file - FTP Script

Posted: 17 Jun 2011 15:07
by Acy Forsythe
For using ELSE and if tests, you have to have the ELSE on the same line as the end ) below:

Code: Select all

IF Test1 (some commands to do
              some more commands to do
) ELSE (commands to do instead
             more commands to do instead
)

OR

IF Test1 (commands to do
              more commands
) ELSE IF Test2 (Commands to do now
                        More commands to do now
) ELSE (command to do now)



The () are treated as a single line, but you can't do this:

Code: Select all

IF test1 (Commands
             More commands)
ELSE this command instead


Re: Using If ... Else within a Batch file - FTP Script

Posted: 17 Jun 2011 17:52
by orange_batch
Yes you should practice that mid_life_crisis, not only does not using else cause bad coding performance but you could run into some unexpected errors. 8) Mainly, the else will fire for all conditions not met by the if. Sometimes things aren't just black and white.