Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file to determine free space  (Read 10834 times)

0 Members and 1 Guest are viewing this topic.

MrDeepFreeze

    Topic Starter


    Newbie

    • Experience: Familiar
    • OS: Windows 7
    Batch file to determine free space
    « on: December 02, 2013, 12:58:55 PM »
    I'd like to create a batch file that completes a DIR command and copies this into a text file like this...

    DIR > Test.txt

    Then I'd like to strip all the information out of the text file except for the last line where it shows bytes free.  I want only the number in that last line.
    I'd like for the bytes free number to be set as a variable and then use the set command to divide this number by 1073742268 to return the free space in GB to yet another variable.

    There may be an easier way to do this without sending this to an actual text file but I've tried different methods out there and none of them work when trying to determine the free space inside a TrueCrypt volume.  I can do this already via WMIC on my C drive, D drive, etc. but not on my G drive which is a TrueCrypt volume.  I can however run a DIR on G which returns the output you would expect. 

    My goal in sending this to a text file and stripping out the unnecessary strings would not only solve my current need to determine free space within my TrueCrypt volume but it would also help me learn how to strip text files of unnecessary text which I could use in other situations. 

    Thanks.

    Salmon Trout

    • Guest
    Re: Batch file to determine free space
    « Reply #1 on: December 02, 2013, 02:00:21 PM »
    You can filter out the 'bytes free' line of DIR output using FIND

    dir | find "bytes free"

    You can parse that line using FOR with the /F switch and separate the 3rd space-delimited token (which is the number of bytes free) and assign it to a variable. In the FOR dataset you need to escape the pipe symbol with a caret

    for /f "tokens=1-3 delims= " %%A in ('dir ^| find "bytes free"') do set bytesfree=%%C

    You now have the bytes free held in the variable %bytesfree% BUT you cannot use SET /A to divide it if the bytes free amount to 2 GB or more because batch arithmetic is limited to 32 bits of precision (i.e integers between -2,147,483,647 and + 2,147,483,647)

    However you can use the Visual Basic Script eval() function which is not limited in this way. You can create a simple one-line VBScript, pass the calculation to it and use FOR /F to parse the results.

    Wscript.echo eval(WScript.Arguments(0))

    You may find a large number of digits after the decimal point like so...

    21.4063174422784

    so you can use another VBScript oneliner to format the number, adding a leading zero if required (for numbers less than 1 GB) and parse the output with FOR /F again

    Putting it all together....

    @echo off
    Set Drive=D:
    for /f "tokens=1-3 delims= " %%A in ('dir %Drive% ^| find "bytes free"') do set bytesfree=%%C
    echo Wscript.echo eval(WScript.Arguments(0)) > Calculate.vbs
    for /f "delims=" %%A in ('cscript //nologo calculate.vbs %bytesfree%/1073742268') do set GBfreeRaw=%%A
    REM How many digits after the decimal point
    set decimals=2
    REM If you don't want a leading zero if GB < 1 then change this to False
    Set LeadingZero=True
    echo Wscript.echo Formatnumber(WScript.Arguments(0), %decimals%, %LeadingZero%) > Nformat.vbs
    for /f "delims=" %%A in ('cscript //nologo NFormat.vbs %GBfreeRaw%') do set GBfreeFormatted=%%A
    echo %Drive% GB free: %GBfreeFormatted%


    Example output

    D: GB free: 128.34










    « Last Edit: December 02, 2013, 02:42:35 PM by Salmon Trout »

    Lemonilla



      Apprentice

    • "Too sweet"
    • Thanked: 70
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: Batch file to determine free space
    « Reply #2 on: December 02, 2013, 05:17:50 PM »
    Don't forget to clean up your .vbs scrips after Salmon Trout's code with
    Code: [Select]
    del Nformat.vbs
    del Calculate.vbs

    Otherwise you end up with .vbs files floating around in your folders which could cause errors in future scripts.
    Quote from: patio
    God Bless the DOS Helpers...
    Quote
    If it compiles, send the files.

    Salmon Trout

    • Guest
    Re: Batch file to determine free space
    « Reply #3 on: December 03, 2013, 10:55:38 AM »
    Tidied up; pass the drive letter as a parameter; cleans up after itself; only one vbs; uses temp folder for vbs; right-justifies output.

    @echo off
    for /f "tokens=1-3 delims= " %%A in ('dir %1: ^| find "bytes free"') do set bytesfree=%%C
    echo Wscript.echo Formatnumber(eval(WScript.Arguments(0)/1073742268), 2, True,, False) > "%temp%\Calculate.vbs"
    for /f "delims=" %%A in ('cscript //nologo "%temp%\Calculate.vbs" %bytesfree%') do set GBfree=%%A & del "%temp%\Calculate.vbs"
    set "GBFree=                %GBFRee%"
    set GBFree=%GBFRee:~-12%
    echo %1 %GBfree% GB free


    example

    C:\>for %A in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do @if exist %A:\ bytesfree.bat %A
    A        3.54  GB free
    C       21.43  GB free
    D      128.34  GB free
    F      182.86  GB free
    H      206.28  GB free
    I      348.84  GB free
    V     1388.91  GB free
    W        5.99  GB free

    « Last Edit: December 03, 2013, 11:32:53 AM by Salmon Trout »

    DRF



      Newbie

      • Experience: Beginner
      • OS: Windows 10
      Re: Batch file to determine free space
      « Reply #4 on: June 12, 2018, 01:29:49 AM »
      To make this work in windows 10 you have to add these lines to the script to take the commas out of the bytes free number returned by the dir find command
      add these lines after the line "for /f "tokens=1-3 delims= " %%A in ('dir %Drive% ^| find "bytes free"') do set bytesfree=%%C"

      set bf=%bytesfree:,=%
      set bytesfree=%bf%



      patio

      • Moderator


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Re: Batch file to determine free space
      « Reply #5 on: June 12, 2018, 05:43:38 AM »
      They haven't been back in 5 years...
      " Anyone who goes to a psychiatrist should have his head examined. "

      patio

      • Moderator


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Re: Batch file to determine free space
      « Reply #6 on: October 08, 2018, 03:18:36 PM »
      Did you see my Post above yours ? ?
      " Anyone who goes to a psychiatrist should have his head examined. "