7

I'm creating a simple command-line backup script to use robocopy to mirror "important" stuff to my NAS box. I would prefer not to rely on a hard-coded mapped drive letter for that NAS box. In the script I would like to map a temporary drive letter, copy the relevant files, and dispose of the mapped drive. I don't want to assume that if that drive letter is already mapped, that it's pointing to the place I expect. I also don't want to "assume" that some drive letter is available when I map the drive, thus the leading '*'.

    net use * \\nasbox\sharename password /user:username /persistent:no

What's the proper way within my .bat script to extract that drive letter for use in subsequent commands? I was thinking that I could piece together some variant of "for"'s token-parsing, but I also suspect there must be an easier way than esoteric for-syntax.

    C:\>net use * \\nasbox\sharename password /user:username /persistent:no
    Drive Z: is now connected to \\nasbox\sharename.

    The command completed successfully.


    C:\>

Once I have the mapped drive letter, I would like to follow up with commands along the lines of:

    robocopy C:\path\to\important\stuff %NewDrive%\path\to\backup /MIR /Z
2
  • 1
    Interesting question in the general case, and a good accepted answer, but wouldn't it be better tojust use the UNC name in the Robocopy command? robocopy C:\path\to\important\stuff \\nasbox\sharename\path\to\backup /MIR /Z
    – Neal
    Oct 23, 2009 at 1:24
  • Hmm, now that's an interesting proposition, and one I should have considered first. Since roboycopy supports UNC paths (just tested it), then yes, I'll switch to that form instead. Having said that, I suspect I'll continue to find use for the pushd/popd pattern, regardless. +1
    – JMD
    Oct 23, 2009 at 14:51

5 Answers 5

6

Use the commands pushd and popd.

pushd \\server\share will create a temporary drive (starting from Z: and going backwards till it finds an available letter) and go into it. When you're done, popd will delete the temporary drive and get you back where you were.

C:\Users\Snark>pushd /?
Stores the current directory for use by the POPD command, then
changes to the specified directory.

PUSHD [path | ..]

  path        Specifies the directory to make the current directory.

If Command Extensions are enabled the PUSHD command accepts
network paths in addition to the normal drive letter and path.
If a network path is specified, PUSHD will create a temporary
drive letter that points to that specified network resource and
then change the current drive and directory, using the newly
defined drive letter.  Temporary drive letters are allocated from
Z: on down, using the first unused drive letter found.

C:\Users\Snark>popd /?
Changes to the directory stored by the PUSHD command.

POPD


If Command Extensions are enabled the POPD command will delete
any temporary drive letter created by PUSHD when you POPD that
drive off the pushed directory stack.
2
  • While this is all great it does not really answer "How can I extract the drive letter for the newly mapped drive" - even the letter map created but pushd Jan 24 at 13:01
  • PUSHD will switch to the newly assigned letter. Just store the current directory with set "drive=%cd%" and you can use it with %drive%
    – Snark
    Jan 25 at 14:15
1

And, for posterity, here's a rough template of how that all works out. Thanks again to @Snark, and @Neal.

Here's my original template, using pushd/popd.

    @echo off

    :: Supply credentials for the NAS share.
    net use \\nasbox\sharename password /user:username /persistent:no
    :: Make sure it worked.
    if ERRORLEVEL 1 echo NET USE returned %ERRORLEVEL% && goto :EOF

    :: Use pushd to supply a drive letter for the share.
    pushd \\nasbox\sharename

    :: Copy the files to be backed up. Rinse and repeat.
    robocopy C:\path\to\important\stuff .\path\to\backups /MIR /Z

    :: Release the temporary drive letter.
    popd

    :: Optionally, revoke the credentials while the share's not in use.
    net use \\nasbox\sharename /delete

Note, that with my revelation from @Neal's comment, my specific application of pushd/popd are unnecessary here, and the form of the robocopy command simply changes to:

    robocopy C:\path\to\important\stuff \\nasbox\sharename\path\to\backups /MIR /Z

No need to vote this up. Just wanted to have it here as a bread crumb for others who find this later.

2
  • +1 Because this worked for me - ta. Oct 23, 2009 at 11:49
  • While this is all great it does not really answer "How can I extract the drive letter for the newly mapped drive" - even the letter map created but pushd Jan 24 at 13:01
0

Try this batch script:

@ECHO OFF

setlocal EnableDelayedExpansion
SET CYBID=
> message1.vbs ECHO WScript.Echo InputBox( "Enter your Elevated Domain ID", "CyberArk ID", "USERID" )
FOR /F "tokens=*" %%A IN ('CSCRIPT.EXE //NoLogo message1.vbs') DO (
 SET CYBID=%%A
)

setlocal DisableDelayedExpansion
SET CYBPW=
> message2.vbs ECHO WScript.Echo InputBox( "Enter your Password. The last password you used is displayed.  If it is more than a day old, you will need a new value from CyberArk", "CyberArk Pw", "PASSWORD" )
FOR /F "tokens=*" %%B IN ('CSCRIPT.EXE //NoLogo message2.vbs') DO (
 SET "CYBPW=%%B"
)

SET MPATH=
> message3.vbs ECHO WScript.Echo InputBox( "Enter your \\server\share", "Enter Share", "\\" )
FOR /F "tokens=*" %%C IN ('CSCRIPT.EXE //NoLogo message3.vbs') DO ( 
 SET MPATH=%%C
)

setlocal EnableDelayedExpansion

net use * !MPATH! !CYBPW! /user:!CYBID! > OUTPUT.txt

FOR /F "tokens=2 eol=T" %%D IN (OUTPUT.txt) DO (
  SET MDRIVE=%%D
)

explorer !MDRIVE!

:END
DEL message*.vbs
DEL OUTPUT.txt
0

Here we could for to parse the drive letter from net use:

@echo off
for /f "tokens=2 delims= " %%a in ('net use * \\server\share ^| find "is now connected"') do set "drive=%%a"

It will set %drive% to the new drive letter.

0

Using this is a .cmd file

rem path:
echo %CD%
rem drive with slash
echo %CD:~0,3%
rem drive with colon only
echo %CD:~0,2%

CD is the current drive letter and path. %CD:~0,3% will be the first three letters (drive letter with slash) like N:\, %CD:~0,2% will be just the drive letter and colon like: N:

So if you for example used this in a cmd file you would get the drive mapped from a UNC path

pushd \\myunc\sharename\folder\folder\folder
rem path with mapped letter Example Y:\folder\folder\folder
echo %CD%
rem drive with slash
echo %CD:~0,3%
rem drive with colon only
echo %CD:~0,2%
popd

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .