AJ01
AJ01

Reputation: 235

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.

Upvotes: 1

Views: 775

Answers (2)

Andriy M
Andriy M

Reputation: 77737

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".

Upvotes: 2

Related Questions