Help! .bat file to get free disk space

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
broksainis
Posts: 5
Joined: 17 Nov 2013 13:46

Help! .bat file to get free disk space

#1 Post by broksainis » 17 Nov 2013 13:55

Hello.
I will be thankful, if someone help me.
First of all, my file:

[spoiler]

Code: Select all

@echo off & setlocal ENABLEDELAYEDEXPANSION
cls
echo -==================-
echo Disks:
echo -==================-

for /f "skip=1 tokens=1,2 delims=: " %%a in ('wmic logicaldisk get deviceid^') do (
   set "_DRIVE.LETTERS.USED=!_DRIVE.LETTERS.USED!%%a,%%b"
)
set _DRIVE.LETTERS.USED=%_DRIVE.LETTERS.USED:~0,-2%
set _DRIVE.LETTERS.USED=%_DRIVE.LETTERS.USED:,@=, @%
set _DRIVE.LETTERS

echo -==================-
Echo 1. C disk
Echo 2. D disk
Echo 3. E disk
Echo -==================-   
set /p op=Input:
IF "%op%"=="1" goto C
IF "%op%"=="2" goto D
IF "%op%"=="3" goto E
echo "%op%" wrong
pause
echo
goto start

:C
echo -==================-
echo
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(Informacija par disku - %volume%
ECHO(kopeejaa  %disktotal:~0,-9% GB
ECHO(pieejamaa %diskavail:~0,-9% GB)
pause
goto exit

:D
echo
SET "volume=D:"
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(Informacija par disku - %volume%
ECHO(kopeejaa  %disktotal:~0,-9% GB
ECHO(pieejamaa %diskavail:~0,-9% GB)
pause
goto exit

:E
echo
SET "volume=E:"
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(Informacija par disku - %volume%
ECHO(kopeejaa  %disktotal:~0,-9% GB
ECHO(pieejamaa %diskavail:~0,-9% GB)
pause
goto exit
exit
@exit
[/spoiler]

Is it possible that batch file shows all avaliable spaces for every disk in every computer, ? Maybe someone can edit my file ?
Is it possible to convert Gigabytes to progress bar, not to show free space in gigabytes, but show it as progress bar?

penpen
Expert
Posts: 1859
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Help! .bat file to get free disk space

#2 Post by penpen » 17 Nov 2013 16:00

broksainis wrote:Is it possible that batch file shows all avaliable spaces for every disk in every computer?
Yes. On the german Win XP version the following returns the disks free bytes.

Code: Select all

@echo off
setlocal enableDelayedExpansion
for %%a in (C D E) do if exist %%a: (
   for /f "tokens=3" %%b in ('dir %%a:') do set "FreeBytesDrive_%%a=%%b"

   echo Drive %%a free disk space:
   echo(!FreeBytesDrive_%%a! B == !FreeBytesDrive_%%a:~0,-4! KB == !FreeBytesDrive_%%a:~0,-8! MB == !FreeBytesDrive_%%a:~0,-12! GB == !FreeBytesDrive_%%a:~0,-16! TB
   echo(no dots: !FreeBytesDrive_%%a:.=! B
   echo(
)
endlocal
the letters 'C', 'D', and 'E' are the drive letters, from which you may want to know the free space in bytes.
Maybe you have to change the '3' after "tokens=".
If you don't get the free bytes, just use "*" instead and see which token contains the needed information.

broksainis wrote:Is it possible to convert Gigabytes to progress bar, not to show free space in gigabytes, but show it as progress bar?
I'm not sure how you mean this; should the progress bar be normalized to:
- percentage use,
- maximum hdd size,
- sum of all hdd sizes,
- (whatever possible)
But in principle: Yes this should be possible.
I've added size translation to the upper example.

penpen

Edit: Added second answer, and extended the code.

aGerman
Expert
Posts: 3935
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help! .bat file to get free disk space

#3 Post by aGerman » 17 Nov 2013 17:31

Try something like that:

Code: Select all

@echo off &setlocal
set "GB=1073741824"
for /f "skip=1 delims=" %%i in ('wmic logicaldisk get DeviceID^,FreeSpace^,Size') do (
  for /f "tokens=1-3" %%j in ("%%i") do call :output %%j %%k %%l
)
pause
goto :eof

:output
for /f "tokens=1-4" %%i in (
  'mshta vbscript:Execute("CreateObject(""Scripting.FileSystemObject"").GetStandardStream(1).Write(FormatNumber(%3/%GB%, 2) & "" "" & FormatNumber((%3-%2)/%GB%, 2) & "" "" & FormatNumber(%2/%GB%, 2) & "" "" & Round((%3-%2)*50/%3)):Close"^)'
) do (
  set "size=      %%i"
  set "used=      %%j"
  set "free=      %%k"
  set /a "nUsed=%%l, nFree=50-%%l"
)
echo(
echo       %1
echo Size: %size:~-10% GB
echo Used: %used:~-10% GB
echo Free: %free:~-10% GB
echo(
for /l %%i in (1 1 %nUsed%) do <nul set /p "=±"
for /l %%i in (1 1 %nFree%) do <nul set /p "=Û"
echo(&echo(&echo(
goto :eof

Big-number calculations are outsourced to HTA/VBS.

Regards
aGerman

AiroNG
Posts: 46
Joined: 17 Nov 2013 15:00
Location: Germany

Re: Help! .bat file to get free disk space

#4 Post by AiroNG » 17 Nov 2013 17:55

Hi,
Very nice solutions.

@aGerman

since cmd.exe can only do arithmetic with 32bit numbers.
Is there some kind of workaround, so that it can be done without the "outsourcing"?

And i found a little problem/bug(?) when using your code:
It displays the correct data on my 3 drives (C:, D:, E:), but on the fourth one,
a virtual DVD-Drive (F:), it uses the data from the third HDD-drive (E:).

Further a skript-error pops up: "error: syntax error" (I'd post the entire error-popup, but it is in german and i do not know the forum-rules regarding the use of langauges other than english)

einstein1969
Expert
Posts: 776
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Help! .bat file to get free disk space

#5 Post by einstein1969 » 17 Nov 2013 18:19

try:

Code: Select all

@echo off & setlocal EnableDelayedExpansion

set Width=20

For /L %%N in (0,1,%width%) do (set "Full=!Full!Û" & set "Empt=!Empt!°")

for /f "skip=1 tokens=1-3" %%a in ('wmic logicaldisk get deviceid^,FreeSpace^,Size') do (
   set disk=%%a
   if "!disk:~1!"==":" (
      set "FreeBytesDrive=%%b"
      set "TotBytesDrive=%%c"
      echo(
      if defined TotBytesDrive (
         set /a Ratio=!FreeBytesDrive:~0,-6!*Width/!TotBytesDrive:~0,-6!, RR=Width-Ratio
         for %%r in (!Ratio!) do for %%p in (!RR!) do echo %%a !Full:~0,%%p!!Empt:~0,%%r!
      ) else echo(%%a N/D
   )
)



Einstein1969

aGerman
Expert
Posts: 3935
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help! .bat file to get free disk space

#6 Post by aGerman » 17 Nov 2013 18:29

Is there some kind of workaround, so that it can be done without the "outsourcing"?

Yes, just search the forum or the internet. I don't use those workarounds. Finally it's always a combination of string manipulation and 32 bit integer calculation. I'd rather write the entire program in a different language.

And i found a little problem/bug(?) when using your code:
It displays the correct data on my 3 drives (C:, D:, E:), but on the fourth one,
a virtual DVD-Drive (F:), it uses the data from the third HDD-drive (E:).

Maybe WMIC can't read the data.
Run:

Code: Select all

@echo off &setlocal
for /f "skip=1 delims=" %%i in ('wmic logicaldisk get DeviceID^,FreeSpace^,Size') do (
  for /f "tokens=1-3" %%j in ("%%i") do echo %%j %%k %%l
)
pause


Further a skript-error pops up: "error: syntax error" (I'd post the entire error-popup, but it is in german and i do not know the forum-rules regarding the use of langauges other than english)

It's an US American forum with users from all over the world. For that reason we should always write the postings in English. However a quotation is a quotation (also in your mother tongue). Of course you would make people happy with an additional shortened English description.

Regards
aGerman

broksainis
Posts: 5
Joined: 17 Nov 2013 13:46

Re: Help! .bat file to get free disk space

#7 Post by broksainis » 17 Nov 2013 18:42

aGerman wrote:Try something like that:

Code: Select all

@echo off &setlocal
set "GB=1073741824"
for /f "skip=1 delims=" %%i in ('wmic logicaldisk get DeviceID^,FreeSpace^,Size') do (
  for /f "tokens=1-3" %%j in ("%%i") do call :output %%j %%k %%l
)
pause
goto :eof

:output
for /f "tokens=1-4" %%i in (
  'mshta vbscript:Execute("CreateObject(""Scripting.FileSystemObject"").GetStandardStream(1).Write(FormatNumber(%3/%GB%, 2) & "" "" & FormatNumber((%3-%2)/%GB%, 2) & "" "" & FormatNumber(%2/%GB%, 2) & "" "" & Round((%3-%2)*50/%3)):Close"^)'
) do (
  set "size=      %%i"
  set "used=      %%j"
  set "free=      %%k"
  set /a "nUsed=%%l, nFree=50-%%l"
)
echo(
echo       %1
echo Size: %size:~-10% GB
echo Used: %used:~-10% GB
echo Free: %free:~-10% GB
echo(
for /l %%i in (1 1 %nUsed%) do <nul set /p "=±"
for /l %%i in (1 1 %nFree%) do <nul set /p "=Û"
echo(&echo(&echo(
goto :eof

Big-number calculations are outsourced to HTA/VBS.

Regards
aGerman


TY. Works :D great for me! But there is no need to show avaliable space for F: , because drive F: is DVD-ROM, and thats way it shows me script error.

aGerman
Expert
Posts: 3935
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help! .bat file to get free disk space

#8 Post by aGerman » 17 Nov 2013 18:54

I see. I don't have a DVD-ROM drive built in my small Netbook. Tomorrow I'll try to get a workaround for that error.

Regards
aGerman

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Help! .bat file to get free disk space

#9 Post by foxidrive » 17 Nov 2013 19:27

It displays nicely, aGerman, but network drives that are not active generate an error too.

einstein1969: your code works well here and does what the OP wants.

einstein1969
Expert
Posts: 776
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Help! .bat file to get free disk space

#10 Post by einstein1969 » 17 Nov 2013 20:08

thanks for feedback foxidrive

This version for display Gb without external using alternate series.

Code: Select all

@echo off & setlocal EnableDelayedExpansion

set Width=20

For /L %%N in (0,1,%width%) do (set "Full=!Full!Û" & set "Empt=!Empt!°")

for /f "skip=1 tokens=1-3" %%a in ('wmic logicaldisk get deviceid^,FreeSpace^,Size') do (
   set disk=%%a
   if "!disk:~1!"==":" (
      set "FreeBytesDrive=%%b"
      set "TotBytesDrive=%%c"
      echo(
      if defined TotBytesDrive (
         set /a Fmb=!FreeBytesDrive:~0,-6!, Tmb=!TotBytesDrive:~0,-6!, Umb=Tmb-Fmb

         call :ConvertGb Tgb Tmb
         call :ConvertGb Fgb Fmb
         call :ConvertGb Ugb Umb

         set /a Ratio=Fmb*Width/Tmb, RR=Width-Ratio
         for %%r in (!Ratio!) do for %%p in (!RR!) do echo %%a !Full:~0,%%p!!Empt:~0,%%r! [Used:!UGb! - Free:!FGb! / Tot:!TGb!] Gb
      ) else echo(%%a N/D
   )
)
goto :eof

::GB with 3 decimal, use only 2 for now, high precision, add padding
:ConvertGB
  set /a tmp=%2-%2/10+%2*1000/31926
  set "tmp=      !tmp:~0,-3!.!tmp:~-3,2!"
  set tmp=!tmp:~-6!
  set %1=!tmp: .=0.!
goto :Eof


Einstein1969

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Help! .bat file to get free disk space

#11 Post by foxidrive » 17 Nov 2013 20:14

einstein, it has an issue in calculation. This drive is a 2 TB drive.

Code: Select all

D: █████████████░░░░░░░ [Used:125.25 - Free:737.72 / Tot:862.98] Gb


I have a spanned drive greater than 2 Tb also.

einstein1969
Expert
Posts: 776
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Help! .bat file to get free disk space

#12 Post by einstein1969 » 17 Nov 2013 20:49

foxidrive wrote:einstein, it has an issue in calculation. This drive is a 2 TB drive.

Code: Select all

D: █████████████░░░░░░░ [Used:125.25 - Free:737.72 / Tot:862.98] Gb


I have a spanned drive greater than 2 Tb also.


How many Tb have the bigger?

AiroNG
Posts: 46
Joined: 17 Nov 2013 15:00
Location: Germany

Re: Help! .bat file to get free disk space

#13 Post by AiroNG » 18 Nov 2013 05:05

@aGerman
Maybe WMIC can't read the data.
Run:

Code: Select all

@echo off &setlocal
for /f "skip=1 delims=" %%i in ('wmic logicaldisk get DeviceID^,FreeSpace^,Size') do (
  for /f "tokens=1-3" %%j in ("%%i") do echo %%j %%k %%l
)
pause

That works. Thank you.


As for the error-popup:
Your code stops after E: (third HDD) is fully displayed and continues running, after...

Code: Select all

Skriptfehler (Script-error)
In dem Skript ist ein Fehler aufgetreten. (An error occured in this script)
Zeile: 1 (Line)
Zeichen: 1 (Character)
Fehler: Syntaxfehler (Error: Syntax-Error)
Code: 0
URL: vbscript:Execute("CreateObject(""Scripting.FileSystemObject"").GetStandardStream(1).Write(FormatNumber(/1073741824, 2) & "" "" & FormatNumber((-)/1073741824, 2) & "" "" & FormatNumber(/1073741824, 2) & "" "" & Round((-)*50/)):Close")
Möchten Sie, dass Scripts auf dieser Seite weiterhin ausgeführt werden? (Do you want to continue running scripts on this page?)
Ja / Nein   (Yes / No)

... by choosing either Yes or No and if a blank window that shows is closed.

/off-topic:
regarding my language remark: since i'm a german and thus using a german version of windows, i do not know the exact or correct translation when it comes to error messages. I've made some rather unpleasant experiences with english message boards when it comes to non-english languages, even with a translation appended. So i've learned to be very careful in that matter.

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Help! .bat file to get free disk space

#14 Post by foxidrive » 18 Nov 2013 05:08

einstein1969 wrote:How many Tb have the bigger?


2 decimal places should cater for realistic sized spanned drives. IE: 1 to 99

aGerman
Expert
Posts: 3935
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help! .bat file to get free disk space

#15 Post by aGerman » 18 Nov 2013 11:25

I tried my script at work and of course got the same error message. The reason is simple - free space and size can't be determined. That means %2 and %3 are empty and cause the script error since there are missing operands for the calculations.
Finally all we have to do is a check whether %3 is empty.
Change the sub routine as follows:

Code: Select all

:output
echo(
echo       %1
if "%3"=="" (
  echo Unable to discover the drive properties.&echo(&echo(
  goto :eof
)
for /f "tokens=1-4" %%i in (
  'mshta vbscript:Execute("CreateObject(""Scripting.FileSystemObject"").GetStandardStream(1).Write(FormatNumber(%3/%GB%, 2) & "" "" & FormatNumber((%3-%2)/%GB%, 2) & "" "" & FormatNumber(%2/%GB%, 2) & "" "" & Round((%3-%2)*50/%3)):Close"^)'
) do (
  set "size=      %%i"
  set "used=      %%j"
  set "free=      %%k"
  set /a "nUsed=%%l, nFree=50-%%l"
)
echo Size: %size:~-10% GB
echo Used: %used:~-10% GB
echo Free: %free:~-10% GB
echo(
for /l %%i in (1 1 %nUsed%) do <nul set /p "=±"
for /l %%i in (1 1 %nFree%) do <nul set /p "=Û"
echo(&echo(&echo(
goto :eof

Regards
aGerman

Post Reply