SS64 Discussion Forum

You are not logged in.

#1 2010-02-08 17:16:10

nishathr
8086
Registered: 2009-08-26
Posts: 10

To check disk space from a .bat file

Hi can some one tell me how to check the disk space in MB from a command line / bat file ?
i also want to get those details from one server to another serevr , please let me know thanks!

Offline

#2 2010-02-08 20:59:59

Drewfus
8088
From: Australia
Registered: 2010-01-10
Posts: 31

Re: To check disk space from a .bat file

For your first problem, try this;

@echo off
setlocal enabledelayedexpansion
echo DL  Free MB
echo --  -------
for /F "tokens=1,*" %%A in ('fsutil fsinfo drives') do @for %%K in (%%B) do @for %%U in (%%K) do @for /F "tokens=1 delims=\" %%e in ('fsutil fsinfo drivetype %%U ^| find "Fixed"') do @for /F "tokens=4" %%o in ('fsutil fsinfo ntfsinfo %%e ^| find "Free Clusters"') do @set /a free=%%o / 256 > nul & echo %%e  !free!

Note Assumes fixed drives are formatted NTFS with 4096 Byte cluster size.

Offline

#3 2010-02-09 03:51:17

bluesxman
Sun Fire
From: Leeds, UK
Registered: 2006-12-29
Posts: 702

Re: To check disk space from a .bat file

Doesn't work correctly on my WinXP -- only shows the first drive it finds.

I believe it'll be because the output of "fsutil fsinfo drives" is slightly different than on, say, Vista.

Offline

#4 2010-02-09 07:04:50

Drewfus
8088
From: Australia
Registered: 2010-01-10
Posts: 31

Re: To check disk space from a .bat file

Having problem with the video card in my XP machine, so didn't check the script on that platform. Sorry about that.

Here is my 'fsutil fsinfo drives' output on my Win 7 machine;

Drives: C:\ D:\ F:\

Is it the backslashes?

Last edited by Drewfus (2010-02-09 07:05:55)

Offline

#5 2010-02-09 10:17:43

jumper
8086
From: tunisia
Registered: 2009-04-17
Posts: 21
Website

Re: To check disk space from a .bat file

Drewfus wrote:

For your first problem, try this;

@echo off
setlocal enabledelayedexpansion
echo DL  Free MB
echo --  -------
for /F "tokens=1,*" %%A in ('fsutil fsinfo drives') do @for %%K in (%%B) do @for %%U in (%%K) do @for /F "tokens=1 delims=\" %%e in ('fsutil fsinfo drivetype %%U ^| find "Fixed"') do @for /F "tokens=4" %%o in ('fsutil fsinfo ntfsinfo %%e ^| find "Free Clusters"') do @set /a free=%%o / 256 > nul & echo %%e  !free!

Note Assumes fixed drives are formatted NTFS with 4096 Byte cluster size.

maybe this:

@Echo oFF
Setlocal EnableDelayedExpansion

Echo Drive   FreeSpace MB
Echo -----   ------------
Set List=&For /F "tokens=*" %%A in ('fsutil fsinfo drives^|MORE') do Set "List=!List! %%A"
For %%B In (%List:*:=%) Do For /F "Delims=-\" %%C In ('fsutil fsinfo drivetype %%B ^| Find /i "Fixe"') Do (
  For /F "Tokens=2 Delims=:" %%D In ('Fsutil Fsinfo NTFSInfo %%C ^|Findstr /I "free libre"') Do (
  Set /A Free=0
  Set /A "Free=%%D/256"
  Echo, %%C       !Free! MB
 )
)

Goto:EOF

Offline

#6 2010-02-09 12:44:11

bluesxman
Sun Fire
From: Leeds, UK
Registered: 2006-12-29
Posts: 702

Re: To check disk space from a .bat file

Drewfus wrote:

Having problem with the video card in my XP machine, so didn't check the script on that platform. Sorry about that.

Here is my 'fsutil fsinfo drives' output on my Win 7 machine;

Drives: C:\ D:\ F:\

Is it the backslashes?

No, it was discussed at length here: http://ss64.org/viewtopic.php?id=327

I propose the following (I believe it'll work on XP, Vista and 7):

for /F "tokens=*" %%A in ('fsutil fsinfo drives ^| find /v ""') do (
    @for %%K in (%%A) do if "%%K" NEQ "Drives:" (
        @for %%U in (%%K) do (
            @for /F "tokens=1 delims=\" %%e in ('fsutil fsinfo drivetype %%U ^| find "Fixed"') do (
                @for /F "tokens=4" %%o in ('fsutil fsinfo ntfsinfo %%e ^| find "Free Clusters"') do (
                    @set /a free=%%o / 256 > nul & echo %%e  !free!
                )
            )
        )
    )
)

I've broken it down from the single line version to make it easier to understand for the uninitiated.

Functional amendments (rather than the clarity changes) are highlighted in red here:
for /F "tokens=*" %%A in ('fsutil fsinfo drives ^| find /v ""') do (
    @for %%K in (%%A) do if "%%K" NEQ "Drives:" (

Offline

#7 2010-02-10 10:55:58

Chimaera
IA-32
Registered: 2009-08-24
Posts: 121

Re: To check disk space from a .bat file

Odd the origanal one by Drewfus gives me this on win 7 Home premium

DL  Free MB
--  -------
C:  13291
D:  2590
E:  74
F:  2922
I:  9905
X:  247859
Y:  283415
Z:  235874

But the last one from Bluesxman gives

C:  !free!
D:  !free!
E:  !free!
F:  !free!
I:  !free!
X:  !free!
Y:  !free!
Z:  !free!

Last edited by Chimaera (2010-02-10 12:17:33)

Offline

#8 2010-02-10 11:35:16

bluesxman
Sun Fire
From: Leeds, UK
Registered: 2006-12-29
Posts: 702

Re: To check disk space from a .bat file

Chimaera wrote:

But the last one from Bluesxman gives

C:  !free!
D:  !free!
E:  !free!
F:  !free!
I:  !free!
X:  !free!
Y:  !free!
Z:  !free!

That'll be because you've used the command I provided in isolation, whereas it was intended as a modification to the original script -- which has delayed expansion enabled.

Last edited by bluesxman (2010-02-10 11:35:29)

Offline

#9 2010-02-10 12:17:14

Chimaera
IA-32
Registered: 2009-08-24
Posts: 121

Re: To check disk space from a .bat file

aah my bad lol

Offline

Board footer

Powered by FluxBB