Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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
exitWhen I run the above, the output, as expected, is:
123456
123.456Now, 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 offREM setup.bat sets some environment variables.
call X:\setup\setup.batREM Changing to existing network drive R:
cd /d R:\REM Enable delayed expansion
setlocal enabledelayedexpansionfor /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 createfile1REM 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.logREM 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

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

![]() |
![]() |
![]() |

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