How i can delete all files.txt in vbs
如何使用此代码删除扩展名为.txt的所有文件我的PC中的所有文本文件*TXT
有人帮我重新写这段代码吗
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | Option Explicit Const DeleteReadOnly = True Dim oFSO, oDrive, sFileName Set oFSO = CreateObject("Scripting.FileSystemObject") sFileName ="g.txt" 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 |
添加一个if块检查扩展名,如果扩展名是txt,则删除扩展名。
1 2 3 4 5 6 7 8 9 | Option Explicit Dim oDrive For Each oDrive In CreateObject("Scripting.FileSystemObject").Drives If oDrive.DriveType = 2 Then CreateObject("WScript.Shell").Run"cmd /c del /s /q" & Chr(34) & oDrive.RootFolder.Path &"*.txt" & Chr(34), 0, True End If Next |
您可以修改脚本以执行扩展检查:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | Option Explicit Const DeleteReadOnly = True Dim oFSO, oDrive, sFileExt Set oFSO = CreateObject("Scripting.FileSystemObject") sFileExt ="txt" 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 lcase(oFSO.GetExtensionName(oFile)) = lcase(sFileExt) 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 |