JSI Tip 1002. Using a temporary drive letter is a batch.

Jerold Schulman

January 17, 1999

1 Min Read
ITPro Today logo in a gray background | ITPro Today


There are times you need to assign a temporary drive letter in a batch file, and delete it when you are done.

Here is fragment of such a script: (Note: %1 in this fragment is a UNC path parameter)

for /f "Tokens=*" %%i in ('net use * %1') do call :what "%%i"if not defined drv goto errorREM Do whatever@echo %drv%net use %drv% /deletegoto end:whatset tst=%1if not "%tst:~8,1%"

":" goto endset drv=%tst:~7,2%:end

Another solution is to use PUSHD and POPD:

for /f "Tokens=*" %%i in ('CD') do set old=%%ipushd %1for /f "Tokens=*" %%i in ('CD') do set drv=%%iif "%old%""%drv%" goto errorREM Do whatever@echo %drv%popd:end


Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like