Get free disk space "C:" using the batch file

I want to write a batch file that gives me free space on drive C.

0
source share
1 answer

The following script will give you free bytes on disk:

@setlocal enableextensions enabledelayedexpansion @echo off for /f "tokens=3" %%a in ('dir c:\') do ( set bytesfree=%%a ) set bytesfree=%bytesfree:,=% echo %bytesfree% endlocal && set bytesfree=%bytesfree% 

Note that this depends on the output of your dir command, which needs the last line containing the free space of 24 Dir(s) 34,071,691,264 bytes free format. In particular:

  • it should be the last line (or you can change the for loop to explicitly define the line, rather than relying on the bytesfree setting for each line).
  • the free space should be the third "word" (or you can change the tokens= bit to get another word).
  • thousands separators are a character , (or you can replace comma-substitution with something else).

It does not pollute the namespace of the environment, and upon exit, only the bytesfree variable. If your dir output is different (e.g. different language settings or language settings), you need to configure the script.

+4
source

All Articles