2

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?

5
  • The funny thing is that changing to the /? directory using cd /? can give you some help with the cd command
    – infixed
    Apr 1, 2016 at 13:07
  • @infixed Yep! :) And you cannot change to a hypothetical folder named D by doing cd /D as that is a valid cd option.
    – Antonio
    Apr 1, 2016 at 13:12
  • 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)). Apr 1, 2016 at 21:22
  • 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
    Apr 1, 2016 at 21:25
  • @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
    Apr 1, 2016 at 22:56

1 Answer 1

3

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

==>
1
  • The last example is amazing! :)
    – Antonio
    Apr 1, 2016 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.