开发者

Putting IF condition in batch file?

I want to check if space in C: drive is more than 60 GB then next step should be done and if space is less it should display a message?

For checking the disk space i can use "fsutil volume diskfree c:" but i dont know how to use it inside the above condition开发者_开发知识库.


Assuming you are fine with treating 1 GB as 109 bytes, not as 230 bytes, you could simply

1) pipe the results of FSUTIL to the FIND utility to filter out irrelevant rows,

2) use the modified command line in a FOR /F loop to store the output into a variable,

3) cut off the last 9 characters from the variable's value and compare the rest to the required quantity.

And here's my attempt at putting the above steps together:

@ECHO OFF
SET required=60
FOR /F "tokens=2 delims=:" %%A IN ('fsutil volume diskfree c: ^| find "# of bytes"') DO SET "space=%%A"
IF %space:~0,-9% LSS %required% ECHO Space is less than %required% GB!

The above script processes the total space. If you want to check the free space, change "# of bytes" to "# of free".


Basic IF reading : http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true

similar blog post here : http://fei-automation.blogspot.com/2011/02/how-to-monitor-free-space-of-group-of.html

similar SO : Free space in a CMD shell

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜