Jump to content

Script/Cmd , Freespace


Recommended Posts

Hi all !

... sorry for my bad english as always ...

I'm stuck in a batch

I would like to get free space of an hard disk and execute command if size is less than 200Gb for example ...

so there is the script for the moment ...

the problem is "set" command cannot work with more than 9 digits numbers and fsutil return freespace in octets ...

if someone has a tip ! thanks in advance !

(note: the script must work in xp and win7 , without wmic ou vbs would be nice too ;-) )


set dossier=C:
for /f "tokens=7" %%? in ('fsutil volume diskfree %dossier%') do set /a espacelibre=%%? & goto suite1
:suite1
echo %espacelibre%
if %espacelibre%<=200 echo less than 200Gb
pause

Link to comment
Share on other sites


I must admit I didd't study your code much but I have a code of my own which I use from time to time

It works with the folowing parameters:

%1 - drive path [ letter:\ ]

%2 - size (in gb) to check against

:checkds
for /f "tokens=4,5 delims= " %%a in ('defrag.exe /a %1^|findstr.exe /r "Free"') do (
set _unit=%%b
set _fspace=%%a
)
for /f "tokens=1 delims=." %%a in ("%_fspace%") do (set _sspace=%%a)
if "%_unit%" equ "GB" (
call :checkds_checksize %2
) else (
echo "The available hard disk space is insufficient."
)
goto :eof

:checkds_checksize
if %_sspace% lss %1 (
echo "The available hard disk space is insufficient."
)
goto :eof

Link to comment
Share on other sites

if you're just trying to estimate anyway....

@echo off

@SETLOCAL ENABLEEXTENSIONS
@SETLOCAL ENABLEDELAYEDEXPANSION

set dossier=C:

for /f "tokens=7" %%? in ('fsutil volume diskfree %dossier%') do set raw=%%? & goto suite1

:suite1
set free=!raw:~0,-10!
if %free% lss 200 echo "less than 200Gb (%free%GB estimated free)"
if %free% gtr 200 echo "Greater than 200Gb (%free%GB estimated free)"
pause
exit

Edited by iamtheky
Link to comment
Share on other sites

I know he did not want a VBS script, but I thought I would post a VBS script that list Drives Under And Over 200 GB.

Save As DrvFreeSpace.vbs


Const GB = 1073741824
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim A1, A2, Free, Drv, Obj
For Each Obj In Fso.Drives
If Obj.IsReady Then
Free = FormatNumber(Obj.FreeSpace/GB,2)
Drv = Obj.DriveLetter & ":\"
If Obj.FreeSpace < 214748364800 Then
A1 = A1 & vbCrLf & "Under 200 GB" & vbTab & Drv & vbTab & Free & " GB"
Else
A2 = A2 & vbCrLf & "Over 200 GB " & vbTab & Drv & vbTab & Free & " GB"
End If
End If
Next
MsgBox "Drive Free Space Report" & vbCrLf & A1 & A2,4128,"Drive Report"

post-5386-0-92889400-1323457909_thumb.pn

Link to comment
Share on other sites

wow ! always good members here ! fast !

thanks all !!

thanks for defrag tip wich is display in GB directly ... but a bit slow due to analyse

thanks iamtheky,

i'm focused to "set /a" to set my variable but with "set" and shrink it that's perfect,

that was the solution wich is my head but doesn't want to go out ;-)

Link to comment
Share on other sites

without wmic ou vbs would be nice too

wmic/vbscript/powershell at least wouldn't require both administrative privileges and also being elevated to even run... It would also make it easier to filter by drive type (e.g. not running against optical disks, removable devices like USB keys or mapped drives), or logging/reporting and so on. It would also be a LOT more reliable than a batch hack job (iamtheky's batch doesn't even run here, I just get an error message: "200 was unexpected at this time."). Your loss.

Link to comment
Share on other sites

than a batch hack job

sorry thats the only kind of batch jobs i do :thumbup

the solution i use regularly is autoit + WINAPIEx, but many many others will beat this batch file with a stick.

I am interested into why the batch fails on your system, as i didnt think reliability would be the concern so much as granularity. Is it an x64 difference/issue?

Link to comment
Share on other sites

sorry thats the only kind of batch jobs i do :thumbup

It's ok. I don't do batch stuff anymore myself. I wasn't so much picking at your particular batch file, I'm just saying that doing such things isn't quite as reliable as newer ways.

I am interested into why the batch fails on your system, as i didnt think reliability would be the concern so much as granularity. Is it an x64 difference/issue?

No idea. Plain vanilla Win 7 x64 install. Then again, fsutil won't run if you're not an administrator, nor will it run without being ran elevated (UAC popup and all). Those are far more serious issues IMO (at least if you're going to run it on an OS that is less than a decade old)

Link to comment
Share on other sites

Here is a better VBS script that uses Wmi to Defrag All Local Hard Drives without 200 GB filter

Save As Wmi_DefragAll.vbs

How To Use

1:\ Open Cmd Promt As Admin

2:\ Type in CD Path_To_VBS_Script

3:\ Type in Cscript Wmi_DefragAll.vbs


Const GB = 1073741824
Dim Wmi :Set Wmi = GetObject("winmgmts:" & _
"{impersonationLevel=impersonate}!\\.\root\cimv2")
Dim Dfg, Dsk, errResult, Free, Size, Used, vB :vB = vbCrLf
For Each Dsk In Wmi.ExecQuery("Select * from Win32_LogicalDisk")
If Dsk.DriveType = 3 Then
Free = FormatNumber(Dsk.FreeSpace/GB,2)
Size = FormatNumber(Dsk.Size/GB,2)
Used = FormatNumber(Size - Free)
For Each Dfg in Wmi.ExecQuery("Select * From Win32_Volume" & _
" Where Name = '" & Dsk.DeviceID & "\\'")
WScript.StdOut.WriteLine _
"Defraging : " & Dsk.DeviceID & vB & _
"Disk Size : " & Size & " GB" & vB & _
"Disk Free : " & Free & " GB" & vB & _
"Disk Used : " & Used & " GB" & vB & _
"Volume Name : " & Dsk.VolumeName
errResult = Dfg.Defrag()
WScript.StdOut.WriteLine "Defrag Finished"
WScript.StdOut.WriteBlankLines 2
Next
End If
Next
WScript.StdOut.WriteLine "Defrag All Local Drives Completed"

post-5386-0-49958100-1323498543_thumb.pn

Rename Wmi_DefragAll.vbs.txt to Wmi_DefragAll.vbs to make active

Wmi_DefragAll.vbs.txt

Link to comment
Share on other sites

for me I would delete the oldest backup if freesize become too small ... running batch hourly/daily ...

That's really easy to do but we don't know where the backup is (what files to delete or what backup program you're talking about). I typically use PowerShell for such automated tasks, but that's an optional download on older OS'es like XP.

A quick example of how one can get a list of drives with < 200GB free in powershell:

gwmi Win32_LogicalDisk -filter "DriveType=3" | ? {$_.FreeSpace -lt 200GB}

gwmi is an alias for Get-WmiObject which gets WMI objects, sort of like wmic does. I ask it to list instances of Win32_LogicalDisk. The -filter "DriveType=3" part tells it to filter the results based on the DriveType, 3 being "Local Disk". The | character pipes the output of that to the next part on the right. ? is an alias for where, which here I use to filter yet again, this time on the FreeSpace property being less than (-lt) 200GB.

Link to comment
Share on other sites


@echo off
SetLocal EnableDelayedExpansion
set count=0
for /F "delims=" %%f in ('"wmic LogicalDisk where drivetype=3 get freespace"') do (
set Freespace=%%f
set /a count=!count! + 1
if !count! GTR 1 goto exit
)
:exit
if %Freespace% GTR 214748364800 Echo %Freespace%
pause

Edited by mchipser
Link to comment
Share on other sites

I think I found what your looking for

@echo off
setlocal enabledelayedexpansion

call :checkdiskspace
echo %_tgbytes%
pause

:checkdiskspace
for /f "tokens=2 delims=:" %%a in ('fsutil volume diskfree h:^|findstr /ir [0-9]*') do set _tbytes=%%a
set _tbytes=%_tbytes: =%
for /f %%a in ('str_math.bat %_tbytes% / 1073741824') do set _tgbytes=%%a
for /f "tokens=1 delims=." %%a in ("%_tgbytes%") do set _tgbytes=%%a
goto :eof

And what you were missing is:

LARGE NUMBERS BATCH

See if it helps.

Link to comment
Share on other sites

<snip />
if %Freespace% GTR 214748364800 Echo %Freespace%
<snip />

Have you tried that, I was sure that my previous venture into using a similar method failed because the number of bytes is too large for the GTR / LSS to be able to solve. Try breaking the figures down into its smaller components.

Here's a 200Gb example for drive C:

@ECHO OFF &SETLOCAL ENABLEEXTENSIONS
FOR /F "TOKENS=3" %%# IN ('DIR C: /-P 2^>NUL') DO SET "SIZE=%%#"
FOR /F "TOKENS=1-4 DELIMS=," %%a IN ("%SIZE%") DO (IF "%%d"=="" (GOTO LO)
SET "gb=%%a" &SET "mb=%%b" &SET "kb=%%c" &SET "by=%%d")
IF %gb%%mb% LSS 214748 (GOTO LO)
IF %gb%%mb% EQU 214748 (IF %kb%%by% LEQ 364800 (GOTO LO))
GOTO HI
:LO
ECHO=Drive has insufficient free space
PAUSE &GOTO :EOF
:HI
ECHO=Drive has sufficient free space
PAUSE

Link to comment
Share on other sites

Have you tried that, I was sure that my previous venture into using a similar method failed because the number of bytes is too large for the GTR / LSS to be able to solve. Try breaking the figures down into its smaller components.

I tried it but i didnt have enough free space so i thought it worked.. i guess i need to try it on a drive with GTR then 200GB :(

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...