如果超过N GB,Windows bat脚本将获取目录大小,然后删除最旧的文件以恢复空间

时间:2018-01-08 23:28:08

标签: batch-file size directory

我是蝙蝠脚本的新手,我有以下要求见面:

如果Dir例如。 C:\测试大小超过50GB,然后脚本应删除最旧的文件,直到目录大小恢复到50GB或更少。

我修改了我在Get size of a directory in 'MB' using batch file上找到的以下脚本,但我不确定如何继续执行其余要求。看看到目前为止我做过的小修改,我将不胜感激:

@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

1 个答案:

答案 0 :(得分:0)

这似乎适用于我的电脑。

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或1 Gig。