DOS: SET will not work, variable keeps its old value

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
stuifbergen
Posts: 2
Joined: 01 Aug 2008 03:05

DOS: SET will not work, variable keeps its old value

#1 Post by stuifbergen » 01 Aug 2008 03:18

The script will be executed via command prompt on XP.

The problem is the variable ORIG_FILENAME keeps its old value and won't be updated. The value should come out of a file which with be read using the FOR command.

This is the code:

Code: Select all

echo START FINDSTR

  findstr /m "%%b" C:\Temp\gasdats\*.* > tmp_filename.txt && (

    echo "%%ORIGFILE_NAME%% (for)" %ORIG_FILENAME%

    for /f "tokens=4-13 delims=\_" %%k in (tmp_filename.txt) do set ORIG_FILENAME=%%k_%%l_%%m_%%n_%%o__%%p_%%q_%%r_%%s

    echo "%%ORIGFILE_NAME%% (after)" %ORIG_FILENAME%
   

    echo ^<td^>%ORIG_FILENAME%^</td^> >> report_gasdats_%UNIQUE%.html
    echo ^<td^> >> report_gasdats_%UNIQUE%.html

    echo "DO SQLQUERY WITH %%ORIG_FILENAME%%" %ORIG_FILENAME%


    sqlplus -s /nolog @query2.sql %ORIG_FILENAME% >> report_gasdats_%UNIQUE%.html

    echo ^</td^> >> report_gasdats_%UNIQUE%.html

  ) || (


    echo ^<td^>NEE^</td^>^<td^>-^</td^> >> report_gasdats_%UNIQUE%.html
    echo ^<td^>-^</td^>
  )

  echo EIND FINDSTR




And in the logging where we can see the variable ORIG_FILENAME keeps its old value CATS_RECVD_2008_07_29__14_43_01_667.dat.cmpr.DA. The logging says the SET command has updated ORIG_FILENAME to CATS_RECVD_2008_07_29__15_02_26_621.dat.


Code: Select all

START FINDSTR
"%ORIGFILE_NAME% (for)" CATS_RECVD_2008_07_29__14_43_01_667.dat.cmpr.DA

C:\Temp>set ORIG_FILENAME=CATS_RECVD_2008_07_29__15_02_26_621.dat
"%ORIGFILE_NAME% (after)" CATS_RECVD_2008_07_29__14_43_01_667.dat.cmpr.DA
"DO SQLQUERY WITH %ORIG_FILENAME%" CATS_RECVD_2008_07_29__14_43_01_667.dat.cmpr.DA
EIND FINDSTR



Can somebody help me with this?

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 28 Aug 2008 20:38

stuifbergen,

The variable is set correctly. The problem occurs when querying the variable.
The command interpreter substitutes variables before executing a block. The whole for loop is considered a block.
By escaping the percent signs and CALL the command is forced to evaluate the variable within the loop.

Use the following code within the block and you should be fine:

Code: Select all

(
  call echo "%%%%ORIGFILE_NAME%%%% (for)" %%ORIG_FILENAME%%
  ...
  call echo "%%%%ORIGFILE_NAME%%%% (after)" %%ORIG_FILENAME%%
)

DosItHelp? :wink:

stuifbergen
Posts: 2
Joined: 01 Aug 2008 03:05

#3 Post by stuifbergen » 29 Aug 2008 00:45

Thnx for your reaction.

I allready found a solution that works. I enabled the DELAYED EXPANSION and user ! instead of %. This should be a solution because the variables are created and used again in the for loop.

Post Reply