For years, I have accepted that the Windows command prompt doesn't support UNC paths as the current directory. However, a few weeks ago, two of my colleagues reported that they could run batch files from a UNC directory.
I have have not been able to figure out what's special with these two machines. Can anyone explain this, and if possible, tell me how to setup my own command prompt to accept UNC paths?
To show you the difference, we have created a small batch file, saved it in "\rpfil01\Projects\test" and executed it from the same UNC path.
Batch file contents:
ver
dir randomFolder
cd randomFolder
pauseThe results are attached as UNC supported.jpg and UNC not supported.png. As you can see from "UNC not supported.png", my machine simply defaults to "C:\Windows", but on my colleague's machine ("UNC supported.jpg") the UNC path is used as the current directory.
However, when a command prompt is using a UNC path as the current directory, "cd" cannot be used to go up or down in the directory tree. This can be seen from "UNC supported.jpg", where the dir command is able to list the contents of "randomFolder" but the cd command is unable to change the current directory to "randomFolder".
As you can see from the attached images, our Windows versions differ (even if both machines are up to date according to Windows Update). Can this behavior be some kind of bug in the older Windows version? If so, a very convenient bug. ;-)
51 Answer
I have finally found the solution myself. It is not the different Windows 10 versions, it is a registry DWORD value called "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\DisableUNCCheck". If this value equals 1, the command prompt accepts UNC paths as the current directory. If this value equals 0 (or not existing), UNC paths are not accepted by the command prompt.
When using a UNC path as current directory, there are limitations to the command prompt:
- Many of the standard commands, like cd (change directory) and del (delete), doesn't work.
- You cannot use the tab-key to automatically complete a half-written command, based on the contents of the current directory.
- Plus probably a lot more that I haven't found yet.
However, programs started from the command prompt inherit the UNC directory as their current directory. Therefore, many batch files will work with this registry flag enabled, but not all. For those who don't work you still need to map a drive letter or use pushd and popd.
Follow-up questions:
- Do you recommend setting the DisableUNCCheck flag or not?
- What are the potential problems when disabling the UNC check? Security problems or something else?
After all, Microsoft has decided to ship Windows without this flag.
2