CMD does not support UNC paths as current directories

27,089

Solution 1

The Windows File Explorer explorer.exe starts cmd.exe with implicit usage of the option /C to close command process after finishing batch file execution and with setting the batch file directory as current directory on double clicking a batch file.

If the double clicked batch file is on a network share and a UNC path is used instead of mapping the network share to a drive letter and use the network drive, cmd.exe informs the user that the UNC path as current directory is not supported and sets %SystemRoot% (Windows directory) as current directory.

The warning can be ignored if the batch file is designed for being executed from any directory which means it does not require that the directory of the batch file is the current directory. Good coded batch files, especially those being executed by Windows task scheduler, can be executed with any directory being the current directory.

See also: How can I disable the Universal Naming Convention (UNC) check for command sessions?

It is possible to configure with a registry value that the Windows command processor cmd.exe accepts a UNC path for the current directory by running in a command prompt window:

%SystemRoot%\System32\reg.exe ADD "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v DisableUNCCheck /t REG_DWORD /d 1 /f

Please note that when you enable support for running batch files with UNC path as current directory, some console applications could fail running because of not being designed for that environment.

The task scheduler sets %SystemRoot%\System32 (Windows system directory) as current directory before executing the command on running a batch file as scheduled task, except a specific directory path is explicitly set as start in directory in properties of scheduled task.

In your case with the batch file designed for execution as scheduled task and taking therefore all aspects into account as listed by me in answer on the question What must be taken into account on executing a batch file as scheduled task?, you can ignore this warning message on double clicking the batch file using UNC path.

Of course you can also create a shortcut file (*.lnk) for the batch file for starting the batch file via this shortcut with Start in in properties of shortcut file is set to %SystemRoot%\System32 as Windows task scheduler does by default.

Solution 2

I have some advice - consider rewriting your script in PowerShell. PowerShell allows the current working directory to be on a network. PowerShell as a whole is more versatile and powerful than CMD is. It's also cross-platform. Consider switching.

Share:
27,089
Nelson
Author by

Nelson

Updated on June 14, 2021

Comments

  • Nelson
    Nelson almost 3 years

    We have batch scripts that seem to run fine however I am editing one of them to include some new work and when manually running the script (by double-clicking from windows explorer) I get the above error message each time a pushd command in the script is encountered. These are pushd commands that have been there since the year dot and the scripts are running fine. Is there a difference between the batch script running manually and when started by a scheduler? How can I disable the error message when running manually (if this is possible)?