Batch script not running as expected

Microsoft XP
July 15, 2010 at 06:40:34
Specs: Windows XP, 2.99 Ghz, 3.21 Ram
Below is a script I am trying to run that reads in a file with a list of file names and completes a specific action depending on what kind of file it is reading in. The problem I am running into is that regardless of the prefix associated with the file the goto tries to complete all actions on the file. I have the ouput if that helps.

@echo on

::s,object1.sql
::c,copy1.txt
::c,copy2.txt

set deploydir=%1
set host=%2
set H1=%host%
set F1=%deploydir%

::CODE
for /f "tokens=1-2 delims=," %%a in (listfile.txt) do (
if %%a equ s goto SQL ::echo SSS %%a ::
if %%a equ c goto COPY
)

:SQL
for /f "tokens=2 delims=," %%b in (listfile.txt) do (
sqlcmd -S %H1% -i %%b
)

:COPY
for /f "tokens=2 delims=," %%b in (listfile.txt) do (
copy %%b %F1%\%%b /y
)

dir %F1%


See More: Batch script not running as expected

Report •

#1
July 15, 2010 at 06:51:34
I also tried this and it is grabbing all 3 files and completing the task. I am only expecting the statement to complete the task on object1.sql but it is trying to run sqlcmd on the other two files.

@echo on

::s,object1.sql
::c,copy1.txt
::c,copy2.txt

set deploydir=%1
set host=%2
set H1=%host%
set F1=%deploydir%

::CODE
for /f "tokens=1-2 delims=," %%a in (listfile.txt) do (

if %%a equ s (
for /f "tokens=2 delims=," %%b in (listfile.txt) do (
sqlcmd -S %H1% -i %%b
)))

dir %F1%


Report •

#2
July 15, 2010 at 08:14:07
So this is what I ended up doing. I was over thinking it a little.

@echo on

set deploydir=%1
set host=%2
set H1=%host%
set F1=%deploydir%

::CODE
for /f "tokens=1-2 delims=," %%a in (listfile.txt) do (
if %%a equ s sqlcmd -S %H1% -i %%b
if %%a equ c copy %%b %F1%\%%b /y
)
dir %F1%

I still have one question: Is there anyway to set this batch script up to exit if one of my commands do not work? For instance, and sqlcmd errors out because there is systax errors in the .sql file and I want to bail out of the batch script on that failure instead of running the next .sql file in the list.


Report •

#3
July 15, 2010 at 09:45:23
If ERRORLEVEL=1 goto :EOF

@echo off

set H1=%2
set F1=%1

::CODE
for /f "tokens=1-2 delims=," %%a in (listfile.txt) do (
if %%a equ s sqlcmd -S %H1% -i %%b >> sqllog.txt
if %%a equ c copy %%b %F1%\%%b /y >> copylog.txt
If ERRORLEVEL=1 goto :EOF
)
type Copylog.txt
type SQLlog.txt


Report •

Related Solutions

#4
July 15, 2010 at 13:55:22
That did it...Don't know what the
type Copylog.txt
type SQLlog.txt
is for though.

Report •

#5
July 21, 2010 at 08:45:26
Ok! I tried the below errorlevel but the script never goes to the :FAIL label. Why is it not going to that label.


If ERRORLEVEL=1 goto :FAIL
)

:FAIL
REM THIS IS FAIL


Report •

#6
July 21, 2010 at 09:57:34
Hi traveye2,

It's..

IF ERRORLEVEL 1 GOTO FAIL (method 1)
IF ERROLEVEL==1 GOTO FAIL (method 2)

Kind Regards,

MUs


Report •

#7
July 21, 2010 at 10:07:20
So what is the difference between the two IFstatements? So if filex does not exist I rollback my previous changes. Would I cascade the commented out do statement within the (method1)?

Here is the snippet of code I am trying to run on errorlevel:
for /f "tokens=1-3 delims=," %%a in (listfile.txt) do (
if %%a equ cc copy %%b %F1%\%%b /y
if %%a equ cb copy %%b %F1%\%F2%\%%b /y
if %%a equ cg copy %%b %F1%\%F3%\%%b /y
If ERRORLEVEL=1 goto FAIL ::echo %errorlevel%
)

:FAIL
REM ERROR CODE GOES HERE
::for /f "tokens=1-3 delims=," %%a in (listfile.txt) do (
::if %%a equ cc move %F1%\backup\BAK.%YYYYMMDD%\%%b %F1%\%%b
::if %%a equ cb move %F1%\backup\BAK.%YYYYMMDD%\%%b %F1%\%F2%\%%b
::if %%a equ cg move %F1%\backup\BAK.%YYYYMMDD%\%%b %F1%\%F3%\%%b
::)


Report •

#8
July 21, 2010 at 10:21:16
Hi traveye2,

"So what is the difference between the two IFstatements?"

These were "two" different example's..

::WRONG
IF ERRORLEVEL=1 GOTO FAIL

::GOOD
IF ERRORLEVEL==1 GOTO FAIL

::GOOD#2
IF ERRORLEVEL 1 GOTO FAIl

Kind Regards,

MUs


Report •

#9
July 21, 2010 at 10:34:08
I've tried all possible combinations of an equal. I did this one:
IF ERRORLEVEL==1 GOTO FAIL
and the resulting output still bypassed the FAIL label.

C:\>RepoDesk_template.bat dummy c:\temp

C:\>REM CREATE BACKUPS
1 file(s) copied.
1 file(s) copied.
The system cannot find the file specified.

C:\>REM DIRECTORY BACKUP LISTING
Volume in drive C has no label.
Volume Serial Number is 4894-9444

Directory of c:\temp

File Not Found

C:\>REM COPY CODE
1 file(s) copied.
1 file(s) copied.
The system cannot find the file specified.

C:\>REM INSTALL DIRECTORY LISTING
Volume in drive C has no label.
Volume Serial Number is 4894-9444

Directory of c:\temp

07/21/2010 10:19 AM <DIR> .
07/21/2010 10:19 AM <DIR> ..
07/13/2010 01:18 PM 479,785 AClient.log
07/21/2010 10:16 AM <DIR> backup
07/21/2010 10:19 AM <DIR> bin
07/21/2010 09:43 AM 20 copy1.txt
07/21/2010 10:19 AM <DIR> Graphics
2 File(s) 479,805 bytes
5 Dir(s) 136,743,944,192 bytes free
C:\>


Report •

#10
July 21, 2010 at 12:19:53
my guess would be that the errorlevel is not established inside the "FOR"-loop (delayed expansion issue).
f/e, try this short test, (with non-existant file "a"):
for %%a in (1) do (
copy a b
echo %errorlevel%)
::----
if file "a" doesn't exist, error should be one, but it is not reflected because the errorlevel is not established inside the loop, so you'll get zero.
you can either use delayedexpansion, or a call statement:
setlocal enabledelayedexpansion
for %%a in (1) do (
copy a b
echo !errorlevel!)
::---- or: (call-method)
for %%a in (1) do (
copy a b
call :test)
goto :eof
:test
echo %errorlevel%


Report •

#11
July 21, 2010 at 13:04:07
I think I figured this out but I need to make a fix to bypass this. If I echo %errorlevel% in my existing script it comes back with "0". I think it comes back as "0" because in the listfile.txt is:true but the file does not exist in the local directory for copying so a "0" errorlevel is inherited. Ideas are welcome ...again :)

Report •

Ask Question