0

I'd like the free space to be in GB, not MB. How could I change that?

for /f "tokens=3" %%a in ('dir c:\') do (
    set bytesfree=%%a
)
set bytesfree=%bytesfree:,=%
echo Free space on partition C: [%bytesfree%] B
endlocal && set bytesfree=%bytesfree%
nyiaq
  • 1
  • Where is this whole code from and what is it trying do? `fsutil volume diskfree c:` should give you the free space (also in GB). – Yisroel Tech Aug 17 '22 at 22:49
  • 1
    Also see this question and answers https://superuser.com/questions/896764/windows-command-line-get-disk-space-in-gb it covers a method to convert the output to GB using VBS or using PowerShell – Yisroel Tech Aug 17 '22 at 23:19
  • i would like to use regular batch, not powershell – nyiaq Aug 18 '22 at 16:41
  • *1)* The VBS is used in a batch file *2)* What about the `fsutil` option? – Yisroel Tech Aug 18 '22 at 16:48

1 Answers1

1
  • For command line:
for /f usebackq^tokens^=4delims^=^>^< %i in (`wmic logicaldisk where Caption^="C:" get FreeSpace /format:xml^|find "VALUE"`)do for /f %G in ('start "" /b /min mshta "vbscript:Execute("createobject(""scripting.filesystemobject""^).GetStandardStream(1^).write(Round(%i/1073741824^)^):close")"^|more')do echo\Free space on partition C: [%G] GB
  • For bat/cmd file:
@echo off 

for /f usebackq^tokens^=4delims^=^>^< %%i in (`wmic logicaldisk where Caption^="C:" get FreeSpace /format:xml^|find "VALUE"`)do for /f %%G in ('
     start "" /b /min mshta "vbscript:Execute("createobject(""scripting.filesystemobject""^).GetStandardStream(1^).write(Round(%%i/1073741824^)^):close")"^|more
   ')do echo\Free space on partition C: [%%G] GB 

  1. You have no option that makes the space available in MB or GB
  2. To get in MB or GB units, at some point you have to convert from unit bytes
  3. See set /a command can convert units but has 32 bit integer limit
  4. When converting, it is necessary to use or , in hybrid mode or in a "mixed" command line.
  5. It is possible to use a "mixed" line in hybrid mode or bat file, and as in the example above, it is possible to execute with mshat.exe to interpret and execute with vbs the desired conversion of bytes to GB:
Io-oI
  • 7,588
  • 3
  • 12
  • 41