handling batch flow

August 30, 2011 at 07:13:12
Specs: Windows Server 08 R2
I have a batch file that uses "if exist" and goto
@Echo off

if exist "E:\Datatel\Files\test.txt" goto AAA

:AAA
Echo I am down in the label FIRST.
pause

if exist "E:\Datatel\Files\test2.txt" goto BBB

:BBB
Echo I am down in the label SECOND.
pause

:END

However, it executes the goto BBB even though test.2.txt file does not exist.

Suggestions?


See More: handling batch flow

Report •

#1
August 30, 2011 at 16:51:56
The problem is this:

if exist "E:\Datatel\Files\test2.txt" goto BBB

Since the file test2.txt doesn't exist, the batch file then skips a line and goes down to the BBB.

:BBB
Echo I am down in the label SECOND.

Try something like this:

:AAA
Echo I am down in the label FIRST.
pause

if exist "E:\Datatel\Files\test2.txt" goto BBB
goto ERROR

:BBB
Echo I am down in the label SECOND.
pause

:END

:ERROR
CLS
ECHO The program could not find test2.txt.
PAUSE
GOTO END

Mouse could not be found. Click OK to continue.
What to do now?


Report •

#2
August 31, 2011 at 06:34:31
Good Morning RMT2,

I appreciate your answer. My other issue is that the batch info above is just a test batch file...I have 17 files that I need to check if exist so in the :ERROR label...I can't GOTO END...i would like to go check if the next file exist.

Thoughts on this?


Report •

#3
August 31, 2011 at 15:40:39
Do the file go in numerical order like test1.txt, test2.txt, test3.txt, ...?

Mouse could not be found. Click OK to continue.
What to do now?


Report •

Related Solutions

#4
September 1, 2011 at 06:26:17
No, unfortunatley not. Goes like this...

if exist "E:\Datatel\Files\Daily_Processes\STA_Add_File.txt" goto ADDSTA
if exist "E:\Datatel\Files\Daily_Processes\ITI_Add_File.txt" goto ADDITI
if exist "E:\Datatel\Files\Daily_Processes\RCA_Add_File.txt" goto ADDRCA
if exist "E:\Datatel\Files\Daily_Processes\RCF_Add_File.txt" goto ADDRCF
if exist "E:\Datatel\Files\Daily_Processes\RCM_Add_File.txt" goto ADDRCM


Report •

#5
September 1, 2011 at 16:10:56
Sadly, I think you will just have to repeat the code for each file if there are 17 files. It is possible to integrate the results together to see a total, but that will add another 2 lines of code or so to each file being checked for existence. You can completely remap your software like this though so that it uses variables:

@ECHO OFF
ECHO Ready to check files.
PAUSE
GOTO CHECKER
:CHECKER
IF EXIST "E:\Datatel\Files\Daily_Processes\STA_Add_File.txt" SET ADDSTA=1
IF EXIST "E:\Datatel\Files\Daily_Processes\ITI_Add_File.txt" SET ADDITI=1
IF EXIST "E:\Datatel\Files\Daily_Processes\RCA_Add_File.txt" SET ADDRCA=1
IF EXIST "E:\Datatel\Files\Daily_Processes\RCF_Add_File.txt" SET ADDRCF=1
IF EXIST "E:\Datatel\Files\Daily_Processes\RCM_Add_File.txt" SET ADDRCM=1
GOTO RESULTS
:RESULTS
IF '%ADDSTA%'=='1' ECHO File "STA_Add_File.txt" was found.
IF '%ADDITI%'=='1' ECHO File "ITI_Add_File.txt" was found.
IF '%ADDRCA%'=='1' ECHO File "RCA_Add_File.txt" was found.
IF '%ADDRCF%'=='1' ECHO File "RCF_Add_File.txt" was found.
IF '%ADDRCM%'=='1' ECHO File "RCM_Add_File.txt" was found.
IF '%ADDSTA%'=='' ECHO File "STA_Add_File.txt" was not found.
IF '%ADDITI%'=='' ECHO File "ITI_Add_File.txt" was not found.
IF '%ADDRCA%'=='' ECHO File "RCA_Add_File.txt" was not found.
IF '%ADDRCF%'=='' ECHO File "RCF_Add_File.txt" was not found.
IF '%ADDRCM%'=='' ECHO File "RCM_Add_File.txt" was not found.
PAUSE
GOTO EXIT
:EXIT
EXIT

If you give me all 17 filenames, and paths, I can put together the rest of the program so that it checks all of the files. Try out the code above and see how it works.

Mouse could not be found. Click OK to continue.
What to do now?


Report •

#6
September 2, 2011 at 05:27:07
Good Morning...I appreciate that you keep making suggestions and helping me with this crazy batch file. I can make the above code you posted work...I have one more twist to add...

After I check the variable...I need to run 2 executable and move a file. Something like this...

GOTO RESULTS
:RESULTS

IF '%ADDSTA%'=='1'
some.exe
someother.exe
move c:\ITI_Chg_File.txt c:\temp\ITI_Chg_BAK.txt

How do I get it to execute only the 3 lines if the variable is 1?

Thanks again for the help.


Report •

#7
September 2, 2011 at 15:48:29
You're welcome :). With an IF statement, you can use something like this:

IF '%ADDSTA%'=='1' (
START (Path to program needed here also)some.exe
START (Path to program needed here also)someother.exe
move c:\ITI_Chg_File.txt c:\temp\ITI_Chg_BAK.txt
)

The important part is the parenthesis. Any lines of code after the ( will only be run if the IF statement matches the requirements. Then just put the ) under the last line of code you want it to execute.

Mouse could not be found. Click OK to continue.
What to do now?


Report •

Ask Question