Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for freedriveletter.bat

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

  1. @ECHO OFF
  2. :: FREEDRIVELETTER
  3. :: Small batch file to list all unused drive letters (works ONLY on 2000/XP/2003)
  4. :: by Rob van der Woude
  5. :: from an idea and minor fixes by Jacopo Lazzari
  6. :: http://www.robvanderwoude.com
  7.  
  8. :: Not for Windows 95/98/ME/NT4
  9. IF NOT "%OS%"=="Windows_NT" EXIT
  10. VER | FIND "Windows NT" >NUL && EXIT
  11.  
  12. SETLOCAL ENABLEDELAYEDEXPANSION
  13. SET ALLDRIVES=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
  14. SET BUSYDRV=
  15. SET FREEDRV=
  16.  
  17. :: List drive letters for physical drives
  18. FOR %%A IN (%ALLDRIVES%) DO (
  19. FOR /F %%B IN ('MOUNTVOL %%A: /L ^|FINDSTR /C:"\\"') DO (
  20. SET BUSYDRV=!BUSYDRV!,%%A:
  21. )
  22. )
  23. :: List drive letters for network drives
  24. FOR /F "tokens=2" %%A IN ('NET USE ^|FINDSTR /R /C:" [A-Z]: "') DO SET BUSYDRV=!BUSYDRV!,%%A
  25. :: Remove leading comma
  26. SET BUSYDRV=%BUSYDRV:~1%
  27. :: Remove backslashes
  28. SET BUSYDRV=%BUSYDRV:\=%
  29.  
  30.  
  31. :: Check which drive letters aren't used and list them as free drive letters
  32. FOR %%A IN (%ALLDRIVES%) DO (
  33. 	ECHO.%BUSYDRV% | FIND "%%A:" >NUL
  34. 	IF ERRORLEVEL 1 SET FREEDRV=!FREEDRV!,%%A:
  35. )
  36. :: Remove leading comma
  37. SET FREEDRV=%FREEDRV:~1%
  38.  
  39. :: Display result
  40. SET BUSYDRV
  41. FOR %%A IN (%BUSYDRV%) DO ECHO %%A BUSY
  42. SET FREEDRV
  43. FOR %%A IN (%FREEDRV%) DO ECHO %%A FREE
  44.  
  45. ENDLOCAL
  46.  

page last uploaded: 2017-04-06, 13:33