if loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ilugopal
Posts: 1
Joined: 23 Sep 2016 15:29

if loop

#1 Post by ilugopal » 23 Sep 2016 15:37

Hi all - I'm writing a batch script for the first time. Its a learning experience. However i need to some help on accomplish my thought process as google is not all.

Code: Select all

FOR %%i in (%OneTime_Dir%\\*.sql) DO (
SET FileName=%%~ni
Set first2=!FileName:~0,2!
echo !FileName!
echo !first2!
IF "!first2!" == "SP" (
     db2 -td@ -vf %%i -z %Log_Dir%\%%~ni_%FileExt%
     SET Error=%errorlevel%
     echo !Error!
) ELSE (
     db2 -tvf %%i -z %Log_Dir%\%%~ni_%FileExt%
)
)


here I'm not able to do a small change or align the brackets ( ) or ELSE to next line as it says syntax error or ELSE is not expected here.

1 ) So all I need is the IF condition's syntax
2 ) I need to capture the %errorlevel% after the db2 command gets completed
so I tought of doing this after db2 command SET Error=%errorlevel% but is not accepting commands in the same line.

Any help is appreciated...
Last edited by Squashman on 23 Sep 2016 15:46, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: if loop

#2 Post by aGerman » 24 Sep 2016 04:20

The changed errorlevel value isn't accessible if you don't use surrounding exclamation marks.

The IF ELSE syntax doesn't seem to be wrong. Because you didn't post the whole code I don't know what else could have been going wrong there.

Some suggestions
- enclose assignments into a pair of quotation marks in order to protect you from problems with special characters and unwanted trailing spaces.
set "variable=value"
- make sure file names and pathes are quoted in order to avoid problems with special characters and spaces
whatevercommand "%mypath%\%myfilename%"

Post Reply