且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

VBS脚本查找和删除文件

更新时间:2023-12-04 22:42:10

你的方法太复杂了.使用简单的递归函数:

Your approach is much too complicated. Use a simple recursive function:

Option Explicit

Const DeleteReadOnly = True 
Dim oFSO, oDrive, sFileName

Set oFSO   = CreateObject("Scripting.FileSystemObject") 
sFileName  = "date.vbs"

For Each oDrive In oFSO.Drives 
  If oDrive.DriveType = 2 Then Recurse oDrive.RootFolder
Next 

Sub Recurse(oFolder)
  Dim oSubFolder, oFile

  If IsAccessible(oFolder) Then
    For Each oSubFolder In oFolder.SubFolders
     Recurse oSubFolder
    Next 

    For Each oFile In oFolder.Files
      If oFile.Name = sFileName Then
        'oFile.Delete ' or whatever
      End If
    Next 
  End If
End Sub

Function IsAccessible(oFolder)
  On Error Resume Next
  IsAccessible = oFolder.SubFolders.Count >= 0
End Function

要实现不区分大小写的文件名比较,可以使用

To achieve case-insensitive file name comparison, you could use

If StrComp(oFile.Name, sFileName, vbTextCompare) = 0 Then