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

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 error
REM Do whatever
@echo %drv%
net use %drv% /delete
goto end
set tst=%1
if not "%tst:~8,1%"

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

Another solution is to use PUSHD and POPD:

for /f "Tokens=*" %%i in ('CD') do set old=%%i
pushd %1
for /f "Tokens=*" %%i in ('CD') do set drv=%%i
if "%old%""%drv%" goto error
REM Do whatever
@echo %drv%

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.