Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

batch file - Documented behavior for multiple backslashes in Windows paths

Apparently, Windows (or at least some part of Windows) ignores multiple backslashes in a path and treats them as a single backslash. For example, executing any of these commands from a command prompt or the Run window opens Notepad:

C:WindowsSystem32Notepad.exe
C:WindowsSystem32\Notepad.exe
C:WindowsSystem32\Notepad.exe
C:WindowsSystem32\\Notepad.exe
C:\Windows\System32\Notepad.exe
C:\Windows\System32\Notepad.exe

This can even work with arguments passed on the command line:

notepad "C:UsersusernameDesktop\\myfile.txt"

Is this behavior documented anywhere? I tried several searches, and only found this SO question that even mentions the behavior.

Note: I am not asking about UNC paths (\servername), the \? prefix, or the " double-quote escape.

Note: I stumbled upon this behavior while working with a batch file. One line in the batch file looked something like this:

"%SOME_PATH%myapp.exe"

After variable expansion, the command looked like:

"C:Program FilesVendorMyApp\myapp.exe"

To my surprise, the batch file executed as desired and did not fail with some kind of "path not found" error.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In most cases, Win32 API functions will accept a wide range of variations in the path name format, including converting a relative path into an absolute path based on the current directory or per-drive current directory, interpreting a single dot as "this directory" and two dots as "the parent directory", converting forward slashes into backslashes, and removing extraneous backslashes and trailing periods.

So something like

c:documents..code.\working.myprogram\
unme.exe..

will wind up interpreted as

c:codeworkingmyprogram
unme.exe

Some of this is documented, some is not. (As Hans points out, documenting this sort of workaround legitimizes doing it wrong.)

Note that this applies to the Win32 API, not necessarily to every application, or even every system component. In particular, the command interpreter has stricter rules when dealing with a long path, and Explorer will not accept the dot or double-dot and typically will not accept forward slashes. Also, the rules may be different for network drives if the server is not running Windows.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...