Andrei Daniel
Andrei Daniel

Reputation: 181

When to use / or \ in Windows CMD?

Is there some rule for using \ or /?

For example, both cd c:/ and c:\ works fine. Using mkdir x/y does not work, saying that "syntax is incorrect", but works with mkdir x\y.

Can anyone explain what's the difference?

Upvotes: 2

Views: 3358

Answers (1)

Mofi
Mofi

Reputation: 49086

On Windows the forward slash / is in general for parameters and the backslash \ is the directory separator in file/folder names with absolute or relative paths, see Microsoft documentation Naming Files, Paths, and Namespaces.

The Windows file system kernel functions handle also file/folder paths with / as directory separator by auto-correcting them to \ internally for a better compatibility with Unix/Mac platforms like #include statements in C/C++/C# source code files referencing header files with relative paths using / as directory separator or URLs in HTML files with also using / as separator. That are just two of many examples on where a forward slash is used as separator between strings representing directories on file system.

But a Windows batch file should be written with using only \ as directory separator in file/folder strings.

On Unix/Mac the directory separator is / and - is used for options which is sometimes problematic because of file/folder names can also start with character -.

How argument strings are parsed depends on used compiler and the application/command itself. There are some general rules for each platform, but each programmer of an application can define the argument parsing rules by oneself. So it is always recommended to read the documentation of the application or command to use. On Windows this can be in general done by running the command/application with /? while on Unix/Mac the option to get help is usually -? or -h or --help.

There are many applications ported from Unix to Windows which require parameters being specified on command line with - instead of / like ping.

Upvotes: 5

Related Questions