如何使用此代码删除我的P.C *.txt中所有扩展名为.txt的所有文件和所有文本文件
有人能帮我重写这段代码吗?
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发布于 2013-12-27 06:59:40
添加IF块检查扩展名,如果是txt则删除。
If oFSO.GetExtensionName(oFile) = "txt" Then
发布于 2013-12-27 07:37:59
您可以修改脚本以执行扩展检查:
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发布于 2013-12-29 01:38:59
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 https://stackoverflow.com/questions/20789801
复制相似问题