Author Topic: OT: Windows XP Batch file to set USB Drive Letter  (Read 8680 times)

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7016
    • My Best Post
OT: Windows XP Batch file to set USB Drive Letter
« on: August 11, 2009, 10:18:48 pm »
I am experimenting with using Code::Blocks on an USB Drive and decided it is easier to always use the same Drive Letter for some Compilers.

Here's the batch file I use to set the USB Drive to "G:"
I copy the batch file to the root folder on the USB Drive and run it manually when needed.
It is only needed once per computer in my experience.

Tim S.

MntAsG.bat
Code: [Select]
@ECHO OFF
REM
REM Change Current USB Drive to be on TARGETDRV Drive Letter.
REM

REM Change to root of Current Drive
CD \

REM Check the current drive is removable.
FOR /F "delims=" %%x IN ('fsutil fsinfo drivetype %CD% ^| FIND /i "removable"') DO SET DF=%%x
IF "%DF%"=="" GOTO ERROR1

echo mountvol %CD% /D > %TEMP%\mountdrv.bat
mountvol %CD% /L >%TEMP%\mountdrv.tmp


setlocal enabledelayedexpansion
SET TARGETDRV=G:
REM Check the TARGETDRV is NOT being used.
FOR /F "delims=" %%x IN ('MOUNTVOL %TARGETDRV% /L') DO SET DF=%%x
IF NOT "%DF%"=="The system cannot find the file specified." GOTO ERROR2

set LINE=mountvol %TARGETDRV%
for /f %%a in (%TEMP%\mountdrv.tmp) do (
set LINE=!LINE!%%a
)
echo %LINE% >> %TEMP%\mountdrv.bat
del /q %TEMP%\mountdrv.tmp
endlocal

REM echo PAUSE >> %TEMP%\mountdrv.bat

type %TEMP%\mountdrv.bat
REM PAUSE
cd /d "%HOMEDRIVE%%HOMEPATH%"
%TEMP%\mountdrv.bat
goto eof

:ERROR1
ECHO Current Drive is not removable.
endlocal
goto eof

:ERROR2
ECHO %TARGETDRV% is already in use.
endlocal
goto eof

:eof
PAUSE
« Last Edit: August 12, 2009, 02:45:57 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5277
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: OT: Windows XP Batch file to set USB Drive Letter
« Reply #1 on: August 12, 2009, 04:52:11 pm »
Thanks for sharing!!

In my Windows XP system, the Driver G is already used for a DVD drive. So, this batch file will always give a error message:

Quote
Current Drive is not removable.
Press any key to continue . . .
:D
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7016
    • My Best Post
Re: OT: Windows XP Batch file to set USB Drive Letter
« Reply #2 on: August 12, 2009, 08:02:50 pm »
Thanks for sharing!!

In my Windows XP system, the Driver G is already used for a DVD drive. So, this batch file will always give a error message:

Quote
Current Drive is not removable.
Press any key to continue . . .
:D

Just set TARGETDRV to the drive you want to use.
Code: [Select]
SET TARGETDRV=G:
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org