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?

Borealid
  • 95,191
  • 9
  • 106
  • 122
Stew
  • 1,081
  • 2
  • 8
  • 4
  • 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 – ToolmakerSteve Mar 20 '14 at 22:28
  • seriously this is a ServerFault.com question – Junchen Liu Nov 18 '15 at 10:49

13 Answers13

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • 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 '12 at 05: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. – Grant Peters Jun 24 '13 at 05:27
  • will the cmd be executed on the remote computer or local? – dingx Nov 22 '13 at 08: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](http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx). – dbenham Nov 22 '13 at 12:14
  • This works great except when the command is killed with ctrl+C. when the command is killed, batch script is terminated without calling popd. Is it possible to always run popd when the script is complete or killed before? – balki Apr 17 '14 at 01:35
  • 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 '14 at 03:52
  • I had a similar issue, and pushd/popd really did the trick beautifully. – zeycus Jul 04 '18 at 08:26
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.

Grallen
  • 1,620
  • 1
  • 17
  • 16
  • 4
    I needed the `popd` at the end (Windows 7), otherwise the mapped drives stuck around after the window was closed. – Patrick Jun 07 '16 at 19:49
  • 2
    Works just fine. Thank you. – Jonas_Hess Jul 25 '16 at 07:56
  • 1
    If should work on Windows 10. It's still the "Command Prompt". – Grallen Jul 22 '19 at 19:04
  • 1
    Worked like a charm for me. – Nikos Oct 22 '21 at 08:12
  • 1
    FYI: You may need to add a [SLEEP/TIMEOUT](https://stackoverflow.com/a/48000823/1762224) 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. – Mr. Polywhirl Mar 20 '23 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

Vinzz
  • 3,968
  • 6
  • 36
  • 51
  • 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 '18 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 '18 at 06:24
  • I'll update the answer then; thanks for the precision – Vinzz Jan 09 '19 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.

aphoria
  • 19,796
  • 7
  • 64
  • 73
  • 1
    Thank you. I will try this as the last option, if there is no other solution. – Stew Jan 27 '12 at 05: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 '12 at 13:31
  • 1
    @Grallen answer is perfect and dynamic as it adapts to your scripts path automatically. – jacktrader Aug 13 '21 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JayRO-GreyBeard
  • 149
  • 2
  • 7
  • @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. – JayRO-GreyBeard Sep 29 '16 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.

jameslr
  • 51
  • 1
  • 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 '12 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 '12 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.

Kai
  • 31
  • 1
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.

James
  • 330
  • 1
  • 12
2

This is the RegKey I used:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor]
"DisableUNCCheck"=dword:00000001
FreeSoftwareServers
  • 2,271
  • 1
  • 33
  • 57
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

Zhui Wang
  • 19
  • 2
1

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

Dom
  • 580
  • 4
  • 26
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.

Ed999
  • 2,801
  • 2
  • 16
  • 19
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
Stefan
  • 70
  • 2
  • 5