Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows batch file to get C:\ drive total space and free space available

I need a bat file to get C:\ drive total space and free space available in GB (giga bytes) in a Windows system and create a text file with the details.

Note: i dont want to use any external utilities.

like image 238
Mani Avatar asked Jun 22 '13 22:06

Mani


People also ask

How do I check my C drive free space?

To check the total disk space left on your Windows 10 device, select File Explorer from the taskbar, and then select This PC on the left. The available space on your drive will appear under Devices and drives.

How do I see disk space in CMD?

Search command prompt in Windows 10, and right-click on the result and choose Run as administrator. Step 2. Type wmic diskdrive get size and press Enter. Finally, the total size of hard disk space (in pure number) is displayed in the figure below.

How much space should be left free in C drive?

The 15% Rule of Thumb for Mechanical Hard Drives You'll commonly see a recommendation that you should leave 15% to 20% of a drive empty.

How do I check disk space on multiple servers using PowerShell?

Run the PowerShell script Get-DiskSpaceReport. ps1. It will retrieve all the Windows Servers in the domain and export the disk drives to a CSV file. Open the CSV file in C:\Temp\DiskSpaceReport.


3 Answers

cut 9 digits of the size by bytes to get the size in GB:

@echo off & setlocal ENABLEDELAYEDEXPANSION
SET "volume=C:"
FOR /f "tokens=1*delims=:" %%i IN ('fsutil volume diskfree %volume%') DO (
    SET "diskfree=!disktotal!"
    SET "disktotal=!diskavail!"
    SET "diskavail=%%j"
)
FOR /f "tokens=1,2" %%i IN ("%disktotal% %diskavail%") DO SET "disktotal=%%i"& SET "diskavail=%%j"
(ECHO(Information for volume %volume%
ECHO(total  %disktotal:~0,-9% GB
ECHO(avail. %diskavail:~0,-9% GB)>size.txt
TYPE size.txt

cmd can calculate only with numbers up to 2^31-1   (2,147,483,647 ~ 2.000001 Gigabytes)

like image 140
Endoro Avatar answered Sep 20 '22 19:09

Endoro


Not a complete solution by any means, but someone might find this helpful:

dir | find "bytes"
like image 22
Joe Bourne Avatar answered Sep 18 '22 19:09

Joe Bourne


This is probably not at all what you want since it uses PowerShell, but "external utilities" is a bit nebulous and leaves me some wiggle room. Plus, it's essentially a one-liner.

SETLOCAL

FOR /F "usebackq tokens=1,2" %%f IN (`PowerShell -NoProfile -EncodedCommand "CgBnAHcAbQBpACAAVwBpAG4AMwAyAF8ATABvAGcAaQBjAGEAbABEAGkAcwBrACAALQBGAGkAbAB0AGUAcgAgACIAQwBhAHAAdABpAG8AbgA9ACcAQwA6ACcAIgB8ACUAewAkAGcAPQAxADAANwAzADcANAAxADgAMgA0ADsAWwBpAG4AdABdACQAZgA9ACgAJABfAC4ARgByAGUAZQBTAHAAYQBjAGUALwAkAGcAKQA7AFsAaQBuAHQAXQAkAHQAPQAoACQAXwAuAFMAaQB6AGUALwAkAGcAKQA7AFcAcgBpAHQAZQAtAEgAbwBzAHQAIAAoACQAdAAtACQAZgApACwAJABmAH0ACgA="`) DO ((SET U=%%f)&(SET F=%%g))

@ECHO Used: %U%
@ECHO Free: %F%

Since batch/CMD is bad at nearly everything, I decided to use PowerShell, which is meant for such stuff and has quick and easy access to WMI.

Here's the PowerShell code:

Get-WMIObject -Query "SELECT * FROM Win32_LogicalDisk WHERE Caption='C:'" `
  | % { 
      $f = [System.Math]::Round($_.FreeSpace/1024/1024/1024,1);
      $t = [System.Math]::Round($_.Size/1024/1024/1024,1);
      Write-Host ('' + ($t-$f) + ',' + $f);
  }

This spits out the two values separated by a comma. Now, if only we could do this in a FOR loop!

PowerShell has the nice ability to accept a Base64-encoded command (to eliminate the need for escaping and making the code hard to read), so all we need to do is shrink this command as much as possible (to reduce the size of the encoded string—strictly a nicety, not absolutely necessary). I also reduced the sizes to integers, which rounded them. It's at least closer than discarding the lower-order decimal digits.

Shrinking the encoded command and encoding it in PowerShell looks like this:

$code = {
gwmi Win32_LogicalDisk -Filter "Caption='C:'"|%{$g=1073741824;[int]$f=($_.FreeSpace/$g);[int]$t=($_.Size/$g);Write-Host ($t-$f),$f}
}
$enc = [convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($code))
Write-Host $enc

(See PowerShell /? for more details.)

I would expect this to run on any Win7 or Win8 machine in existence. The PoSH code doesn't rely on any advanced features (except maybe the EncodedCommand bit), so if PoSH is installed on the XP or Vista machine, there's a good chance of it working. I can't speak about the history of MS pushing PoSH via Windows Update, but I think there's a good chance that this will work ubiquitously.

like image 44
mojo Avatar answered Sep 18 '22 19:09

mojo