Variable beginning with a numeral doesn't work as expected in batch file

F

Frank B Denman

Hi Folks,

I'm getting into trouble in a batch file when I set a variable name that begins with a number. I'm
hoping somebody can help me understand why.

This batch file works just fine:
-------------------------------------------
SET A_FOLDER=D:\Temp\test\Able
SET B_FOLDER=D:\Temp\test\Baker
SET C_FOLDER=D:\Temp\test\Charley
SET _4C_FOLDER=D:\Temp\test\4_Charley
SET DIR_OUTPUT=D:\Temp\test\Delta\Appended_Dir_List.txt
::
dir "%A_FOLDER%">"%DIR_OUTPUT%"
dir "%B_FOLDER%">>"%DIR_OUTPUT%"
dir "%C_FOLDER%">>"%DIR_OUTPUT%"
dir "%_4C_FOLDER%">>"%DIR_OUTPUT%"
--------------------------------------------
However, if I lose the leading underscore on the fourth variable, so that the batch file looks like
this:
--------------------------------------------
SET A_FOLDER=D:\Temp\test\Able
SET B_FOLDER=D:\Temp\test\Baker
SET C_FOLDER=D:\Temp\test\Charley
SET 4C_FOLDER=D:\Temp\test\4_Charley
SET DIR_OUTPUT=D:\Temp\test\Delta\Appended_Dir_List.txt
::
dir "%A_FOLDER%">"%DIR_OUTPUT%"
dir "%B_FOLDER%">>"%DIR_OUTPUT%"
dir "%C_FOLDER%">>"%DIR_OUTPUT%"
dir "%4C_FOLDER%">>"%DIR_OUTPUT%"
--------------------------------------------
The last line now fails. Here's the output at the command prompt:
---------------------------------------------
D:\Temp\test>dirlist

D:\Temp\test>SET A_FOLDER=D:\Temp\test\Able

D:\Temp\test>SET B_FOLDER=D:\Temp\test\Baker

D:\Temp\test>SET C_FOLDER=D:\Temp\test\Charley

D:\Temp\test>SET 4C_FOLDER=D:\Temp\test\4_Charley

D:\Temp\test>SET DIR_OUTPUT=D:\Temp\test\Delta\Appended_Dir_List.txt

D:\Temp\test>dir "D:\Temp\test\Able" 1>"D:\Temp\test\Delta\Appended_Dir_List.txt"

D:\Temp\test>dir "D:\Temp\test\Baker" 1>>"D:\Temp\test\Delta\Appended_Dir_List.txt"

D:\Temp\test>dir "D:\Temp\test\Charley" 1>>"D:\Temp\test\Delta\Appended_Dir_List.txt"

D:\Temp\test>dir "C_FOLDERDIR_OUTPUT"
Volume in drive D is Data
Volume Serial Number is 94B6-1DC6

Directory of D:\Temp\test

File Not Found
D:\Temp\test>
---------------------------------------------
The final dir command seems to have concatenated elements of the line immediately above it. I would
really like to understand why.

Thanks for your help!

Frank

Frank Denman
Denman Systems
(e-mail address removed)
[Please delete the "x" from my email address]
 
F

foxidrive

SET 4C_FOLDER=D:\Temp\test\4_Charley
dir "%4C_FOLDER%">>"%DIR_OUTPUT%"
The final dir command seems to have concatenated elements of the line immediately above it. I would
really like to understand why.

%0 to %9 has a special significance to batch files and the command
interpreter is seeing %4 followed by C_FOLDER etc and a mismatched set of %
signs.
 
E

Esra Sdrawkcab

Frank said:
Hi Folks,

I'm getting into trouble in a batch file when I set a variable name that begins with a number. I'm
hoping somebody can help me understand why.

This batch file works just fine:
-------------------------------------------
SET A_FOLDER=D:\Temp\test\Able
SET B_FOLDER=D:\Temp\test\Baker
SET C_FOLDER=D:\Temp\test\Charley
SET _4C_FOLDER=D:\Temp\test\4_Charley
SET DIR_OUTPUT=D:\Temp\test\Delta\Appended_Dir_List.txt
::
dir "%A_FOLDER%">"%DIR_OUTPUT%"
dir "%B_FOLDER%">>"%DIR_OUTPUT%"
dir "%C_FOLDER%">>"%DIR_OUTPUT%"
dir "%_4C_FOLDER%">>"%DIR_OUTPUT%"
--------------------------------------------
However, if I lose the leading underscore on the fourth variable, so that the batch file looks like
this:
--------------------------------------------
SET A_FOLDER=D:\Temp\test\Able
SET B_FOLDER=D:\Temp\test\Baker
SET C_FOLDER=D:\Temp\test\Charley
SET 4C_FOLDER=D:\Temp\test\4_Charley
SET DIR_OUTPUT=D:\Temp\test\Delta\Appended_Dir_List.txt
::
dir "%A_FOLDER%">"%DIR_OUTPUT%"
dir "%B_FOLDER%">>"%DIR_OUTPUT%"
dir "%C_FOLDER%">>"%DIR_OUTPUT%"
dir "%4C_FOLDER%">>"%DIR_OUTPUT%"


the '%4' above is first parsed to substitute the fourth parameter passed
into the batch file, as this is probably blank, you get the other set
string, C_FOLDER instead.

if you called your set 4_C_FOLDER I suggest that the dir command would
fail in a less puzzling manner!

You have already hit upon a good workaround; prepend the numeric
variable names with an underscore (i.e. don't have a variable starting
with a number)
D:\Temp\test>dirlist

D:\Temp\test>SET A_FOLDER=D:\Temp\test\Able

D:\Temp\test>SET B_FOLDER=D:\Temp\test\Baker

D:\Temp\test>SET C_FOLDER=D:\Temp\test\Charley

D:\Temp\test>SET 4C_FOLDER=D:\Temp\test\4_Charley

D:\Temp\test>SET DIR_OUTPUT=D:\Temp\test\Delta\Appended_Dir_List.txt

D:\Temp\test>dir "D:\Temp\test\Able" 1>"D:\Temp\test\Delta\Appended_Dir_List.txt"

D:\Temp\test>dir "D:\Temp\test\Baker" 1>>"D:\Temp\test\Delta\Appended_Dir_List.txt"

D:\Temp\test>dir "D:\Temp\test\Charley" 1>>"D:\Temp\test\Delta\Appended_Dir_List.txt"

D:\Temp\test>dir "C_FOLDERDIR_OUTPUT"
Volume in drive D is Data
Volume Serial Number is 94B6-1DC6

Directory of D:\Temp\test

File Not Found
D:\Temp\test>
---------------------------------------------
The final dir command seems to have concatenated elements of the line immediately above it. I would
really like to understand why.

Thanks for your help!

Frank

Frank Denman
Denman Systems
(e-mail address removed)
[Please delete the "x" from my email address]
 
F

Frank B Denman

Thanks, guys, for all the good advice!

Frank



Frank said:
Hi Folks,

I'm getting into trouble in a batch file when I set a variable name that begins with a number. I'm
hoping somebody can help me understand why.

This batch file works just fine:
-------------------------------------------
SET A_FOLDER=D:\Temp\test\Able
SET B_FOLDER=D:\Temp\test\Baker
SET C_FOLDER=D:\Temp\test\Charley
SET _4C_FOLDER=D:\Temp\test\4_Charley
SET DIR_OUTPUT=D:\Temp\test\Delta\Appended_Dir_List.txt
::
dir "%A_FOLDER%">"%DIR_OUTPUT%"
dir "%B_FOLDER%">>"%DIR_OUTPUT%"
dir "%C_FOLDER%">>"%DIR_OUTPUT%"
dir "%_4C_FOLDER%">>"%DIR_OUTPUT%"
--------------------------------------------
However, if I lose the leading underscore on the fourth variable, so that the batch file looks like
this:
--------------------------------------------
SET A_FOLDER=D:\Temp\test\Able
SET B_FOLDER=D:\Temp\test\Baker
SET C_FOLDER=D:\Temp\test\Charley
SET 4C_FOLDER=D:\Temp\test\4_Charley
SET DIR_OUTPUT=D:\Temp\test\Delta\Appended_Dir_List.txt
::
dir "%A_FOLDER%">"%DIR_OUTPUT%"
dir "%B_FOLDER%">>"%DIR_OUTPUT%"
dir "%C_FOLDER%">>"%DIR_OUTPUT%"
dir "%4C_FOLDER%">>"%DIR_OUTPUT%"


the '%4' above is first parsed to substitute the fourth parameter passed
into the batch file, as this is probably blank, you get the other set
string, C_FOLDER instead.

if you called your set 4_C_FOLDER I suggest that the dir command would
fail in a less puzzling manner!

You have already hit upon a good workaround; prepend the numeric
variable names with an underscore (i.e. don't have a variable starting
with a number)
D:\Temp\test>dirlist

D:\Temp\test>SET A_FOLDER=D:\Temp\test\Able

D:\Temp\test>SET B_FOLDER=D:\Temp\test\Baker

D:\Temp\test>SET C_FOLDER=D:\Temp\test\Charley

D:\Temp\test>SET 4C_FOLDER=D:\Temp\test\4_Charley

D:\Temp\test>SET DIR_OUTPUT=D:\Temp\test\Delta\Appended_Dir_List.txt

D:\Temp\test>dir "D:\Temp\test\Able" 1>"D:\Temp\test\Delta\Appended_Dir_List.txt"

D:\Temp\test>dir "D:\Temp\test\Baker" 1>>"D:\Temp\test\Delta\Appended_Dir_List.txt"

D:\Temp\test>dir "D:\Temp\test\Charley" 1>>"D:\Temp\test\Delta\Appended_Dir_List.txt"

D:\Temp\test>dir "C_FOLDERDIR_OUTPUT"
Volume in drive D is Data
Volume Serial Number is 94B6-1DC6

Directory of D:\Temp\test

File Not Found
D:\Temp\test>
---------------------------------------------
The final dir command seems to have concatenated elements of the line immediately above it. I would
really like to understand why.

Thanks for your help!

Frank

Frank Denman
Denman Systems
(e-mail address removed)
[Please delete the "x" from my email address]
Frank Denman
Denman Systems
(e-mail address removed)
[Please delete the "x" from my email address]
 
Top