VBScript - 删除子文件夹

时间:2016-08-06 16:29:29

标签: vbscript

我环顾四周,我能找到的所有示例都非常通用,我只需要代码从Temporary Internet Files文件夹中随机命名的子文件夹中删除单个.json文件。

目前我每次运行VBScript时都会下载一个非常小的文件,但是它似乎将该文件下载到Temp Internet Files,并且在每次后续运行时都会从那里获取它而不是Internet。这是一个必须始终是新的文件。

如何搜索Temp Internet Files中的所有子文件夹并删除伪造[1] .json? AND,删除它的父文件夹?

对于dbmitch:

'  DELETE CACHED FORGE FILE  ------------------------ '
Function DelFiles()

    look_subfolders UserProfile & "AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\"

    Set Folder = objFso.GetFolder ( fold )

Sub look_subfolders ( fold )

    For Each objFile in Folder.Files
       If objFile.Name = "forge[1].json" Then
          objFile.Delete
    End If
       Next

'look into subfolders:
    For Each Subfolder in Folder.SubFolders
        look_subfolders Subfolder.Path
    Next
End Sub 

End Function

Set objFso = CreateObject( "Scripting.FileSystemObject" )在脚本顶部的其他位置定义(适用于我使用过的所有其他对象)。

我也试过这个......

Path = UserProfile & "AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5"

Set objFolder = objFso.GetFolder(Path)
Set colFiles = objFolder.Files

For Each objFile in colFiles
   If objFile.Name = "forge[1].json" Then
        objFile.Delete
End If
  Next

For Each objFile In objFolder.SubFolders    
   If objFile.Name = "forge[1].json" Then
        objFile.Delete
   End if
 Next

它会从主文件夹中删除该文件,但不会深入研究任何子文件夹。

1 个答案:

答案 0 :(得分:2)

试试这个,它是一个递归调用:

Set fs = CreateObject("Scripting.FileSystemObject")

look_subfolders "C:\windows\temp" 'change the path to your temp folder

Sub look_subfolders(fold)
    Set Folder = fs.GetFolder(fold)

    'folder files:
    For Each objFile in Folder.Files
        If objFile.Name = "forge[1].json" Then
            objFile.Delete
            msgbox "File found and deleted" 'message to confirm deletion
            WScript Quit
        End If
    Next

    'look into subfolders:
    For Each Subfolder in Folder.SubFolders
        look_subfolders Subfolder.Path
    Next
End Sub