I have a specific case here. I need to pull in network files to copy locally, and I wanted to use a specific drive letter. But what if that drive is already in use? What if all of my choices are in use?
My solution looks for an existing drive letter, skips it if it is in use, and if the drive is not in use, goes to the net use command to make the drive letter available for the rest of the longer script. I know it might not be elegant but it does the job. If all the letters specified are taken, the program exits.
cls
@echo off
cls
echo.
echo.
:TestW
IF EXIST w:\ (goto TestX) ELSE (goto close1)
:TestX
IF EXIST x:\ (goto TestY) ELSE (goto close2)
:TestY
IF EXIST Y:\ (goto failed) ELSE (goto close3)
:close1
echo.
net use w: \\wshd\hd\Office\OfficeUninstall\SARAOfficeRemoval\
echo Drive W: has been created.
goto End
:close2
echo.
net use X: \\wshd\hd\Office\OfficeUninstall\SARAOfficeRemoval\
echo Drive X: has been created.
Goto End
:close3
echo.
net use Y: \\wshd\hd\Office\OfficeUninstall\SARAOfficeRemoval\
echo Drive Y: has been created.
Goto End
:failed
cls
echo Drives W, X, and Y are all unavailable. End Program.
Exit
:End
echo Closing program....
pause