1

I'm new to bat scripting and I have following requirement to meet:

If Dir eg. C:\test size exceed 50GB, then the script should delete the oldest files until the size of directory goes back to 50GB or little less.

I have modified the below script I found on Get size of a directory in 'MB' using batch file but I'm not sure how to proceed with the remaining requirements. See the small modifications I have done so far and I would appreciate any input:

@echo off
set limit="50.0"
set Folder="C:\test"
echo The size of %Folder% is 
Call :GetSize %Folder%
IF /I "Call :GetSize %Folder%" GEQ "50.0" Echo Overlimit
<Here I would like to delete oldest zip files until the folder size is back to <=50.0GB>


pause
:GetSize
(
echo wscript.echo GetSize("%~1"^)
echo Function GetSize(MyFolder^)
echo    Set fso = CreateObject("Scripting.FileSystemObject"^)
echo    Set objFolder= fso.GetFolder(MyFolder^)  
echo    GetSize = FormatSize(objFolder.Size^)
echo End Function
echo '*******************************************************************
echo 'Function to format a number into typical size scales
echo Function FormatSize(iSize^)
echo    aLabel = Array("bytes", "KB", "MB", "GB", "TB"^)
echo    For i = 0 to 4
echo        If iSize ^> 1024 Then
echo            iSize = iSize / 1024
echo        Else
echo            Exit For
echo        End If
echo    Next
echo    FormatSize = Round(iSize,2^)
echo End Function
echo '*******************************************************************
)>%tmp%\Size.vbs
Cscript /NoLogo %tmp%\Size.vbs
Del %tmp%\Size.vbs
Exit /b
Ali
  • 35
  • 1
  • 7

1 Answers1

1

This seems to work on my computer.

Set fso = CreateObject("Scripting.FileSystemObject")
Set F = fso.GetFolder("C:\Users\User\Desktop\New Folder\Stories\Test")

If F.size > 2^30*2 Then

            'Comments on a stupid editor that can't handle tabs
            'Creating an in memory disconnected recordset to sort files by date
        Set rs = CreateObject("ADODB.Recordset")
        With rs
            .Fields.Append "Date", 7 
            .Fields.Append "Txt", 201, 5000 
            .Open

            For Each Thing in f.files
                .AddNew
                .Fields("Date").value = thing.datelastmodified
                .Fields("Txt").value = thing.path
                .UpDate
            Next
            .Sort = "Date Desc"
            Do While not .EOF
                fso.deletefile  .Fields("Txt").Value
                If f.size < 2^30*2 then Exit Do
                .MoveNext
            Loop
        End With
End If

PS 2^30 = 1 GiByte or 1 Gig.

ACatInLove
  • 530
  • 2
  • 5
  • Is this VB or bat script? – Ali Jan 09 '18 at 12:05
  • vbscript, like your program. – ACatInLove Jan 09 '18 at 12:12
  • Works great, is there a possibility to run this script from within my @ existing bat script? I mean I have a bat script that is zipping all files every day and then delete the zipped files are older than 15 days. This was the first requirement followed by if the folder size increase to more than 50GB then the oldest files should be deleted until it reduces to threshold. I would like to do all from one script. – Ali Jan 09 '18 at 13:25
  • Put echo before each line and escape brackets as you do with your existing vbs. Also `CreateObject("Wscript.Shell").Run "Program to run.exe", true` – ACatInLove Jan 09 '18 at 22:52
  • This solution works great for 1 folder with many files (thank you!). Follow up question, could this vbs script be expanded to start removing files in the oldest subfolders first? Or is that asking too much? :) – user225479 Feb 16 '20 at 12:35