Windows batch file to get C:\\ drive total space a

2019-02-15 00:30发布

问题:

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.

回答1:

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)



回答2:

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

dir | find "bytes"


回答3:

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.



回答4:

This should work in batch:

for /f "tokens=2" %%S in ('wmic volume get DriveLetter^, FreeSpace ^| findstr "^C:"') do set space=%%S
echo %space%