Reputation: 25
Below mentioned batch file displays Flashdrive model, filesystem and size of PC
@echo off
setlocal
for /f "usebackq skip=1" %%X in (`
wmic logicaldisk where "drivetype='2'" 2^>nul get caption
`) do call :driveusb %%X
pause
::-----------------------------------------
:driveusb
if "%~1"=="" goto :eof
set "freeB=" & set "sizeB="
set "filesys="
set "flashmodel="
for /f "usebackq skip=1 tokens=1,2" %%x in (`
wmic logicaldisk where "name='%1'" get filesystem
`) do if not defined filesys set filesys=%%x
for /f "usebackq skip=1 tokens=1-6" %%a in (`
wmic diskdrive where "mediatype='removable media'" get model
`) do if not defined flashmodel set flashmodel=%%a %%b %%c %%d %%e %%f
for /f "usebackq skip=1 tokens=1,2" %%X in (`
wmic logicaldisk where "name='%1'" get freespace^,size
`) do if not defined freeB (set "freeB=%%X" & set "sizeB=%%Y")
set/a freeMB = %freeB:~0,-6% & set/a sizeMB = %sizeB:~0,-6%
set/a freePCT = (100 * freeMB + sizeMB / 2) / sizeMB
call :mb2gib freeMB freeGiB & call :mb2gib sizeMB sizeGiB
echo %1 %flashmodel%[%filesys%] free %freeGiB% GB of %sizeGiB% GB = %freePCT%%%
exit /b
:mb2gib
@rem double 1000/1024 mb->mib correction
set/a %2 = (125 * ((125 * %1 + 64) / 128) + 64) / 128
@rem 1/1024 mib->gib conversion
set/a %2 = (%2 + 512) / 1024
Exit /b
The problem is that I can't figure out how to loop Flashdrive model if more than 1 Flashdrive is installed in PC, is there any missing config ?
for /f "usebackq skip=1 tokens=1-6" %%a in (`
wmic diskdrive where "mediatype='removable media'" get model
`) do if not defined flashmodel set flashmodel=%%a %%b %%c %%d %%e %%f
Here is the Current output:
F: Kingston DT 101 G2 USB Device[FAT32] free 2 GB of 29 GB = 6%
G: Kingston DT 101 G2 USB Device[NTFS] free 4 GB of 4 GB = 99%
What i am expecting the output to be:
F: SanDisk Ultra USB 3.0 USB Device [FAT32] free 2 GB of 29 GB = 6%
G: Kingston DT 101 G2 USB Device[NTFS] free 4 GB of 4 GB = 99%
Upvotes: 0
Views: 305
Reputation: 79982
Not sure what you mean by "make variable for this command" so assuming you want to establish separate variables in the case of multiple drives,
@echo OFF
setlocal
for %%X in (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) do set "flashdrive%%X="
for /f "usebackq skip=1" %%X in (`
wmic logicaldisk where "drivetype='2'" 2^>nul get caption
`) do call :driveusb %%X
set flashdrive
goto :eof
:: -----------------------------------------
:driveusb
set "drive=%~1"
if not defined drive goto :eof
set "freeB=" & set "sizeB="
set "filesys="
set "flashmodel="
for /f "usebackq skip=1 tokens=1,2" %%x in (`
wmic logicaldisk where "name='%1'" get filesystem
`) do if not defined filesys set filesys=%%x
for /f "usebackq skip=1 tokens=1-6" %%a in (`
wmic diskdrive where "mediatype='removable media'" get model
`) do if not defined flashmodel set flashmodel=%%a %%b %%c %%d %%e %%f
for /f "usebackq skip=1 tokens=1,2" %%X in (`
wmic logicaldisk where "name='%1'" get freespace^,size
`) do if not defined freeB (set "freeB=%%X" & set "sizeB=%%Y")
set/a freeMB = %freeB:~0,-6% & set/a sizeMB = %sizeB:~0,-6%
set/a freePCT = (100 * freeMB + sizeMB / 2) / sizeMB
call :mb2gib freeMB freeGiB & call :mb2gib sizeMB sizeGiB
echo %1 %flashmodel%[%filesys%] free %freeGiB% GB of %sizeGiB% GB = %freePCT%%%
set "flashdrive%drive:~0,1%=%1 %flashmodel%[%filesys%] free %freeGiB% GB of %sizeGiB% GB = %freePCT%%%"
exit /b
:mb2gib
:: double 1000/1024 mb->mib correction
set/a %2 = (125 * ((125 * %1 + 64) / 128) + 64) / 128
:: 1/1024 mib->gib conversion
set/a %2 = (%2 + 512) / 1024
Exit /b
which sets flashdriveX
for each drive X
found.
Notes :
added setlocal
to avoid contaminating environment
replaced pause
by goto :eof
to avoid re-invoking :driveusb
by flow-through
added loop to clear flashdrive
variables
converted @rem
and comment lines to ::
-comments
modified method of detection for :driveusb
missing-parameter
Results displayed on my system:
E: USB DISK 2.0 USB Device [FAT32] free 14 GB of 14 GB = 100%
F: USB DISK 2.0 USB Device [FAT32] free 7 GB of 7 GB = 93%
V: USB DISK 2.0 USB Device [FAT32] free 17 GB of 58 GB = 29%
flashdriveE=E: USB DISK 2.0 USB Device [FAT32] free 14 GB of 14 GB = 100%
flashdriveF=F: USB DISK 2.0 USB Device [FAT32] free 7 GB of 7 GB = 93%
flashdriveV=V: USB DISK 2.0 USB Device [FAT32] free 17 GB of 58 GB = 29%
The first 3 lines are the report from the "Echo" line within the code. The last 3 are from the set flashdrive
command, which lists all environment variables that start flashdrive
.
Upvotes: 1