Batch script variables not working as expected

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
plank
Posts: 8
Joined: 31 May 2011 03:11

Batch script variables not working as expected

#1 Post by plank » 31 May 2011 03:23

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

plank
Posts: 8
Joined: 31 May 2011 03:11

Re: Batch script variables not working as expected

#2 Post by plank » 31 May 2011 03:25

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch script variables not working as expected

#3 Post by Ed Dyreen » 31 May 2011 03:37

There was an error
Last edited by Ed Dyreen on 31 May 2011 04:22, edited 2 times in total.

plank
Posts: 8
Joined: 31 May 2011 03:11

Re: Batch script variables not working as expected

#4 Post by plank » 31 May 2011 03:41

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch script variables not working as expected

#5 Post by Ed Dyreen » 31 May 2011 03:44

There was an error
Last edited by Ed Dyreen on 31 May 2011 04:23, edited 3 times in total.

plank
Posts: 8
Joined: 31 May 2011 03:11

Re: Batch script variables not working as expected

#6 Post by plank » 31 May 2011 03:49

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 . . .

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch script variables not working as expected

#7 Post by Ed Dyreen » 31 May 2011 03:51

I get it now :oops:

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
Last edited by Ed Dyreen on 31 May 2011 03:59, edited 1 time in total.

plank
Posts: 8
Joined: 31 May 2011 03:11

Re: Batch script variables not working as expected

#8 Post by plank » 31 May 2011 03:58

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch script variables not working as expected

#9 Post by Ed Dyreen » 31 May 2011 04:01

:?:
post the code please. the one that doesn't work.

plank
Posts: 8
Joined: 31 May 2011 03:11

Re: Batch script variables not working as expected

#10 Post by plank » 31 May 2011 04:06

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch script variables not working as expected

#11 Post by Ed Dyreen » 31 May 2011 04:10

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
Last edited by Ed Dyreen on 31 May 2011 04:20, edited 2 times in total.

plank
Posts: 8
Joined: 31 May 2011 03:11

Re: Batch script variables not working as expected

#12 Post by plank » 31 May 2011 04:17

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!

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch script variables not working as expected

#13 Post by Ed Dyreen » 31 May 2011 04:21

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

plank
Posts: 8
Joined: 31 May 2011 03:11

Re: Batch script variables not working as expected

#14 Post by plank » 31 May 2011 04:40

Thank you so much! It now works perfectly.

Your a star!

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Batch script variables not working as expected

#15 Post by orange_batch » 31 May 2011 11:48

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
)

Post Reply