Computing.Net > Forums > Windows XP > Delayed Variable Expansion in batch

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Delayed Variable Expansion in batch

Reply to Message Icon

Name: ASBk
Date: January 10, 2008 at 16:10:30 Pacific
OS: Windows XP 5.1 SP2
CPU/Ram: Intel Xeon 2.66GHz/3.00GB
Product: HP/xw8400
Comment:

Hi.

I am having trouble with delayed variable expansion in a batch file. Unfortunately I've been unsuccessful in trying to reproduce this problem in a simplified batch file.

An example simplified batch file, that works as expected, is:

@echo off
setlocal enabledelayedexpansion
set XXX=123456
echo !XXX!
set XXX=!XXX:~0,3!.!XXX:~3!
echo !XXX!
endlocal
pause
exit

When I run the above, the output, as expected, is:
123456
123.456

Now, since I couldn't reproduce the problem I'm having, I must provide a smaller version of my batch file that doesn't work (I've added comments to make it easier to read):
@echo off

REM setup.bat sets some environment variables.
call X:\setup\setup.bat

REM Changing to existing network drive R:
cd /d R:\

REM Enable delayed expansion
setlocal enabledelayedexpansion

for /d %%i in (*) do (
cd %%i
echo processing %%i...
for /d %%j in (*) do (
rem generate
copy /y ..\file1.bat . >nul

:createfile1
set XXX=XXX!random!
if exist !XXX! goto createfile1

REM Program placevar in this case has the output 600.0000, and therefore adds 600.0000 to the file !XXX!
placevar \\drive\%%j.NEW input1 input2 input3 > !XXX!

REM Batch program setenvtofile sets the variable ZZZ equal to the contents of the file !XXX!
call setenvtofile ZZZ !XXX!

echo ZZZ is !ZZZ! %%j of %%i >> R:\running.log
set ZZZ=!ZZZ:.0000=!
echo ZZZ is !ZZZ! after first modification: removal of .0000 >> R:\running.log
echo the two pieces I want to work with are !ZZZ:~0,1! and !ZZZ:~-2! >> R:\running.log
set ZZZ=!ZZZ:~0,1!.!ZZZ:~-2! >> R:\running.log
echo ZZZ is !ZZZ! after second modification >> R:\running.log

REM Delete temporary file.
del !XXX!

)
cd ..
)

:end
endlocal
exit

--------
Given: first %%i is 200
first %%j is XYZ
output of placevar is 600.0000
When this batch file is run, I expect the output (in file "running.log") to be:

echo processing 200...
ZZZ is 600.0000 for XYZ of 200
ZZZ is 600 after first modification: removal of .0000
the two pieces I want to work with are 6 and 00
ZZZ is 6.00 after second modification

--------
Instead, the output is:

echo processing 200...
ZZZ is 600.0000 for XYZ of 200
ZZZ is 600 after first modification: removal of .0000
the two pieces I want to work with are and
ZZZ is . after second modification

---------
I hope that this is clear.
Thank you VERY much for any help.
ASBk



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: January 11, 2008 at 03:20:24 Pacific
Reply:

I do not understand fully your script, but I have to point out two things.

The first, that is of little matter here, can cause troubles in future runs

:createfile1
set XXX=XXX!random!
if exist !XXX! goto createfile1

Goto inside a For/Do loop may disrupt correct instructions' flow (in your case only if the "if exist" is satisfied).

The second note is more relevant and I think it is the key of the failure.

You can't set environmental variables by calling a batch as the Call statement invokes a secondary command processor whose environment is inherited from the parent one, BUT disregarded at the return point.

So the critical lines are

call X:\setup\setup.bat

and

call setenvtofile ZZZ !XXX!

I keep this supposition higly pertinent, but as I said I don't grasp fully your code.


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Windows XP Forum Home


Sponsored links

Ads by Google


Results for: Delayed Variable Expansion in batch

How can I print out a variable in batch file www.computing.net/answers/windows-xp/how-can-i-print-out-a-variable-in-batch-file/179789.html

Delayed XP logging in www.computing.net/answers/windows-xp/delayed-xp-logging-in/93989.html

difference b/w %%g and %g% in batch file www.computing.net/answers/windows-xp/difference-bw-g-and-g-in-batch-file/176113.html