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%
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,
which sets
flashdriveX
for each driveX
found.Notes :
added
setlocal
to avoid contaminating environmentreplaced
pause
bygoto :eof
to avoid re-invoking:driveusb
by flow-throughadded loop to clear
flashdrive
variablesconverted
@rem
and comment lines to::
-commentsmodified method of detection for
:driveusb
missing-parameterResults displayed on my system:
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 startflashdrive
.