Page 1 of 1

Passing variables/arguments between programs

Posted: 13 Aug 2009 09:07
by BuckeyeDave
I am new to DOS/CMD programming. I am writing a script to automate the backing up of certain user files and the user state. That script calls this one, called "scanstate.cmd".

I am introducing 2 new variables:
"rerun" is used to check to make sure that scanstate.exe ran without error. If scanstate.exe errors, it returns an errorlevel=4.
"continue" is used to verify that scanstate.exe is done and return processing to the original script.

I need the values of the variables to be passed to the original script as arguments.

(I have the line "set errorlevel=4" in the code to force the subsequent If Conditional to run, for testing purposes.)

In the If conditional, "set rerun=1" doesn't seem to be working. Neither does "set continue=1".

What am I doing wrong?

====================================
@echo on

set rerun=0
set continue=0
echo %rerun%
echo %continue%
pause

echo Enter Log In Name of User being Refreshed
set /P strAcct= User :
Set strUser=%strDomain%\%strAcct%

Start "Copying User State" /b /wait "c:\_Deployment\ScanState\scanstate.exe" c:\_Deployment\Backup /ue:*\* /ui:ent\%strAcct%

rem set errorlevel=4

if %errorlevel% == 4 (
set rerun=1
echo %rerun%
echo Scanstate failed. Restarting scan.
rem del c:\_Deployment\Backup\* /s
Start "" /b /wait "C:\_Deployment\Backup.cmd" %rerun% %continue%
echo error level %errorlevel%
echo %rerun% %continue%
Pause
)


Set continue=1
echo %rerun% %continue%
Pause
Start "" /b /wait "C:\_Deployment\Backup.cmd" %rerun% %continue%
===============================================

Thanks,
Dave

Posted: 13 Aug 2009 14:20
by avery_larry
A) Use call instead of start when running a batch file from another batch file:

Code: Select all

call "C:\_Deployment\Backup.cmd" %rerun% %continue%




2) All of the lines inside parentheses (IF statements and FOR loops as examples) are evaluated as a group, which means that the variables are evaluated at that time, which subsequently means that CHANGING the value of a variable one a line that's inside parentheses will NOT change the value of the variable on lines that follow. The variable WILL be changed after you exit the parentheses (the IF or FOR loop)

To access the CHANGED value of the variable, you have to use delayedexpansion and evaluate the variable with ! instead of % like this:

Code: Select all

setlocal enabledelayedexpansion

. . .

if %errorlevel% == 4 (
set rerun=1
echo !rerun!
echo Scanstate failed. Restarting scan.
rem del c:\_Deployment\Backup\* /s
call "C:\_Deployment\Backup.cmd" !rerun! %continue%
echo error level !errorlevel!
echo !rerun! %continue%
Pause
)

Posted: 13 Aug 2009 15:07
by BuckeyeDave
I'll give it a try.

THANKS!

Posted: 13 Aug 2009 15:17
by BuckeyeDave
Ok, here's the new code:

rem set errorlevel=4

setlocal enabledelayedexpansion

if %errorlevel% == 4 (
set rerun=1
echo !rerun!
echo Scanstate failed. Restarting scan.
rem del c:\_Deployment\Backup\* /s
Call "C:\_Deployment\Backup.cmd" !rerun! %continue%
rem Start "" /b /wait "C:\_Deployment\Backup.cmd" !rerun! %continue%
echo error level %errorlevel%
echo !rerun! %continue%
Pause
)



Still not working...

Posted: 14 Aug 2009 07:02
by BuckeyeDave
I realized I had "set errorlevel=4" rem'ed out, so I fixed that & ran it with this code:

setlocal enabledelayedexpansion

set errorlevel=4

if %errorlevel% == 4 (
set rerun=1
echo !rerun!
echo Scanstate failed. Restarting scan.
rem del c:\_Deployment\Backup\* /s
Call "C:\_Deployment\Backup.cmd" !rerun! %continue%
rem Start "" /b /wait "C:\_Deployment\Backup.cmd" !rerun! %continue%
echo error level %errorlevel%
echo !rerun! %continue%
Pause
)

This is the output (echo is on):

setlocal enabledealyedexpansion
set errorlevel=4
if 4==4 (
echo !rerun!
echo scanstate failed. Restarting scan.
rem del c:\_Deployment\Backup\* /s
Call"C:\_Deployment\Backup.cmd" !rerun!
Rem Start "" /b /wait "C"\_Deployment\Backup.cmd" !rerun!
echo error level 4
echo !rerun!
Pause
)
1
Scanstate failed. Restarting scan.
The System cannot find the path specified. (I expected this, see below.)
error level 4
1
Press any key to continue . . .

So it is working. Thanks for all your help!

I am testing on a different PC right now, but I will test again on the right PC, in the right environment as soon as I can. It may be a week or more...

Posted: 14 Aug 2009 09:19
by avery_larry
Inside the if statement you need to use !errorlevel! towards the end.

Posted: 25 Aug 2009 10:00
by BuckeyeDave
Looks like it works!

THANKS!