Page 1 of 2
Batch script variables not working as expected
Posted: 31 May 2011 03:23
by plank
Hey all,
I have this code
Code: Select all
@echo off
set CURRENTVERSION=10_03_00_136
set SIGFILE=%windir%\Deploy\Salto.sig
set VERSION=X
IF NOT EXIST %SIGFILE% (
call :Install
exit /b
) ELSE (
for /f %%a in (%SIGFILE%) do (
echo VERSION %VERSION%
echo %%a
set VERSION=%%a
echo VERSION AFTER %VERSION%
)
IF %VERSION%==%CURRENTVERSION% (
EXIT /b
) ELSE (
ECHO INSTALL
exit /b
)
)
Now the result i get from this is
VERSION X
10_03_00_136
VERSION AFTER X
INSTALL
What i cant understand is why i dont get
VERSION X
10_03_00_136
VERSION AFTER 10_03_00_136
Its as if it cannot overwrite the variable. However if i dont define the var at the start, then it fails becuase the variable is null.
Any ideas?
TIA
Dan
Re: Batch script variables not working as expected
Posted: 31 May 2011 03:25
by plank
Forgot to mention. If i run the code, it brings back that result. If i then remove the set line for VERSION at the top then run it in the same DOS window it works perfectly.
However it wont run without this line normally because of the null variable doesnt run with the IF statement.
Thanks
Dan
Re: Batch script variables not working as expected
Posted: 31 May 2011 03:37
by Ed Dyreen
There was an error
Re: Batch script variables not working as expected
Posted: 31 May 2011 03:41
by plank
Thanks for the response. However the result is even wierder now
VERSION X
C:\Windows\Deploy\Salto.sig
VERSION AFTER C:\Windows\Deploy\Salto.sig
INSTALL
The file contains one line that says 10_03_00_136
Dan
Re: Batch script variables not working as expected
Posted: 31 May 2011 03:44
by Ed Dyreen
There was an error
Re: Batch script variables not working as expected
Posted: 31 May 2011 03:49
by plank
I still have
C:\>dfs-install-salto.cmd
VERSION X
'C:\Windows\Deploy\Salto.sig'
VERSION AFTER C:\Windows\Deploy\Salto.sig
INSTALL
Press any key to continue . . .
Re: Batch script variables not working as expected
Posted: 31 May 2011 03:51
by Ed Dyreen
I get it now
Code: Select all
@echo off &setlocal enabledelayedexpansion
set "CURRENTVERSION=10_03_00_136"
set "SIGFILE=TST.TXT"
set "VERSION=X"
IF EXIST "!SIGFILE!" (
for /f %%! in (!SIGFILE!) do (
echo VERSION !VERSION!
echo '%%~!'
set "VERSION=%%~!"
echo VERSION AFTER !VERSION!
)
IF ["!VERSION!"] neq ["!CURRENTVERSION!"] (
ECHO INSTALL
)
) ELSE call :Install
pause
EndLocal
exit /b
Re: Batch script variables not working as expected
Posted: 31 May 2011 03:58
by plank
Yeah - i am looking to see if that is the ONLY thing in a file. It should contain a version number for a piece of software. If the version number from the file is incorrect it needs to uninstall the software and install the latest version. I have obviously cut those bits out of the script.
If i run the the original script twice in a dos window (in between rem the set version=X at the top) - the second time will work perfectly.
C:\>dfs-install-salto.cmd
VERSION X
10_03_00_136
VERSION AFTER X
INSTALL
Then do the REM. Then run again.
C:\>dfs-install-salto.cmd
VERSION 10_03_00_136
10_03_00_136
VERSION AFTER 10_03_00_136
Which is perfect. However if i REM that line out and run it for the first time i get.
C:\>dfs-install-salto.cmd
( was unexpected at this time.
Thanks alot for your help!
Dan
Re: Batch script variables not working as expected
Posted: 31 May 2011 04:01
by Ed Dyreen
post the code please. the one that doesn't work.
Re: Batch script variables not working as expected
Posted: 31 May 2011 04:06
by plank
Ok. So back to my original code. here is what i run:
If i run this
Code: Select all
@echo off
set CURRENTVERSION=10_03_00_136
set SIGFILE=%windir%\Deploy\Salto.sig
set VERSION=X
IF NOT EXIST %SIGFILE% (
call :Install
exit /b
) ELSE (
for /f %%a in (%SIGFILE%) do (
echo VERSION %VERSION%
echo %%a
set VERSION=%%a
echo VERSION AFTER %VERSION%
)
IF %VERSION%==%CURRENTVERSION% (
EXIT /b
) ELSE (
ECHO INSTALL
exit /b
)
)
This is the result.
VERSION X
10_03_00_136
VERSION AFTER X
INSTALL
Without closing cmd prompt. If i then run this
Code: Select all
@echo off
set CURRENTVERSION=10_03_00_136
set SIGFILE=%windir%\Deploy\Salto.sig
[b]REM[/b] set VERSION=X
IF NOT EXIST %SIGFILE% (
call :Install
exit /b
) ELSE (
for /f %%a in (%SIGFILE%) do (
echo VERSION %VERSION%
echo %%a
set VERSION=%%a
echo VERSION AFTER %VERSION%
)
IF %VERSION%==%CURRENTVERSION% (
EXIT /b
) ELSE (
ECHO INSTALL
exit /b
)
)
I then get this
VERSION X
10_03_00_136
VERSION AFTER 10_03_00_136
Which is correct. However if i run the above code in a new command prompt - i get this
( was unexpected at this time.
Does that make sense? I hope so.... because i dont understand why?
I am running it on windows 7 if that helps!
Dan
Re: Batch script variables not working as expected
Posted: 31 May 2011 04:10
by Ed Dyreen
try this
Code: Select all
echo "%%a"
set "VERSION=%%a"
echo "VERSION AFTER %VERSION%"
but I don't understand why my last code wouldn't work ??
Here is your problem:
REM set VERSION=X
IF %VERSION%==%CURRENTVERSION%
a comparision needs values on both sides, we can't compare with nothing can we.
You need Delayed Expansion
%expansion at read time%
!expansion at exec time!
Code: Select all
@echo off
(
set x=1
echo.%x%
)
setlocal enabledelayedexpansion
(
set x=1
echo.!x!
)
EndLocal
pause
exit /b
Re: Batch script variables not working as expected
Posted: 31 May 2011 04:17
by plank
This code:
Code: Select all
@echo off
set CURRENTVERSION=10_03_00_136
set SIGFILE=%windir%\Deploy\Salto.sig
set VERSION=X
IF NOT EXIST %SIGFILE% (
call :Install
exit /b
) ELSE (
for /f %%a in (%SIGFILE%) do (
echo "%%a"
set "VERSION=%%a"
echo "VERSION AFTER %VERSION%"
)
IF %VERSION%==%CURRENTVERSION% (
EXIT /b
) ELSE (
ECHO INSTALL
exit /b
)
)
Is giving me:
C:\>test.bat
"10_03_00_136"
"VERSION AFTER X"
INSTALL
Thanks. Sorry for the hassle. I dont understand why either!
Re: Batch script variables not working as expected
Posted: 31 May 2011 04:21
by Ed Dyreen
read the post above please
if you build your ifs like
if "gg" == "fdg" you will have less problems.
if you use
if /i ["dg"] == ["gfx"] you will have even less problems
Re: Batch script variables not working as expected
Posted: 31 May 2011 04:40
by plank
Thank you so much! It now works perfectly.
Your a star!
Re: Batch script variables not working as expected
Posted: 31 May 2011 11:48
by orange_batch
My gosh Ed, can't you just solve it properly? lol
Plank, the problem is because you are trying to use regular variable expansion within parentheses. Command Prompt expands variables within parentheses at the time it READS the code. There's two ways around this, either using a call to delay that expansion or enabling delayed expansion and using the ! ! syntax to expand the variable at execution time.
In your code, you are testing the variable right away within parentheses though, so you have to use ! ! syntax because call only works with commands like set and echo.
Here is your modified code with some minor fixes:
Code: Select all
@echo off&setlocal enabledelayedexpansion
set CURRENTVERSION=10_03_00_136
set SIGFILE="%windir%\Deploy\Salto.sig"
set VERSION=X
IF NOT EXIST %SIGFILE% (
call :Install
exit /b
) ELSE (
for /f "usebackq" %%a in (%SIGFILE%) do (
echo "%%a"
set "VERSION=%%a"
echo "VERSION AFTER !VERSION!"
)
IF "!VERSION!"=="%CURRENTVERSION%" (
EXIT /b
) ELSE (
ECHO INSTALL
exit /b
)
)
If you need to preserve exclamation marks, you need to use enabledelayedexpansion carefully... best done using a flag. The logic behind this is slightly advanced.
Code: Select all
@echo off
set CURRENTVERSION=10_03_00_136
set SIGFILE="%windir%\Deploy\Salto.sig"
set VERSION=X
IF NOT EXIST %SIGFILE% (
call :Install
exit /b
) ELSE (
setlocal enabledelayedexpansion&set delayed=on
for /f "usebackq" %%a in (%SIGFILE%) do (
echo "%%a"
set "VERSION=%%a"
echo "VERSION AFTER !VERSION!"
)
IF "!VERSION!"=="%CURRENTVERSION%" (
EXIT /b
) ELSE (
if defined delayed endlocal
ECHO INSTALL
exit /b
)
if defined delayed endlocal
)