1

Ok,I have this code:

echo [%time%] Beginning install
echo [%time%] Checking hard drive C:\
for /f "usebackq tokens=* delims=" %%# in (`"wmic logicaldisk  where name='C:' get  FreeSpace /format:value"`) do (
    for /f "tokens=* delims=" %%a in ("%%#") do @set "%%a"
)
echo [%time%] Available space: %freespace% Bytes
echo [%time%] Required space: 34 Megabytes
@set requiredspace=34
@set /a "available_required=%freespace%-%requiredspace%"
echo [%time%] %available_required%
pause

It finds the available storage on a hard drive and takes away the required to find out if there is enough storage to complete the install, unfortunately, Windows CMD doesn't like numbers higher than 32 bit so I need to cut it down to megabytes, the maths cant be done on the stored variable so It has to be found directly from the command. For example, (Obviously not working though)

"wmic logicaldisk  where name='C:' get  FreeSpace /format:value /division:megabytes"

Thanks

  • 1
    Why didn't you just include this in your previous question? – Squashman Feb 11 '17 at 14:12
  • [Here](http://stackoverflow.com/a/36318398) you can find a pure [tag:batch-file] solution for performing a division (by 1024*1024) of a number exceeding the signed 32-bit integer range... – aschipfl Feb 20 '17 at 07:34

3 Answers3

0

Instead of utilising WMIC, you could leverage powershell for your freespace calculation directly into MegaBytes:

@Echo Off

For /F "Delims=" %%A In ('Powershell "gWMI Win32_LogicalDisk"^
 "-F ""DeviceID='C:'""""|%%{[Math]::Round($_.FreeSpace/1MB,0)}"'
) Do Set "FDS=%%A"
Echo=Freespace on C: is %FDS%MB

Timeout -1

If you were looking for GB to two decimal places then a quick edit of the above could achieve that too:

@Echo Off

For /F "Delims=" %%A In ('Powershell "gWMI Win32_LogicalDisk"^
 "-F ""DeviceID='C:'""""|%%{[Math]::Round($_.FreeSpace/1GB,2)}"'
) Do Set "FDS=%%A"
Echo=Freespace on C: is %FDS%GB

Timeout -1
Compo
  • 36,585
  • 5
  • 27
  • 39
0

Quick-and-dirty:

set "freespace=%freespace:~0,-6%"
echo [%time%] Available space: %freespace% MBytes

Divides freespace by 1,000,000 rather than 1024^2

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

You can pad the values and compare them as strings

set "rSpace=00000000000035651584"
set "fSpace=00000000000000000000%freeSpace%"
if "%rSpace:~-20%" gtr "%fSpace:~-20%" (
    echo [%time%] Not enough space available
) else (
    echo [%time%] Enough Space available
)
MC ND
  • 69,615
  • 8
  • 84
  • 126