Sonic Posted December 9, 2011 Posted December 9, 2011 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:suite1echo %espacelibre%if %espacelibre%<=200 echo less than 200Gbpause
Ulaiphur Posted December 9, 2011 Posted December 9, 2011 I must admit I didd't study your code much but I have a code of my own which I use from time to timeIt works with the folowing parameters: %1 - drive path [ letter:\ ] %2 - size (in gb) to check against:checkdsfor /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_checksizeif %_sspace% lss %1 ( echo "The available hard disk space is insufficient.")goto :eof
iamtheky Posted December 9, 2011 Posted December 9, 2011 (edited) 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:suite1set 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)"pauseexit Edited December 9, 2011 by iamtheky
gunsmokingman Posted December 9, 2011 Posted December 9, 2011 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.vbsConst GB = 1073741824Dim 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"
Sonic Posted December 9, 2011 Author Posted December 9, 2011 wow ! always good members here ! fast !thanks all !!thanks for defrag tip wich is display in GB directly ... but a bit slow due to analysethanks 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 ;-)
CoffeeFiend Posted December 9, 2011 Posted December 9, 2011 without wmic ou vbs would be nice toowmic/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.
iamtheky Posted December 9, 2011 Posted December 9, 2011 than a batch hack jobsorry thats the only kind of batch jobs i do 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?
CoffeeFiend Posted December 9, 2011 Posted December 9, 2011 sorry thats the only kind of batch jobs i do 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)
gunsmokingman Posted December 10, 2011 Posted December 10, 2011 Here is a better VBS script that uses Wmi to Defrag All Local Hard Drives without 200 GB filterSave As Wmi_DefragAll.vbsHow To Use1:\ Open Cmd Promt As Admin2:\ Type in CD Path_To_VBS_Script 3:\ Type in Cscript Wmi_DefragAll.vbsConst 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"Rename Wmi_DefragAll.vbs.txt to Wmi_DefragAll.vbs to make activeWmi_DefragAll.vbs.txt
Sonic Posted December 10, 2011 Author Posted December 10, 2011 for me I would delete the oldest backup if freesize become too small ... running batch hourly/daily ...
CoffeeFiend Posted December 10, 2011 Posted December 10, 2011 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.
mchipser Posted December 12, 2011 Posted December 12, 2011 (edited) @echo offSetLocal EnableDelayedExpansionset count=0for /F "delims=" %%f in ('"wmic LogicalDisk where drivetype=3 get freespace"') do (set Freespace=%%fset /a count=!count! + 1if !count! GTR 1 goto exit):exitif %Freespace% GTR 214748364800 Echo %Freespace%pause Edited December 12, 2011 by mchipser
Ulaiphur Posted December 12, 2011 Posted December 12, 2011 I think I found what your looking for@echo offsetlocal enabledelayedexpansioncall :checkdiskspaceecho %_tgbytes%pause:checkdiskspacefor /f "tokens=2 delims=:" %%a in ('fsutil volume diskfree h:^|findstr /ir [0-9]*') do set _tbytes=%%aset _tbytes=%_tbytes: =%for /f %%a in ('str_math.bat %_tbytes% / 1073741824') do set _tgbytes=%%afor /f "tokens=1 delims=." %%a in ("%_tgbytes%") do set _tgbytes=%%agoto :eofAnd what you were missing is:LARGE NUMBERS BATCHSee if it helps.
Yzöwl Posted December 12, 2011 Posted December 12, 2011 <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 ENABLEEXTENSIONSFOR /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:LOECHO=Drive has insufficient free spacePAUSE &GOTO :EOF:HIECHO=Drive has sufficient free spacePAUSE
mchipser Posted December 13, 2011 Posted December 13, 2011 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
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now