Search code examples
windowsbatch-filevariableswmic

Windows Batch: Set variables for FOR loop, WMIC diskdrive get size


below mentioned batch file displays Harddisk size of PC, but if more than 1 HDD is installed in PC, how to set variables in that case? how to do that?

set _hdd=
For /F "Skip=1 Delims=" %%A In (
    '"WMIC diskdrive Where (MediaType="Fixed hard disk media") Get Size"'  
) Do For /F "Tokens=1-2" %%B In ("%%A") Do set _hdd=%%B
Echo %_hh%
pause

Solution

  • @Echo off&SetLocal EnableExtensions EnableDelayedExpansion
    set cnt=0
    For /F "Skip=1 Delims=" %%A In (
        '"WMIC diskdrive Where (MediaType="Fixed hard disk media") Get Size"'  
    ) Do For /F "Tokens=1-2" %%B In ("%%A") Do (
      Set /A cnt+=1
      set _hdd[!cnt!]=%%B
    )
    Set _hdd[
    pause
    

    Sample output:

    > Q:\Test\2017-03\01\SO_42530497.cmd
    _hdd[1]=2000396321280
    _hdd[2]=256052966400