Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi all,
I'm writing a new batch file to check the total and free space of certain network drives. It'll run every morning so that when I arrive, I'll have a quick report of the current state of storage across the network.
Currently, I have a simple Batch file running that pipes a Find command to the Dir of the \\network\drive to give me the "bytes free". That is,
echo Networkdrive 1 Free Space >> drivespacefree.txt
dir \\networkdrive1\maindirectory|find /i "bytes free" >> drivespacefree.txtIt repeats for each drive, until drivespacefree.txt contains a listing of the free space in bytes of all network drives.
Now, what I want to do is sort of the same thing - except, echo to the text file the free space, the total space, and percentage remaining.
My questions, then, are - how do I get a drive's free space SET to a variable, a drive's total space SET to a variable, so that I can Echo those, and then echo (with SET /A) the percent remaining? Preferably in a nice round GB (because the higher ups like to look at reports but don't like commas and bytes)
I tried messing with msinfo32.exe, using:
start /wait msinfo32.exe /computer 192.168.1.xxx /categories +componentsstorage /report spacefree.txt
but the output report file, "spacefree.txt" can't seem to be searched with FINDSTR.
Please help, I'm at my wit's end with this. Thanks!

Hi,
I dunno msinfo32.exe but if you post the txt file, we can have a look.
=====================================
If at first you don't succeed, you're about average.M2

Well, the msinfo32.exe output text file ranges anywhere from 9k to 500k, depending on the settings. It was an alternative but I don't think it'll work the way I wanted it to...
I'm just desperately searching for a way to get a drive's total space in GB SET to a variable, it's free space in GB SET to a variable, and then SET /A the %free%/%total% * 100 to get a percentage of free space. That's basically it... does anyone know of any way to do this? Any command line apps, etc? I can't believe I'm stumped with this, so any help at all is appreciated.

Read this post the other day and found this while looking for something else...
@echo off
:: Parses the output of the dir command to get
:: the free space on a disk. Uses debug to trim
:: away all unwanted info from the response line.
set diskfree=
deltree /y ~~~ > nul
md ~~~
dir ~~~ | find "dir" > ~dfree.bat
deltree /y ~~~ > nul
set diskfree=
> script echo f 0100 l 11 20
>> script echo e 0104 "set diskfree="
>> script echo rcx
>> script echo 21
>> script echo w
>> script echo q
debug ~dfree.bat < script > nul
del script
call ~dfree.bat
del ~dfree.bat
echo disk free space is %diskfree%hope it helps
Taken from www.ericphelps.com/batch
Fight Crime,
Shoot Back !

Easy to get free space in NT without all those gyrations. What's not so easy is total space.
=====================================
If at first you don't succeed, you're about average.M2

Try this:
::== pcUSED.bat
@echo off
setLocal EnableDelayedExpansionfor /f "tokens=3 delims= " %%A in ('dir /s/-c \ ^|find "File(s)"') do (
set /a fileTOT=%%A/1000000
)
for /f "tokens=3 delims= " %%A in ('dir /s/-c ^|find "bytes free"') do (
set /a FREE=%%A/1000000
)echo used: !fileTOT!MB
echo free: !FREE!MB
set /a TOTAL=!fileTOT!+!FREE!
echo total disk space: !TOTAL!MB
set /a pcUSED=(!fileTOT!*100)/!TOTAL!
echo percent used: !pcUSED!
::==========================
=====================================
If at first you don't succeed, you're about average.M2

Hmm, doesn't appear to work correctly. This is the output that is generated:
Invalid Number. Numbers are limited to 32-bits of precision.
Invalid Number. Numbers are limited to 32-bits of precision.
used: 0MB
free: MB
Missing operand.
total disk space: MB
Divide by zero error.
percent used:

Depending on the size of the drive, you may need to divide further. The one I tested on is 2GB.
=====================================
If at first you don't succeed, you're about average.M2

Hey M2,
thanks for all the help, but I think I'm in over my head now. Most of the drives I'll be checking for space are 100+ gigs, and one is a RAID 5 array at about 1TB. We're adding a 3 TB NAS too, so I'll need to check that...I'm at a loss as to what the FOR loop in your example actually does, and why I'd need to divide further (right now, it's getting MB's, correct?). I haven't really worked with batch files since the early 90's, or done any programming in about 9 years. So all the new commands are a bit confusing.
I understand that you're piping a Find command into the DIR of the entire drive. Apparently every time the Find finds an instance, it sets the var to %A/1 000 000. But what to the Tokens and Delims do?
thanks for any help!

You can look at: for /? but the 'help' is rather tortured, as usual.
"tokens=*" means 'use every chunk' and "delims=" sets the 'separator'; by default, it's SPACE/TAB. "tokens=3" means 'use the 3rd chunk'.
I think you'll need to divide by 1G in stead of 1M to avoid going out of range.
=====================================
If at first you don't succeed, you're about average.M2

So, should I have the FOR loop continue to divide by 1M, but add them up, then divide by 1000 to get gigs after the FOR loop?

Alright this is my current batch file:
@echo off
setLocal EnableDelayedExpansionecho Filetot for loop:
for /f "tokens=3 delims= " %%A in ('dir /s/-c \ ^|find "File(s)"') do (
set /a fileTOT+=%%A/1000000
echo FileTot: !fileTOT! MB
)echo freespace for loop:
for /f "tokens=3 delims= " %%A in ('dir /-c \ ^|find "Dir(s)"') do (
set /a freespace=%%A/1000000000
echo Free: !freespace! GB
)echo used: !fileTOT!MB
echo free: !freespace!GB
set /a TOTAL=!fileTOT!+!FREE!
echo total disk space: !TOTAL!MB
set /a pcUSED=(!fileTOT!*100)/!TOTAL!
echo percent used: !pcUSED!This is the output:
Filetot for loop:
[...tons of lines of FileTOT adding up...]
FileTot: 4290 MB
FileTot: 4291 MB
FileTot: 4291 MB
FileTot: 4291 MB
FileTot: 4292 MB
FileTot: 4293 MB
FileTot: 4293 MB
FileTot: 4293 MB
FileTot: 4293 MB
Invalid number. Numbers are limited to 32-bits of precision.
FileTot: 4293 MB
freespace for loop:
Invalid number. Numbars are limited to 32-bits of precision.
Free: GB
used: 4293MB
free: GB
Missing operand.
total disk space: MB
Missing operand.
percent used:Okay, so apparently I hit a wall somewhere when getting the MB for Filetot, and then I hit the same wall when even just trying to divide by 1 000 000 000 to get freespace in GB. Is there any way to get DOS to use larger numbers? thanks for all the help!

Alright, I think I nailed it. Here's the output from the final batch file (pasted below).
command line: tester \
Total space: 155 GB
Free space: 146 GB (95%)
Used space: 9 GB (5%)Check the batch file below, feel free to copy it for your own usage!
Doug
:: tester.bat
:: 2007 Douglas Craig with massive assistance by Mechanix2Go@echo off
setLocal EnableDelayedExpansion
:: check for path
if ()==(%1%) goto syntax:: check existence of path
if not exist %1 goto badpath:: find total used space
for /f "tokens=3 delims= " %%A in ('dir /s/-c %1 ^|find "File(s)"') do (
set totalused=%%A
):: find total free space
for /f "tokens=3 delims= " %%A in ('dir /-c %1 ^|find "Dir(s)"') do (
set freespace=%%A
):: output truncated totals in temp file for use later
echo.Used !totalused:~0,-9! >"testtemp.txt"
echo.Free !freespace:~0,-9! >>"testtemp.txt":: input truncated total used space from temp file
for /f "tokens=2 delims= " %%A in ('type testtemp.txt ^|find "Used"') do (
set totalused=%%A
):: input truncated free space
for /f "tokens=2 delims= " %%A in ('type testtemp.txt ^|find "Free"') do (
set freespace=%%A
):: calculate total drive space, percentage used, and percentage free
set /a totalspace=!totalused!+!freespace!
set /a percentused=(!totalused!*100)/!totalspace!
set /a percentfree=100-!percentused!:: output results
echo.
echo Total space: %totalspace% GB
echo Free space: %freespace% GB (%percentfree%%%)
echo Used space: %totalused% GB (%percentused%%%):: clean up temp file
del testtemp.txtgoto end
:: syntax info
:syntax
echo.
echo.Usage: TESTER [path]
echo.
echo. where [path] is a directory on drive or network path
echo.
echo. example: TESTER \
echo. TESTER \\mynetwork\sharedfolder
goto end:: invalid path
:badpath
echo.
echo.The path %1 is not accessible.
echo.:end

Hi Doug,
Yep, you nailed it.
Seems like youcould skip some steps and writing to file.
set raw=%%A
set free=!raw:~0,-9!
=====================================
If at first you don't succeed, you're about average.M2

Thanks, that works. Didn't realize you had to reassign variables to get them from the FOR loop. Thanks for all the help!

I'm not a big fan of batch scripting. If you have the knowledge, I would've suggested using VBScript.
You can easily get drive information (including used and free space) with the Win32_LogicalDisk WMI class. The only tiny snag is that the drives MUST be mapped in order for them to show up. But you can easily map the drives, run your calculations, and then disconnect those network drives. And from there, it's easy output to your file! IMHO, that just seems like a lot less hassle. But it all comes down to personal preference.

EDIT: That WMI class I mentioned in the previous post should actually be "Win32_MappedLogicalDisk". The Win32_LogicalDisk class shows both the physical and networked drives.

Here is another suggestion - only I'd like to get a more accurate number that "80GB" on my 80 GB drive which is in fact 74.5 gb formatted....anyway...
@ECHO OFF
@COLOR 7
@SETLOCAL ENABLEEXTENSIONS
@SETLOCAL ENABLEDELAYEDEXPANSION
@CLS@FOR /F "tokens=1-3" %%n IN ('"WMIC LOGICALDISK GET Name,Size,FreeSpace | find /i "%1""') DO @SET FreeBytes=%%n & @SET TotalBytes=%%p
@SET /A TotalSpace=!TotalBytes:~0,-9!
@SET /A FreeSpace=!FreeBytes:~0,-10!
@SET /A TotalUsed=%TotalSpace% - %FreeSpace%
@SET /A PercentUsed=(!TotalUsed!*100)/!TotalSpace!
@SET /A PercentFree=100-!PercentUsed!@ECHO Total space: %TotalSpace%GB
@ECHO Free space: %FreeSpace%GB
@ECHO Used space: %TotalUsed%GB
@ECHO Percent Used: %PercentUsed%%%
@ECHO Percent Free: %PercentFree%%%@SET TotalSpace=
@SET FreeSpace=
@SET TotalUsed=
@SET PercentUsed=
@SET PercentFree=
/Tipsmark

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |