How does `pushd` exactly behave regarding path with slashes `/`?

1

Consider the following commands given from a cmd prompt:

D:\test>cd D:/

D:\>cd /test

D:\test>pushd D:/
The syntax of the command is incorrect.

D:\test>pushd "D:/"

D:\>popd

D:\test>cd ..

D:\>pushd /test
The syntax of the command is incorrect.

D:\>pushd "/test"
The syntax of the command is incorrect.

D:\>pushd "D:/test"

D:\test>

It seems that cd process correctly slashes / in general, while pushd only accepts them only if at the same time:

  • The argument is quoted
  • The full path is given

Does this correspond to truth? Is it documented anywhere?

Antonio

Posted 2016-04-01T12:49:22.953

Reputation: 308

The funny thing is that changing to the /? directory using cd /? can give you some help with the cd command – infixed – 2016-04-01T13:07:03.800

@infixed Yep! :) And you cannot change to a hypothetical folder named D by doing cd /D as that is a valid cd option. – Antonio – 2016-04-01T13:12:26.530

It looks like you are using the cmd shell in Microsoft's Windows, therefore you have to put the slash the wrong way around. (This is because of a historical accident, there the slash was used to give options. It seemed like a good idea at the time, as Microsoft had not heard of directories at this time, (or of windowing systems, or networks)). – ctrl-alt-delor – 2016-04-01T21:22:44.993

Windows doesn’t really do forward slashes. They’re traditionally used to indicate switches on the command line. So while your question might be academically interesting, it’s not really about a problem. – Daniel B – 2016-04-01T21:25:14.497

@DanielB I wonder then why they didn't simply implement functions to give error in case / was used (without a valid option). My use case concerns paths generated by cmake to be used in a configure file generating a batch script. – Antonio – 2016-04-01T22:56:31.797

Answers

2

Read MSDN article Naming Files, Paths, and Namespaces:

Naming Conventions The following fundamental rules enable applications to create and process valid names for files and directories, regardless of the file system:

  • Use a period to separate the base file name from the extension in the name of a directory or file.
  • Use a backslash (\) to separate the components of a path. The backslash divides the file name from the path to it, and one directory name from another directory name in a path. You cannot use a backslash in the name for the actual file or directory because it is a reserved character that separates the names into components.
  • Use a backslash as required as part of volume names, for example, the "C:\" in "C:\path\file" or the "\\server\share" in "\\server\share\path\file" for Universal Naming Convention (UNC) names. For more information about UNC names, see the Maximum Path Length Limitation section.

More reading on (\)/ (reverse) solidus: Why does Windows use backslashes for paths and Unix forward slashes?

In the command interpreter (cmd.exe), you can use / as a separator of path components in many cases, but not always. Example:

==> d:\bat\so\second.bat a b c
second.bat parameters: %*=a b c

==> d:/bat/so/second.bat a b c
second.bat parameters: %*=a b c

==> type d:/bat/so/second.bat
The syntax of the command is incorrect.

==> type "d:/bat/so/second.bat"
The system cannot find the file specified.

==> type d:/bat/so\second.bat
The syntax of the command is incorrect.

==> type "d:/bat/so\second.bat"
@echo %~nx0 parameters: %%*=%*

==>

Another example:

==> dir d:/bat/so/second.bat
Parameter format not correct - "bat".

==> dir "d:/bat/so/second.bat"
 Volume in drive D is DataDisk
 Volume Serial Number is 4288-6B27

 Directory of d:\bat\so

File Not Found

==> dir "d:/bat/so\second.bat"
 Volume in drive D is DataDisk
 Volume Serial Number is 4288-6B27

 Directory of d:\bat\so

27.11.2015  17:35                32 second.bat
               1 File(s)             32 bytes
               0 Dir(s)  910 153 654 272 bytes free

==>

JosefZ

Posted 2016-04-01T12:49:22.953

Reputation: 9 121

The last example is amazing! :) – Antonio – 2016-04-01T22:57:36.820