Powered by GeSHi

Source code for getlastdrive.bat

(view source code of getlastdrive.bat as plain text)

  1. @ECHO OFF
  2. :: Check Windows version and command line arguments (none required)
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4. IF NOT  "%~1"==""           GOTO Syntax
  5.  
  6. :: Remove drive letters no longer in use
  7. MOUNTVOL /R
  8.  
  9. SETLOCAL ENABLEDELAYEDEXPANSION
  10. :: Read the list of physical drives from the registry, and save only the last one
  11. FOR /F "tokens=2 delims=\ " %%A IN ('REG Query "HKLM\SYSTEM\MountedDevices" /v "\DosDevices\*" ^| FIND "\DosDevices\"') DO (
  12. 	IF %%A GTR !LastDrive! SET LastDrive=%%A
  13. )
  14. :: Read the list of mapped network drives, and save only the last one
  15. FOR /F "tokens=2" %%A IN ('NET USE ^| FINDSTR /R /C:" [A-Z]: "') DO (
  16. 	IF %%A GTR !LastDrive! SET LastDrive=%%A
  17. )
  18. :: Read the list of SUBSTituted drives, and save only the last one
  19. FOR /F "delims=:" %%A IN ('SUBST ^| FINDSTR /R /B /C:"[A-Z]:\\: "') DO (
  20. 	IF %%A GTR !LastDrive! SET LastDrive=%%A:
  21. )
  22. ECHO %LastDrive%
  23. ENDLOCAL & SET LastDrive=%LastDrive%
  24. GOTO:EOF
  25.  
  26. :Syntax
  27. ECHO.
  28. ECHO GetLastDrive.bat,  Version 1.00 for Windows 7 and later
  29. ECHO Return the last ("highest") drive letter used
  30. ECHO.
  31. ECHO Usage:   GETLASTDRIVE
  32. ECHO.
  33. ECHO Written by Rob van der Woude
  34. ECHO http://www.robvanderwoude.com
  35.