The problem is this: how to get used hard drive space, and then find out how long it will take to transfer data to a particular USB drive.
I was able to get capacity and free space (see script at bottom)…which I then used to calculate used space.
I needed to convert the used hard drive numbers that looked something like these:
4000000000/800000000 = 5 minutes (which is an estimated 4GB/5 min)
2000000000/800000000 = 2.5 minutes
1000000000/800000000 = 1.25 minutes
They needed to be much shorter, usable numbers for a customer-facing splash screen I was working on.
The minutes reflect how long it takes to copy X amount of files (based upon bytes)
to an external USB drive. Note, I was using the robocopy command.
The idea was to take a user’s used hard drive space (which was going to be backed up)
and divide that by an estimated value (which equates to 1 minute of transfer time per 800000000 bytes [roughly]).
Of course, the returned number looked something like this 15.327642384678236.
My VBScript to drop everything after the decimal looks like this
‘Perform basic division to return time in minutes
strMinutes = strUsedSpace / 8000000000
str = strMinutes
strMinutes = Left(str, InstrRev(str, “.”)-1)
Returning drive capacity and free space to calculate used space
If you want to see how I was returning drive capacity and free space, take a look here:
My Batch File
setlocal enabledelayedexpansion
rem sets drive letter
set DLetter=c
rem returns drive capacity
set count=1
for /f “delims=” %%a in (‘wmic volume where “driveLetter=’%DLetter%:'” get capacity’) do (
if !count!==2 set strCAP=%%a
set /a count+=1
)
rem returns drive freespace
set count=1
for /f “delims=” %%a in (‘wmic volume where “driveLetter=’%DLetter%:'” get freespace’) do (
if !count!==2 set strFS=%%a
set /a count+=1
)
echo %strCAP%
echo %strFS%
echo %strCAP%>capacity.txt
echo %strFS%>freespace.txt
Something important to acknowledge is that the above works for Windows 7. For Windows 7, to return capacity and free space, you would have to use the fsutil command:
set count=1
for /f “tokens=1,2,3,4,5,6 delims= ” %%a in (‘fsutil volume diskfree d:’) do (
if !count!==2 set strCAP=%%f
set /a count+=1
)
ping -n 4 127.0.0.1>nul
cls
Echo Loading Backup Software…
rem returns drive freespace
set count=1
for /f “tokens=1,2,3,4,5,6,7 delims= ” %%a in (‘fsutil volume diskfree d:’) do (
if !count!==1 set strFS=%%g
set /a count+=1
)
echo capacity is %strCAP%
echo freespace is %strFS%
pause
Note, I did start off only using the batch file to do all the calculations, but soon realized that there are severe limitations in calculations where batch files can only handle 32-bit integers.
The error: Invalid Number. Numbers are limited to 32-bits of precision.
The differences are in how negative numbers are parsed, and also how overflow and invalid number errors are handled. It appears there is one set of rules for SET /A, and another set of rules used by all other contexts. To make matters worse, SET /A behavior on XP is different than the more modern Windows versions (Vista onward).
Just remember, if you have really large numbers you want to use in calculations, switch to another language.