108

I am trying to run a batch file from a network share, but I keep getting the following message: "UNC path are not supported. Defaulting to Windows directory." The batch file is located on \\Server\Soft\WPX5\install.bat. While logged in as administrator, from my Windows 7 Desktop, I navigate to \\Server\Soft\WP15\ and double click on install.bat, that's when I get the "UNC path are not supported." message. I found some suggestions online stating that mapping drive will not work, but using a symbolic link will solve this issue, but the symbolic link didn't work for me. Below is my batch file content, I would appreciate any assistance that can help me accomplish what I am trying to do. Basically, I want to be able to run the batch file from \\Server\Soft\WP15\install.bat.

Batch file content

mklink /d %userprofile%\Desktop\WP15 \\server\soft\WP15
\\server\soft\WP15\setup.exe
robocopy.exe "\\server\soft\WP15\Custom" /copyall "C:\Program Files (x86)\WP\Custom Templates"
Regedit.exe /s \\server\soft\WPX5\Custom\Migrate.reg

Also, how do I remove the symbolic link after the install is completed?

2
  • 4
    Years later, but maybe useful to someone else: After creating the symbolic link, why do you continue referring to the original path? The point of the symbolic link is to give you a local path you can use instead. E.g. don't say "\\server\soft\WP15\setup.exe", say "%userprofile%\Desktop\WP15\setup.exe Mar 20, 2014 at 22:28
  • seriously this is a ServerFault.com question Nov 18, 2015 at 10:49

13 Answers 13

155

PUSHD and POPD should help in your case.

@echo off
:: Create a temporary drive letter mapped to your UNC root location
:: and effectively CD to that location
pushd \\server\soft

:: Do your work
WP15\setup.exe
robocopy.exe "WP15\Custom" /copyall "C:\Program Files (x86)\WP\Custom Templates"
Regedit.exe /s WPX5\Custom\Migrate.reg

:: Remove the temporary drive letter and return to your original location
popd

Type PUSHD /? from the command line for more information.

7
  • 2
    Thank you for the quick responses and suggestions. I was hoping your suggestion would work, but unfortunately it did not work for me. I got the same message: "...UNC paths are not supported. Defaulting to Windows directory." I also got the User Account Control dialogbox to click Yes or No, even though I ran the file while logged in as a member of the administrator group.
    – Stew
    Jan 27, 2012 at 5:21
  • 29
    @Stew At the start of your .bat file, put: pushd %~dp0 That should get it to change the directory to the one containing your bat file. It will still print out the warning, but it should then work as per usual. If the bat file uses '%~dp0' elsewhere, it may have issues with that, in that case, you can detect if %~dp0 starts with '//' at the start of the bat file, and if so, pushd, and then run the bat file again, then goto :EOF. This will cause it to run in the newly mapped dir. Jun 24, 2013 at 5:27
  • will the cmd be executed on the remote computer or local?
    – dingx
    Nov 22, 2013 at 8:30
  • @DingxinXu - The remote code will run locally. If you want to run the code on the remote computer, then you need something like PsExec.
    – dbenham
    Nov 22, 2013 at 12:14
  • 3
    @balki - Yes, put the remote commands in another script. After PUSHD, execute the 2nd script via CMD /C, and follow that with POPD. Control will return to the parent script after the CMD /C script exits, even if it was terminated by CTRL-C.
    – dbenham
    Apr 17, 2014 at 3:52
42

I feel cls is the best answer. It hides the UNC message before anyone can see it. I combined it with a @pushd %~dp0 right after so that it would seem like opening the script and map the location in one step, thus preventing further UNC issues.

cls
@pushd %~dp0
:::::::::::::::::::
:: your script code here
:::::::::::::::::::
@popd

Notes:

pushd will change your working directory to the scripts location in the new mapped drive.

popd at the end, to clean up the mapped drive.

5
  • 5
    I needed the popd at the end (Windows 7), otherwise the mapped drives stuck around after the window was closed.
    – Patrick
    Jun 7, 2016 at 19:49
  • 3
    Works just fine. Thank you.
    – Jonas_Hess
    Jul 25, 2016 at 7:56
  • 2
    If should work on Windows 10. It's still the "Command Prompt".
    – Grallen
    Jul 22, 2019 at 19:04
  • 2
    Worked like a charm for me.
    – Nikos
    Oct 22, 2021 at 8:12
  • 1
    FYI: You may need to add a SLEEP/TIMEOUT before calling @popd when executing a task via that START command. My script in this instance was a wrapper script that would call START javaw -jar app.jar. Since the application would not launch before the drive was unmapped, the application ceased. Mar 20 at 18:20
33

There's a registry setting to avoid this security check (use it at your own risks, though):

Under the registry path

   HKEY_CURRENT_USER
     \Software
       \Microsoft
         \Command Processor

add the value DisableUNCCheck REG_DWORD and set the value to 0 x 1 (Hex).

Note: On Windows 10 version 1803, the setting seems to be located under HKLM: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor

3
  • 2
    Not sure why but I found my "Command Processor" folder under the following path: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
    – aoh
    May 29, 2018 at 16:25
  • Thanks, it's helpful. But on Windows 10 1803, it's HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor, as @aoh pointed out.
    – Edwin Yip
    Dec 25, 2018 at 6:24
  • I'll update the answer then; thanks for the precision
    – Vinzz
    Jan 9, 2019 at 14:53
11

Basically, you can't run it from a UNC path without seeing that message.

What I usually do is just put a CLS at the top of the script so I don't have to see that message. Then, specify the full path to files in the network share that you need to use.

3
  • 1
    Thank you. I will try this as the last option, if there is no other solution.
    – Stew
    Jan 27, 2012 at 5:22
  • @jameslr answer will work, but it is more work (typing) than I'm usually willing to do. So, just clearing the screen to get rid of the error is good enough for me. :)
    – aphoria
    Jan 27, 2012 at 13:31
  • 1
    @Grallen answer is perfect and dynamic as it adapts to your scripts path automatically.
    – jacktrader
    Aug 13, 2021 at 21:55
8

I needed to be able to just Windows Explorer browse through the server share, then double-click launch the batch file. @dbenham led me to an easier solution for my scenario (without the popd worries):

:: Capture UNC or mapped-drive path script was launched from
set NetPath=%~dp0

:: Assumes that setup.exe is in the same UNC path
%NetPath%setup.exe

:: Note that NetPath has a trailing backslash ("\")
robocopy.exe "%NetPath%Custom" /copyall "C:\Program Files (x86)\WP\Custom Templates"
Regedit.exe /s %NetPath%..\WPX5\Custom\Migrate.reg

:: I am not sure if WPX5 was typo, so use ".." for parent directory
set NetPath=
pause
1
  • @peter-mortensen, edited the comments in the script 4x to capitalize the first word? Worthy of an edit? Why not put a period at the end to make it a sentence? How about: because it's a just a comment, I purposely do NOT use upper-case in the first word in comments EXCEPT in appreviations and variables (or emphasis) for clearer understanding. Sep 29, 2016 at 11:48
4

Instead of launching the batch directly from explorer - create a shortcut to the batch and set the starting directory in the properties of the shortcut to a local path like %TEMP% or something.

To delete the symbolic link, use the rmdir command.

2
  • 1
    This will work, but only if you want to launch from a shortcut. It will not help if you want to run from a command line or call from another batch file.
    – aphoria
    Jan 26, 2012 at 14:05
  • 2
    If you want to call from another batch or command line you can call like this: start /d %TEMP% \\Server\Soft\WP15\install.bat
    – jameslr
    Jan 26, 2012 at 14:52
3

I ran into the same issue recently working with a batch file on a network share drive in Windows 7.

Another way that worked for me was to map the server to a drive through Windows Explorer: Tools -> Map network drive. Give it a drive letter and folder path to \yourserver. Since I work with the network share often mapping to it makes it more convenient, and it resolved the “UNC path are not supported” error.

2

My situation is just a little different. I'm running a batch file on startup to distribute the latest version of internal business applications.

In this situation I'm using the Windows Registry Run Key with the following string

cmd /c copy \\serverName\SharedFolder\startup7.bat %USERPROFILE% & %USERPROFILE%\startup7.bat

This runs two commands on startup in the correct sequence. First copying the batch file locally to a directory the user has permission to. Then executing the same batch file. I can create a local directory c:\InternalApps and copy all of the files from the network.

This is probably too late to solve the original poster's question but it may help someone else.

2

This is the RegKey I used:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor]
"DisableUNCCheck"=dword:00000001
2

Editing Windows registries is not worth it and not safe, use Map network drive and load the network share as if it's loaded from one of your local drives.

enter image description here

3
  • 1
    How is this different from Kai's answer? Nov 8, 2019 at 1:35
  • 1
    I didn't notice it tbh, maybe screenshots are worth a thousand words that make a difference?
    – Dom
    Nov 11, 2019 at 11:01
  • Agreed. This answer popped out more to me than Kai's answer. It was also more clear and concise. Apr 20 at 9:04
1

My env windows10 2019 lts version and I add this two binray data ,fix this error

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor DisableUNCCheck value 1 Computer\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Command Processor DisableUNCCheck value 1

1
  • Thanks you - on 2016 this also seems to have solved it - the second value was also needed
    – whytheq
    Feb 9, 2021 at 18:30
1

This is a very old thread, but I still use Windows 7. :-)

There is one point that no one seems to have taken into account, which probably would help Windows 10 users also.

If Command Extensions are enabled, the PUSHD command accepts network paths in addition to the normal drive letter and path.

So the obvious - and simplest - answer might be to enable command extensions in the batch script, if you intend to use PUSHD. At the very least, this ought to reduce the problems you might have in using PUSHD wqith a network path.

0

I stumbled upon this question while searching for a solution to a specific problem. I needed to make a batch script that sits in a network folder (UNC path) with a Python script. The goal was to be able to double click on the batch script and have it run the Python script:

  • with the network folder containing the script as the working directory,
  • without modifications to the Python script (no command line parameters or hard-coded paths).
  • without creating another Python file.

The pushd and popd solutions were unsatisfactory. They work, but if the user were to get in the habit of forcefully terminating the script while it was running, they would end up with a bunch of mapped drives in My Computer since popd wasn't run.

I start by using cls to clear the UNC path error. I then assign the path containing the batch script to a variable. I slice the path to remove the trailing backslash (otherwise, Python throws a SyntaxError). Finally, I run a couple Python commands inside the batch file that change the working directory and execute the target script:

cls
@echo off

set pyfile=myscript.py
set batchdir=%~dp0
set wdir=%batchdir:~0,-1%

python -c "import os; import runpy; os.chdir(r""%wdir%""); runpy.run_path(r""%pyfile%"")"

pause

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.