Fun stuff with \?


This comes up in an internal discussion about MAX_PATH limit. People do mention that the core file services supports syntax like \\?\ for path longer than MAX_PATH. The caveat is that file name with \\?\ is not canonicalized by the OS. This can lead to many funny behavior. One of our senior developer writes the following example to illustrate this problem.


C:\temp>md “\\?\c:\temp\bar.”


C:\Temp>dir *bar*
 Volume in drive C has no label.
 Volume Serial Number is B0F1-CCDC


 Directory of C:\Temp


02/18/2004  04:06 AM    <DIR>          bar.
               0 File(s)              0 bytes
               1 Dir(s)   6,810,882,048 bytes free


C:\Temp>rd bar.
The system cannot find the file specified.


c:\Temp\md bar


C:\Temp>dir *bar*
 Volume in drive C has no label.
 Volume Serial Number is B0F1-CCDC


 Directory of C:\Temp


02/18/2004  04:08 AM    <DIR>          bar
02/18/2004  04:06 AM    <DIR>          bar.
               0 File(s)              0 bytes
               2 Dir(s)   6,810,882,048 bytes free


C:\Temp>echo foo > bar.\12346.txt


C:\Temp>dir/s/b 12346.txt
C:\Temp\bar\12346.txt
C:\Temp\bar.\12346.txt


C:\Temp>del /s 12346.txt
Deleted file – C:\Temp\bar\12346.txt


C:\Temp>echo foo > “\\?\c:\Temp\bar.\12347.txt


C:\Temp>dir/s/b 12347.txt
File Not Found


C:\Temp>type “\\?\c:\Temp\bar.\12347.txt
foo


 

Comments (4)

  1. Mo says:

    I don’t know if this should be considered a bug or not, really, but can’t change to a directory created this way using cmd.exe – it believes, thanks to the \, that it’s a UNC path, and so can’t be the CWD.

    Probably a good thing, really, but an interesting side-effect nonetheless.

  2. .. says:

    yes because side effects are good in software.

  3. Mo says:

    As a general rule, no.

    However, given that cmd.exe obviously doesn’t have a clue how to handle such paths, it’s probably a good thing that it has as little to do with them as possible.

  4. Pavel Lebedinsky says:

    This has nothing to do with UNC paths. \? prefix allows you to bypass win32 naming rules (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/naming_a_file.asp).

    This is how you can create names such as "bar." (which is not a valid win32 directory name).