如果我使用VBScript进行了新的备份后,该文件夹中有15个以上的文件,该如何删除备份文件夹中最旧的文件



如果我使用vbscript进行了新的备份后,该文件夹中有15个以上的文件,该如何删除备份文件夹中最旧的文件?

我发现我的备份占用了我的HDD上的足够空间

是否可以通过计算文件夹中的文件数来进行操作?我的备份名为" ST-06.02.18 07H20.ZIP"。如果名称使它变得更容易... XD

我总是可以更改名称。
Dim objFSO, objFolder, strDirectory, dNow, yy, mt, dd, hh, nn, objShell, dOpen
Set objFSO = CreateObject("Scripting.FileSystemObject")
strDirectory = "c:test"
If objFSO.FolderExists(strDirectory) Then
   Set objFolder = objFSO.GetFolder(strDirectory)
   WScript.Echo strDirectory & " already created "
Else
   Set objFolder = objFSO.CreateFolder(strDirectory)
   WScript.Echo "Just created " & strDirectory
End If

dNow = Now
yy = Right(Year(dNow), 2)
mt = Right("00" &Month(dNow), 2)
dd = Right("00" &Day(dNow), 2)
hh = Right("00" &Hour(dNow), 2)
nn = Right("00" &Minute(dNow), 2)
ss = Right("00" &Second(dNow), 2)

Compress "C:Program FilesSTDb" ,strDirectory & "ST-" &dd & "." &mt & "." &yy & " " &hh & "h" &nn &".zip"
Sub Compress(Input, ZipFile)
Dim Shell : Set Shell = CreateObject("Shell.Application")
Dim FSO : set FSO = CreateObject("Scripting.fileSystemObject")
FSO.CreateTextFile(ZipFile, true).WriteLine "PK" & Chr(5) & Chr(6) & String(18, 0)
Set ZipFile = Shell.NameSpace(ZipFile) 
ZipFile.CopyHere Input
Do Until ZipFile.items.Count = 1 
'important, makes the script not fall out and dispose of objects before they are done
'items.count is the amount of root items you anticipate to be in the zip file
        wscript.sleep 200
Loop
Set Shell = Nothing
Set FSO = Nothing
Set ZipFile = Nothing
End Sub
Set objShell = CreateObject("Wscript.Shell")
dOpen = "explorer.exe /e," & strDirectory
objShell.Run  dOpen
Set fso = CreateObject("Scripting.FileSystemObject")
Set F = fso.GetFolder("C:UsersDavid CandyDesktopNew FolderStoriesTest")
If F.size > 2^30 Then
        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
                msgbox f.size
                If f.size < 2^30 then Exit Do
                .MoveNext
            Loop
        End With
End If

当文件夹大于2 gig并删除最古老的文件直到2 gig以下时,此示例代码将运行。

它使用在内存中创建的断开记录集来对最后修改的文件进行排序。

最新更新