Help! .bat file to get free disk space

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

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

#16 Post by einstein1969 » 18 Nov 2013 20:02

foxidrive wrote:
einstein1969 wrote:How many Tb have the bigger?


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


In GB:

freespace.cmd:

Code: Select all

@echo off & setlocal EnableDelayedExpansion

set Width=20
rem remove comment for small size
rem set small=1

if defined small (set cs=Ü.) else set cs=Û°
For /L %%N in (0,1,%width%) do (set "Full=!Full!!cs:~0,1!" & set "Empt=!Empt!!cs:~1!" & set "Space=!Space! ")

echo(   !Space:~0,%Width%!      Used      Free       Tot

for /f "skip=1 tokens=1-3" %%a in ('wmic logicaldisk get deviceid^,FreeSpace^,Size') do (
   set disk=%%a
   if "!disk:~1!"==":" (

      If not defined small echo(
      if not "%%c"=="" (

        call :ConvertGb TGB %%c
        call :ConvertGb FGB %%b
        set /a UGB=TGB-FGB, Ratio=FGB*Width/TGB, RR=Width-Ratio

        for %%d in (UGB FGB TGB) do call :pad %%d

        for %%r in (!Ratio!) do for %%p in (!RR!) do echo %%a !Full:~0,%%p!!Empt:~0,%%r! [!UGB!  !FGB!  !TGB!] GB
      ) else echo(%%a N/D
   )
)
goto :eof
       
:ConvertGb retvat Bytes         rem 2decimal from 0,01GB to 102400,00GB (100TB)
  set "tmp=%~200"
  if "!tmp:~9,1!"=="" (set %1=0) else (set /a t=!tmp:~0,-9!, %1=t-t/10+t/30-t/400+t/2000-t/90000+t/2200000)
goto :eof

:pad var                        rem pad until 98TB for optimizing space char
  set tmp=!%1!
  set tmp=        !tmp:~0,-2!.!tmp:~-2!
  set tmp=!tmp:~-8!
  set %1=!tmp: .=0.!
goto :eof


I add small size:

Code: Select all

                             Used      Free       Tot
C: ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄..... [    5.70      2.29      7.99] GB
D: ▄▄.................. [    0.48      4.39      4.87] GB
E: ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄... [   45.35      8.34     53.69] GB
F: ▄▄▄▄▄............... [    1.65      6.35      8.00] GB
G: N/D



Einstein1969

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

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

#17 Post by foxidrive » 18 Nov 2013 21:27

@ einstein.... and aGerman: They work fine here guys in Windows 8.1 32 bit

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

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

#18 Post by bars143 » 18 Nov 2013 23:09

hi @aGerman

in my netbook: dell inspiron, window7 ultimate 32bit ,1gb ram
in my netbook it listed the ff:

c:\ (internal drive)
e:\ (removable drive) --- this one has no external drive inserted.

then using your first code,
will output like this (progress-bar not incl.):

Code: Select all

    C:
size:   232.79GB
used:    34.59GB
free:   198.19GB



then next come popup message like this:

Code: Select all

script error

an error has occurred in the script on this page

line:   1
char:   1
error:  syntax error
code:   0
URL:   vbscript:Execute("CreateObject(""Scripting.FileSystemObject"").GetStandardStream(1).Write(FormatNumber(%3/%GB%, 2) & "" "" & FormatNumber((%3-%2)/%GB%, 2) & "" "" & FormatNumber((-

      do you want to continue running scripts on this page?

                   yes or no


when chosen any one of them -- it will output for an e-drive with same result as of c-drive as shown below:

Code: Select all

    C:
size:   232.79GB
used:    34.59GB
free:   198.19GB

    e:
size:   232.79GB
used:    34.59GB
free:   198.19GB



your next code result is:

Code: Select all


C:\bars>echo(


C:\bars>echo
ECHO is on.

C:\bars>if "" == "" (echo Unable to discov
er the drive properties.  & echo(  & echo(
 goto :eof )
Unable to discover the drive properties.



C:\bars>


can you fix e-drive result -- so that non-existence e-drive(yet listed in exporer) be removed?

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

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

#19 Post by foxidrive » 18 Nov 2013 23:47

Does this also fail?

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
if "%3"=="" (
  rem echo Unable to discover the drive properties.
  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(
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

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

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

#20 Post by bars143 » 19 Nov 2013 00:00

foxidrive wrote:Does this also fail?

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
if "%3"=="" (
  rem echo Unable to discover the drive properties.
  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(
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


@foxi,

above code are working and im looking forward to study your code for identifying non-existence drive . :idea:

thanks for help!
Bars

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

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

#21 Post by foxidrive » 19 Nov 2013 00:16

It's aGerman's code - I just stopped it echoing the message when %3 is empty and it skips that drive.

You probably made a typo when you edited the code originally.

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

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

#22 Post by bars143 » 19 Nov 2013 01:08

foxidrive wrote:It's aGerman's code - I just stopped it echoing the message when %3 is empty and it skips that drive.

You probably made a typo when you edited the code originally.


@foxi,

:oops: and actually its not a typo its a misreading of whole thread,

i found out that the first code should have new script to replace with second script of @aGerman, which start at ":output"

its really working with added echoing of:

"Unable to discover the drive properties."

this could be aGerman final code:

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
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


the output to my netbook is:

Code: Select all


      C:
Size:     232.79 GB
Used:      34.56 GB
Free:     198.23 GB

▒▒▒▒▒▒▒███████████████████████████████████████████



      E:
Unable to discover the drive properties.


Press any key to continue . . .



-------------------------
thanks foxi for verification

Bars

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

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

#23 Post by einstein1969 » 19 Nov 2013 11:00

AiroNG wrote:Hi,
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"?


These lines divide a big number.
The divide to 1GB (1.073.741.824)
The error is very small. I suppose it is less than or equal to 0.01

The last term is extrapolated empirically and non-mathematical (t/2200000). Also, if you wanted to you could implement the Round and have even greater accuracy. Perhaps identical to the result obtained with dedicated tools.

Code: Select all

:ConvertGb retvat Bytes         rem 2decimal from 0,01GB to 102400,00GB (100TB)
  set "tmp=%~200"
  if "!tmp:~9,1!"=="" (set %1=0) else (set /a t=!tmp:~0,-9!, %1=t-t/10+t/30-t/400+t/2000-t/90000+t/2200000)
goto :eof


In fact for this kind of application is not the best solution.
You should go to terabytes and when the number is in the correct range.

but must calculate an appropriate workaround.

If it helps I can modify my code to handle even the TB and maybe something for the future, what's' next?

If it can be useful can expose the method used to generate the division through this series alternating which is nothing a division or reverse for subsequent refinement. But I'm not a mathematician!

I remained difficult to find around the workaround. If someone can post the links would be appreciated.

Einstein1969

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

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

#24 Post by aGerman » 19 Nov 2013 16:33

Last year Aacini provided a nice algorithm. See:
http://www.dostips.com/forum/viewtopic.php?p=15823#p15823

Regards
aGerman

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

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

#25 Post by einstein1969 » 20 Nov 2013 13:24

Thanks aGerman.

Very nice solution.

shayanjameel08
Posts: 10
Joined: 21 Nov 2013 01:07

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

#26 Post by shayanjameel08 » 23 Nov 2013 05:40

I want to write a batch file which gives me free space left in C drive. Any help will be great

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

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

#27 Post by foxidrive » 23 Nov 2013 06:46

Code: Select all

@echo off
for /f "tokens=3" %%a in ('dir c:\ /-p ^|find /i " dir(s) " ') do echo %%a

Post Reply