Is there any standard equivalent of a .bashrc script for cmd?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
OJBakker
Expert
Posts: 90
Joined: 12 Aug 2011 13:57

Re: Is there any standard equivalent of a .bashrc script for cmd?

#16 Post by OJBakker » 02 Jun 2023 13:41

Code: Select all

SystemRoot=C:\WINDOWS
So

Code: Select all

"%SystemRoot%/system32/cmd.exe"
becomes

Code: Select all

"C:\WINDOWS/system32/cmd.exe"
what gets executed is

Code: Select all

md .exe
so probably something like this happens
command looks like %ComSpec% but with some forward slashes instead of backslashes.

Code: Select all

ComSpec=C:\WINDOWS\system32\cmd.exe
command is interpreted as starting with %ComSpec% followed by /cmd.exe
there is a /c parameter in the command,
so add a separator space before and after this parameter.
Now the command has changed to

Code: Select all

%ComSpec% /c md.exe
so the command to be executed is md
add a separator space after the md command
resulting in the observed

Code: Select all

%comspec% /c md .exe
cmd trying to be smart, but sometimes it is just plain stupid.

mataha
Posts: 35
Joined: 27 Apr 2023 12:34

Re: Is there any standard equivalent of a .bashrc script for cmd?

#17 Post by mataha » 13 Jun 2023 03:52

It gets even better.

Code: Select all

> subst K: C:\WINDOWS\System32

> K:/cmd
The syntax of the command is incorrect.

> K:cmd
Microsoft Windows [Version 10.0.19045.2965]
...
With K:/cmd it becomes evident - Command Prompt parses the whole command line from the beginning, disregarding that it was used to execute the application in the first place.
It sees /c, so it starts to parse the command to execute immediately - but md is incorrect, as a path is required, thus an error is thrown
An even more evident example:

Code: Select all

cmd/cecho/hi
This means that %CMDCMDLINE% is ripe for raw parsing as long as you get rid of slash escapes (//) and poisonous characters.
I've started to work on that here, but I have yet to thoroughly test and stabilize this.

Post Reply