5
2
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
1Interesting 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 – 2009-10-23T01:24:18.657
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 – 2009-10-23T14:51:33.527