1.

Solve : Batch file to determine free space?

Answer»

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










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


They haven't been back in 5 years...Did you see my Post above yours ? ?



Discussion

No Comment Found