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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
aown61
Posts: 4
Joined: 02 Sep 2010 01:10

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

#1 Post by aown61 » 15 Jun 2011 03:08

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

aown61
Posts: 4
Joined: 02 Sep 2010 01:10

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

#2 Post by aown61 » 15 Jun 2011 05:02

Solved! Thanks anyway

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

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

#3 Post by orange_batch » 15 Jun 2011 05:50

I sent you mind bullets. You're welcome.

Easiest thread ever.

mid_life_crisis
Posts: 22
Joined: 26 May 2011 07:56

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

#4 Post by mid_life_crisis » 17 Jun 2011 14:15

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

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

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

#5 Post by Acy Forsythe » 17 Jun 2011 15:07

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


orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

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

#6 Post by orange_batch » 17 Jun 2011 17:52

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.

Post Reply