That title was a mouthful, I know! Here’s the problem/annoyance I wanted to solve: I often need to copy files to a remote share that requires authentication. The remote share is often accessed through a VPN. Mapping the drive in Windows Explorer takes several steps including re-entering your credentials, and you have to unmap it again later, or else once the VPN is disconnected, Windows Explorer will hang while the mapped drive times out. None of these things make the task impossible, but they do make it fiddly and annoying. In response, I have concocted the following batch script:
net use \\server\share password /user:domain\user
for %%F in (%*) do (
robocopy "!dp:~0,-1!" "\\server\share" "%%~nxF"
net use \\server\share /delete
Just in case you aren’t fluent in batch, here’s a quick play-by-play describing what the above code does. The first line just tells Windows to re-evaluate variables every time they are referenced. Otherwise, variables keep the value they had the first time they were evaluated. The next line is what maps the remote share and provides authentication. The for loop iterates through all of the batch file parameters (which will be the files to copy) and copies them to the remote share. Finally, the last line disconnects from the remote share again.
Here’s how to configure the script for your own use:
- Copy the above code into a new text file
- Replace “\\server\share'” with the UNC path to the remote share
- Replace “password” with your password
- Replace “domain\user” with your domain and user
- Save the file with a .bat extension in the same directory where the file(s) you want to copy are
To use it, simply drag the file(s) you want to copy on to the batch file. This will call the batch file, passing all the files you dragged onto it as parameters to be processed by the for loop. I find this to be much quicker and easier than the alternative. If you need this capability in multiple locations, the batch file can be added to your Send To menu, so you can right-click files anywhere and send them to this batch file for quick and convenient copying.