1

I've used the pushd "%~dp0" trick for years to run batch files from server locations without issue. It's not working anymore, and it's not because CMD extensions are unavailable or I've run out of drive letters (ref1,ref2). Any clue what's going on?

System is Win7-Pro SP1 x64.

3rd party installs which interact with or modify CMD (I'm pretty sure I've used pushd+UNC after installing these, it's a very common technique, but can't swear to it):

.

d:\>pushd \\server4\share
' '
CMD does not support UNC paths as current directories.

.

d:\>reg query "HKCU\Software\Microsoft\Command Processor" /v EnableExtensions

HKEY_CURRENT_USER\Software\Microsoft\Command Processor
    EnableExtensions    REG_DWORD    0x1

d:\>net use
New connections will be remembered.

Status       Local     Remote                    Network
-------------------------------------------------------------------------------
OK           B:        \\server1\dev              Microsoft Windows Network
OK           F:        \\server1\dev              Microsoft Windows Network
OK           G:        \\server2\corp             Microsoft Windows Network
OK           H:        \\server3\home             Microsoft Windows Network
OK           I:        \\server4\share            Microsoft Windows Network
OK           N:        \\server1\dev              Microsoft Windows Network
OK           O:        \\server1\dev              Microsoft Windows Network
OK           P:        \\server5\files            Microsoft Windows Network
OK           Q:        \\server1\dev              Microsoft Windows Network
OK           R:        \\server5\work             Microsoft Windows Network
             S:        \\server6\data             Microsoft Windows Network
OK           T:        \\server5\maps             Microsoft Windows Network
OK           U:        \\server7\layers           Microsoft Windows Network
OK           V:        \\server8\home\me          Microsoft Windows Network
OK           W:        \\server5\warehouse        Microsoft Windows Network
OK           X:        \\server5\work             Microsoft Windows Network
OK           Y:        \\server5\dev_repo         Microsoft Windows Network
OK           Z:        \\server5\repository       Microsoft Windows Network
OK                     \\server5\work             Microsoft Windows Network
                       \\mysites.sharepoint.org\DavWWWRoot  Web Client Network
OK                     \\server4\ITSupport        Microsoft Windows Network
                       \\sharepoint.org@SSL\DavWWWRoot  Web Client Network
                       \\sharepoint.org\DavWWWRoot      Web Client Network

The command completed successfully.
Community
  • 1
  • 1
matt wilkie
  • 17,268
  • 24
  • 80
  • 115
  • Not a solution, but: in case `pushd` fails, I do the following: `pushd \\server4\share || exit /B 1`, so there is no `popd` executed which could lose a path from another `pushd`... – aschipfl Jan 30 '17 at 22:05
  • Why not just use the UNC path directly in your batch file. I am hoping some day MS will get rid of drive letters and just use mount points. – Squashman Jan 31 '17 at 05:42

2 Answers2

1

Figured it out! I was out of drive letters, but not from mapped or local drives. I have a Dell display with integrated USB hub, and it pre-allocates drive letters J: to L: even if no device is plugged in. I use them so rarely I'd forgotten all about that.

What led me to the discovery was deleting all non-essential mapped drives and repeatedly pushd \\server\share until the error occurred, noticing it always jumped some drive letters, and then having a look at Disk Management.

D:\>pushd \\server\share

Q:\>pushd \\server\share

O:\>pushd \\server\share

N:\>pushd \\server\share

F:\>pushd \\server\share

B:\>pushd \\server\share
' '
CMD does not support UNC paths as current directories.

Disk Management showing pre-allocated USB drive letters

matt wilkie
  • 17,268
  • 24
  • 80
  • 115
  • 1
    Unfortunately, `net use` does not support characters other than letters as drive _letters_ (opposed to `subst`, which allows almost every character)... – aschipfl Jan 31 '17 at 12:54
  • Whoa @aschipfl that is cool. I didn't know subst could use non-letter characters. (A hint of the [Amiga `assign`](http://wiki.amigaos.net/wiki/AmigaOS_Manual:_AmigaDOS_Command_Reference#ASSIGN) which could be any length of characters. I miss it every day.) Thank you! – matt wilkie Jan 31 '17 at 17:24
  • 1
    `subst` even seems to work together with UNC paths (unexpectedly); anyway, do you really need all the drives simultaneously? what about using a loop that maps one or some drives and destroys it/then when the iteration is done? To securely find a free drive letter you could use `pushd "\\%ComputerName%\ADMIN$"` and capture its output by `for /F`, for example... – aschipfl Jan 31 '17 at 17:35
  • ...Uhm, `pushd` does not produce any output to capture, but there are ways to get the current drive; for instance, to read variable `%CD%` and split off the drive part... – aschipfl Jan 31 '17 at 17:47
  • 1
    http://ss64.com/nt/subst.html - reference doc that includes this undocumented behaviour plus caution re: not all defined drives are listed after the fact (up to you to remember they were created). Plus severe performance warning on unavailable network resources. @aschipfl – matt wilkie Jan 31 '17 at 18:20
1

I can see some multiple temporary drive mappings of the same UNC path in net use output, for instance \\server1\dev:

OK           B:        \\server1\dev              Microsoft Windows Network
OK           F:        \\server1\dev              Microsoft Windows Network
OK           N:        \\server1\dev              Microsoft Windows Network
OK           O:        \\server1\dev              Microsoft Windows Network
OK           Q:        \\server1\dev              Microsoft Windows Network

Note that popd is used along with pushd in the linked pushd "%~dp0" trick:

@echo off

    :: Create a temporary drive letter mapped to your UNC root location
    ::                          and effectively CD to that location
pushd "%~dp0"

    :: Do your work
       :: launch executables directly
something.exe parameters
       :: aternatively, launch executables using START
start "" something.exe parameters

       :: IMPORTANT! launch batch files using CALL command
       ::            to return from a called script to the calling one
CALL something.BAT parameters
CALL something.CMD parameters

    :: Remove the temporary drive letter and return to your original location
POPD
Community
  • 1
  • 1
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • Yeah popd is important to release the drive letter. I was troubleshooting a script which was exiting prematurely and never getting to popd. Looking at net use output I thought I still had 4 or more letters to go and was postponing cleanup until I "had to". (Grandma always did say lazy hands work twice...) – matt wilkie Jan 30 '17 at 22:00