不要删除文件夹,直到文件夹中的所有文件都超过24小时

时间:2016-10-16 22:21:04

标签: windows vbscript

我的vsb文件有问题。我正在尝试创建一个删除超过24小时的所有文件和文件夹的脚本,但它不应删除目录,直到其中的所有文件都超过24小时。我的脚本的问题是它删除所有目录,即使其中的文件小于24小时。我无法真正找到问题的解决方案,我在谷歌搜索并没有任何帮助。 这是我的剧本:

    Const strPath = "D:\shared\temp"
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Call Search (strPath)
Sub Search(str)
 Dim objFolder, objSubFolder, objFile
 Set objFolder = objFSO.GetFolder(str)
 For Each objFile In objFolder.Files
  If objFile.DateCreated < (Now() - 1) Then
   objFile.Delete(True)
  End If
 Next
 For Each objSubFolder In objFolder.Subfolders 
  Flag = ""
  If objSubFolder.DateCreated < (Now() - 1) Then
   For Each Thing in objSubFolder
    If thing.DateCreated > Now() - 1 then Flag="yes"
   Next
   If Flag = "yes" then objSubFolder.Delete(True)
  End If
 Next
End Sub

如果有人在这里知道我可以在我的脚本中更改以使其工作,我会非常感谢帮助。

2 个答案:

答案 0 :(得分:0)

For Each objSubFolder In objFolder.Subfolders
    Flag = ""   
    If objSubFolder.DateCreated < (Now() - 1) Then
        For Each Thing in objSubFolder
            If thing.DateCreated > Now() - 1 then Flag="yes"
        Next
        If Flag = "yes" then objSubFolder.Delete(True)
    End If
Next

答案 1 :(得分:0)

如果您只删除早于指定限制的文件,并且只有在内部所有文件与上一个条件匹配时才删除文件夹,请首先删除匹配的文件,然后仅在文件夹为空时删除文件夹。

Option Explicit

Dim strPath
    strPath = "d:\shared\temp"

    Call removeOldFiles( strPath, DateAdd("h", -24, Now()), False )

Sub removeOldFiles( ByVal currentFolder, timeLimit, deleteFolder )
    ' Retrieve a reference to currentFolder if it is not a FSO.Folder
    If TypeName( currentFolder ) <> "Folder" Then 
        With WScript.CreateObject("Scripting.FileSystemObject")
            If .FolderExists( currentFolder ) Then 
                Set currentFolder = .GetFolder( currentFolder )
            Else 
                Exit Sub
            End If 
        End With
    End If

    ' Remove files older than timeLimit
    Dim oFile    
    For Each oFile In currentFolder.Files 
        If oFile.DateCreated < timeLimit Then 
            Call oFile.Delete( True )
        End If
    Next 

    ' Recursive call to clean each subfolder
    Dim oSubFolder    
    For Each oSubFolder In currentFolder.Subfolders
        Call removeOldFiles( oSubFolder, timeLimit, True )
    Next 

    ' If the folder is old enough and it is empty, remove it
    If  currentFolder.DateCreated < timeLimit _ 
        And currentFolder.Files.Count = 0 _ 
        And currentFolder.SubFolders.Count = 0 _ 
        And deleteFolder _ 
    Then 
        Call currentFolder.Delete( True )
    End If
End Sub 

如果您需要保留所有文件/文件夹,直到所有文件/文件夹都更旧,然后全部删除,那么您需要先检查每一个

Option Explicit

Dim strPath
    strPath = "d:\shared\temp"

    Call removeOldFolder( strPath, DateAdd("h", -24, Now()) )

Sub removeOldFolder( ByVal currentFolder, timeLimit )
    If recurseCheckOldData( currentFolder, timeLimit ) Then 
        Call currentFolder.Delete( True )
    End If 
End Sub

Private Function recurseCheckOldData( ByRef currentFolder, timeLimit )
    ' Until everything is checked, the data is considered newer than timeLimit
    recurseCheckOldData = False

    ' Retrieve a reference to currentFolder if it is not a FSO.Folder
    If TypeName( currentFolder ) <> "Folder" Then 
        With WScript.CreateObject("Scripting.FileSystemObject")
            If .FolderExists( currentFolder ) Then 
                Set currentFolder = .GetFolder( currentFolder )
            Else 
                Exit Function
            End If 
        End With
    End If

    ' Check current folder time
    If  currentFolder.DateCreated > timeLimit Then 
        Exit Function
    End If 

    ' Check current folder files
    Dim oFile    
    For Each oFile In currentFolder.Files 
        If oFile.DateCreated > timeLimit Then 
            Exit Function
        End If
    Next 

    ' Recursive call to check each subfolder
    Dim oSubFolder
    For Each oSubFolder In currentFolder.Subfolders
        If Not recurseCheckOldData( oSubFolder, timeLimit ) Then 
            Exit Function
        End If 
    Next 

    ' Up to now everything is older than the indicated time
    recurseCheckOldData = True
End Function
相关问题