10

Hello having my first go with a BATCH script, I'm getting the size of the HDD as follow:

wmic diskdrive get size

Which works fine but I'd like to store this value into a variable for later use, such as using ECHO to display the value.

I'm not sure how I set the output of the above command to a variable. I went with:

SET hddbytes=wmic diskdrive get size

But this just sets the variable to the above text string and not the output.

foxidrive
  • 40,353
  • 10
  • 53
  • 68
twigg
  • 3,753
  • 13
  • 54
  • 96
  • I think that all these solutions will give only one drive in the list - mine has a few drives listed. BTW, this is `batch` script and not `BASH` – foxidrive Nov 26 '13 at 15:09
  • @foxidrive yes of course :) I'm mostly defiantly not a Windows person just have BASH on the mind, thanks for pointing it out – twigg Dec 04 '13 at 23:47

4 Answers4

15

For usage in batch file. From command line, replace %% with %

for /f "tokens=*" %%f in ('wmic diskdrive get size /value ^| find "="') do set "%%f"
echo %size%

Or, if you want to use you prefered variable

for /f "tokens=2 delims==" %%f in ('wmic diskdrive get size /value ^| find "="') do set "myVar=%%f"
echo %myVar%
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • +1.Would prefer Textvaluelist format and thus `set` could be used directly. – npocmaka Nov 26 '13 at 14:32
  • you can use `"delims="` and `set %%f` directly but this is just the way I prefer to set values when using wmic – npocmaka Nov 26 '13 at 14:57
  • @npocmaka: Yes. I understand. If you look at the first code in answer, changing `delims=` for `tokens=*` you will have the same solution `set "%%f"` – MC ND Nov 26 '13 at 15:19
  • For both of these, as-is, in a batch file on Windows 7, I get: `'wmic diskdrive get size /value | find "="' is not recognized as an internal or external command, operable program or batch file.` – ashleedawg Nov 01 '18 at 09:40
  • @ashleedawg, Tested in W10 (five years from the answer, I don't have W7 at hand) and working. Have you checked `wmic` is available in your install? Some problem with `path`? – MC ND Nov 01 '18 at 10:08
1

You want:

for /f %%a in ('wmic diskdrive get size^|findstr [0-9]') do echo %%a
Matt Williamson
  • 6,947
  • 1
  • 23
  • 36
1

Updated:

  1. WMIC's output could get a trailing Carriage Return in some environment:

    Size        <CR>
    <CR><LF>
    256052966400<CR>
    <CR><LF>
    500105249280<CR>
    <CR><LF>
    15496427520 <CR>
    <CR><LF>
    
  2. use csv format, and use FOR loop to truncate the wanted value from the wmic output:

    For /F "tokens=2,3 delims=," %%a in ('"wmic diskdrive get size,Status 
    /format:csv |findstr "[0-9]" "') do (
    echo "%%a"
    )
    

Another Batch Script Example:

setlocal ENABLEDELAYEDEXPANSION
:: You can change active code page as well::
@chcp 936 >NUL

:: [example] Remove all network printers:
for /F "tokens=2,3 delims=," %%a in ('"wmic printer where 'local=FALSE' get Name,PrinterStatus /format:csv |findstr "." "') do (
    echo "%%a"
    rundll32 printui.dll,PrintUIEntry /n "%%a" /dn /q
)
YGXXII
  • 331
  • 2
  • 7
0
for /f "delims="  %%w in ('wmic diskdrive get size /format:Textvaluelist.xsl') do for /f "usebackq delims=" %%a in ('%%w') do set %%a
echo %size%
npocmaka
  • 55,367
  • 18
  • 148
  • 187