windows - Missing operand in Batch Script -


i think part there's error. code fragment inside loop fyi.

set /a used=%size% - %free% set /a percentage=100 * %used% / %size% 

how implement arithmetic equation?

thank much!

as others have said, 1 solution problem delayed expansion.

your actual code must like:

for ... in (...) (   set size=...   set free=...   set /a used=%size% - %free%   set /a percentage=100 * %used% / %size% ) 

the problem %size%, %free%, , %used% expanded when line parsed, , entire loop (also, parenthesized block) parsed in 1 pass, before loop executed. expand value existed before loop executed, empty in case.

one solution use delayed expansion, takes place @ execution time. delayed expansion must enabled before used.

setlocal enabledelayedexpansion ... in (...) (   set size=...   set free=...   set /a used=!size! - !free!   set /a percentage=100 * !used! / !size! ) 

but not necessary if doing set /a computations because set /a command knows how expand variables without doing it. also, multiple computations can concatenated on 1 line comma in between. note automatic expansion , concatenated assignments work /a option.

for ... in (...) (   set size=...   set free=...   set /a "used=size-free, percentage=100*used/size" ) 

you still have potential problem in batch supports integer numbers. fractional portion truncated. might want round fractional portion up. example, percentage of 45.7 reported 45, might want round 46. following formula can used rounded integral percents:

set /a "used=size-free, percentage=(1000*used/size+5)/10" 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -