Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file set wmi output as a variable

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.

like image 293
twigg Avatar asked Nov 26 '13 14:11

twigg


2 Answers

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%
like image 130
MC ND Avatar answered Oct 10 '22 11:10

MC ND


You want:

for /f %%a in ('wmic diskdrive get size^|findstr [0-9]') do echo %%a
like image 45
Matt Williamson Avatar answered Oct 10 '22 10:10

Matt Williamson