1
votes

I've looked around and all examples I can find are very general purpose, I simply need code to delete a single .json file from a randomly named subfolder within the Temporary Internet Files folder.

Currently I am downloading a very small file each time my VBScript is ran, however it seems to download that file to Temp Internet Files and on each subsequent run, grabs it from there instead of the Internet. This is a file that must always be new.

How can I search through all sub-folders within Temp Internet Files and delete forge[1].json? AND, delete it's parent folder?

For 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" ) is defined elsewhere at the top of the script (which has worked for every other object I've used).

I also tried this too...

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

It deletes the file from the main folder, but doesn't delve into any subfolders.

1
You can't nest Subs in VBScript. So clean up your code layout. - Ekkehard.Horner
Not my code, however I kinda had a feeling it was relating to the sub, and so I tried to just run the code without sub but it doesn't actually delete the file I need deleting. - Bolte
I added some other code I was toying with when trying to do it without a Sub. - Bolte

1 Answers

2
votes

Try this, it's a recursive call:

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