Variables in batch file names

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
aGerman
Expert
Posts: 4744
Joined: 22 Jan 2010 18:01
Location: Germany

Variables in batch file names

#1 Post by aGerman » 29 Sep 2014 13:24

As mentioned in another thread there is a strange behavior if you use a variable in batch names. The topic does NOT mean that you can use variables inside of a batch code to call another file. In that case we expect that the variable is expanded to its value first. I meant variables used in batch file names in the windows file system (e.g. "test %path%.bat" or the like). If you execute those files via double click the variable will be expanded in a command line context. That is
- If the variable is not defined it will be interpreted as nothing but a literal expression (e.g. "test %novariable%.bat"). Such file names won't cause any problems.
- If the variable is predefined (e.g. "test %path%.bat" or "test %prompt%.bat") the variables are expanded. That causes error messages or funny side effects.

Lets talk about the latter where I can imagine two possibilities.
- The variable value contains characters that are invalid in file names (e.g. a colon). In that case the cmd complains about wrong syntax.
- The variable contains a value that is valid for file names. In that case the cmd will probably complain that it isn't able to find the file.

BUT what happens if the file exists? Quite a good question. It depends ... :wink:

Lets create two batch files in the same directory. First one named "%prompt%.bat", the second "$P$G.bat" (where $P$G is the default value of %prompt%, you should change the name if it differs).
Some cases:

- "%prompt%.bat" is empty, don't care about the content of "$P$G.bat":
The OS checks whether a batch file is empty or not. You would get an error message that the file isn't a valid application as soon as you double click on "%prompt%.bat".

- "%prompt%.bat" contains at least one arbitrary character, "$P$G.bat" contains a valid code:
If you double click on "%prompt%.bat" the code in "$P$G.bat" will be executed because %prompt% was expanded to $P$G. Although if you check the CMDCMDLINE variable you will still find the originally opened "%prompt%.bat". Arguments passed to "%prompt%.bat" are accessible in "$P$G.bat".

- "%prompt%.bat" contains at least one arbitrary character, "$P$G.bat" is empty:
If you double click on "%prompt%.bat" the "$P$G.bat" file will be executed. That means the cmd window opens and closes immediately because the end of the file was reached. The OS doesn't complain because the check for emptyness was done to "%prompt%.bat" before.

Regards
aGerman

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Variables in batch file names

#2 Post by foxidrive » 29 Sep 2014 18:15

tl;dr

My comment is that anyone who uses variables in a filename deserves what they get. :D

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Variables in batch file names

#3 Post by ShadowThief » 29 Sep 2014 18:19

For some reason, I could have sworn that % was one of those symbols that Windows didn't let you use in filenames, like \

aGerman
Expert
Posts: 4744
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Variables in batch file names

#4 Post by aGerman » 30 Sep 2014 04:41

foxidrive wrote:tl;dr

Not fair :lol: I had to write it in a foreign language just to make it readable for foxidrive. (just kidding)
Eventually your comment is right though.

ShadowThief wrote:I could have sworn that % was one of those symbols that Windows didn't let you use in filenames

As long as I remember percent signs were valid under Windows.

Regards
aGerman

Post Reply