TransWikia.com

How to Set WMIC in for loop Flashdrive get model, file system and size in gigabyte?

Super User Asked by sopandi_akbar on December 7, 2020

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%

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP