Vbscript to delete files in unknown directories

Good morning

I'm completely new to the vbscripting and we need a script to delete a certain files which is exact path is:
D:\Documents and Settings\jntakaile\Application Data\Mozilla\Firefox\Profiles\3w7is70x.default\

but we need to go through all the user profiles on the machine and the .default folder at the end of the path is also different on each user profile

the files to delete is Pluginreg.dat and Blocklist.xml.

i have found this but not sure how to change it

Dim fso, startFolder, OlderThanDate

Set fso = CreateObject("Scripting.FileSystemObject")
startFolder = "D:\Documents and Settings\%" ' folder to start deleting (subfolders will also be cleaned)
OlderThanDate = DateAdd("d", -2, Date)  ' 30 days (adjust as necessary)

DeleteOldFiles startFolder, OlderThanDate

Function DeleteOldFiles(folderName, BeforeDate)
   Dim folder, file, fileCollection, folderCollection, subFolder

   Set folder = fso.GetFolder(folderName)
   Set fileCollection = folder.Files
   For Each file In fileCollection
      If file.DateLastModified < BeforeDate Then
         fso.DeleteFile(file.Path)
      End If
   Next

    Set folderCollection = folder.SubFolders
    For Each subFolder In folderCollection
       DeleteOldFiles subFolder.Path, BeforeDate
    Next
End Function
ablsysadminAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Meir RivkinFull stack Software EngineerCommented:
if u got the exact path all u need is to get all users under D:\Documents and Settings\, and do find replace in the  this path:

"D:\Documents and Settings\<user>\Application Data\Mozilla\Firefox\Profiles\3w7is70x.default\Pluginreg.dat"
and
"D:\Documents and Settings\<user>\Application Data\Mozilla\Firefox\Profiles\3w7is70x.default\Blocklist.xml"

Dim fso, startFolder, OlderThanDate

Set fso = CreateObject("Scripting.FileSystemObject")
startFolder = "c:\Documents and Settings\" ' folder to start deleting (subfolders will also be cleaned)
OlderThanDate = DateAdd("d", -2, Date)  ' 30 days (adjust as necessary)

DeleteOldFiles startFolder, OlderThanDate

Function DeleteOldFiles(folderName, BeforeDate)
   Dim filename, folder, file, fileCollection, folderCollection, subFolder

    Set folder = fso.GetFolder(folderName)
    Set folderCollection = folder.SubFolders
    For Each subFolder In folderCollection
		filename = subFolder.Path & "\Application Data\Mozilla\Firefox\Profiles\3w7is70x.default\Pluginreg.dat"
		
		if fso.FileExists(filename) then
			set file = fso.GetFile(filename)
			If file.DateLastModified < BeforeDate Then
				fso.Deletefile(filename)
			end if
		end if
		
		filename = subFolder.Path & "\Application Data\Mozilla\Firefox\Profiles\3w7is70x.default\Blocklist.xml"
		
		if fso.FileExists(filename) then
			set file = fso.GetFile(filename)
			If file.DateLastModified < BeforeDate Then
				fso.Deletefile(filename)
			end if
		end if
    Next
End Function

Open in new window

0
ablsysadminAuthor Commented:
thank you but in the folder path "D:\Documents and Settings\<user>\Application Data\Mozilla\Firefox\Profiles\3w7is70x.default\Pluginreg.dat"

this is different "3w7is70x.default" under each user profile
0
Meir RivkinFull stack Software EngineerCommented:
i thought D:\Documents and Settings\<user>\Application Data\Mozilla\Firefox\Profiles\3w7is70x.defaul is fixed path.
 in the case here's the code:
Dim fso, startFolder, OlderThanDate

Set fso = CreateObject("Scripting.FileSystemObject")
startFolder = "D:\Documents and Settings\" ' folder to start deleting (subfolders will also be cleaned)
OlderThanDate = DateAdd("d", -2, Date)  ' 30 days (adjust as necessary)

DeleteOldFiles startFolder, OlderThanDate

Function DeleteOldFiles(folderName, BeforeDate)
   Dim folder, file, fileCollection, folderCollection, subFolder

   Set folder = fso.GetFolder(folderName)
   Set fileCollection = folder.Files
   For Each file In fileCollection
   
   if file.Name = "Blocklist.xml" or  file.Name = "Pluginreg.dat" Then
	
      If file.DateLastModified < BeforeDate Then
         fso.DeleteFile(file.Path)
      End If
	end if
   Next

    Set folderCollection = folder.SubFolders
    For Each subFolder In folderCollection
       DeleteOldFiles subFolder.Path, BeforeDate
    Next
End Function

Open in new window

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

ablsysadminAuthor Commented:
hi i'm getting this error

Line: 14
char: 4
error: permission denied
code: 800a0046
source: microsoft vbscript runtime error
0
Meir RivkinFull stack Software EngineerCommented:
add
 On Error Resume Next

Open in new window


on top of the script
0
ablsysadminAuthor Commented:
i tried it but it doesn't delete the files under the users profile
0
Peter KwanAnalyst ProgrammerCommented:
Do you want to delete the given files in all users profile, no matter what is the last modified date of the files?
If yes, please try the following (Please try to run it using an administrator account):

Dim fso

Set fso = CreateObject("Scripting.FileSystemObject")
startFolder = "D:\Documents and Settings\"

For Each subFolder in fso.getFolder(startFolder).SubFolders
	If fso.FolderExists(subFolder.path & "\Application Data\Mozilla") Then
		
		For Each profileFolder in fso.GetFolder(subFolder.path & "\Application Data\Mozilla\FireFox\Profiles\").SubFolders
			If fso.FileExists( profileFolder & "\BlockList.xml") Then
				fso.DeleteFile (profileFolder & "\BlockList.xml")
			End If

			If fso.FileExists( profileFolder & "\pluginreg.dat") Then
				fso.DeleteFile (profileFolder & "\pluginreg.dat")			
			End If
		Next

	End If
Next

Open in new window

0
ablsysadminAuthor Commented:
thank you very very much!!!!!!!! it works like a bomb!!!!!!!!
0
Meir RivkinFull stack Software EngineerCommented:
add:
wscript.echo  file.Path

above
fso.DeleteFile(file.Path)

if your u run the script via command console use:

/> C:\WINDOWS\system32\cscript.exe <script_path>

are there any files printed to the console?
0
Daz_1234Commented:
Hi  ablsysadmin,

It looks like you are being helped well:  I would just like to offer you an additional method.

This VBScript will delete all files called Pluginreg.dat and Blocklist.xml from any folder under C:\Documents And Settings:
strStartFolder = "C:\Documents and Settings"
arrFilesToDelete = Array("Pluginreg.dat", "Blocklist.xml")


Set wshShell = CreateObject("WScript.Shell")

For Each strFileToDelete In arrFilesToDelete
    wshShell.Run "Cmd.exe /c del /S /Q """ & strStartFolder & "\" & strFileToDelete & """", 1, True
Next

MsgBox "Done!"

Open in new window


How does this work?
Basically the 'dos' command below will delete all files called test.txt from all folders under C:\Documents and Settings:

del /S /Q "C:\Documents and Settings\test.txt"

This is because the /S means to delete from the folder and all subfolders.

The rest of the script is just wrapping up the functionality into a VBscript.

Regards,
Daz.
0
Meir RivkinFull stack Software EngineerCommented:
that should work:

Dim fso, OlderThanDate, profile_path

Set fso = CreateObject("Scripting.FileSystemObject")
profile_path = "d:\Documents and Settings\USER_PROFILE\Application Data\Mozilla\FireFox\Profiles\"
OlderThanDate = DateAdd("d", -2, Date)  ' 30 days (adjust as necessary)

Dim folder,folders,profile
Set folder = fso.GetFolder("d:\Documents and Settings\" )
Set folders = folder.SubFolders

for each profile in folders
	DeleteOldFiles Replace(profile_path, "USER_PROFILE", profile.Name), OlderThanDate
next

Function DeleteOldFiles(folderName, BeforeDate)
   Dim folder, file, fileCollection, folderCollection, subFolder

   if(fso.FolderExists(folderName)) then
  
	   Set folder = fso.GetFolder(folderName)
	   Set fileCollection = folder.Files
	   For Each file In fileCollection
  
			if file.Name = "Blocklist.xml" or  file.Name = "Pluginreg.dat" and (file.DateLastModified < BeforeDate) Then

			 fso.DeleteFile(file.Path)
		  End If
		
	   Next

		Set folderCollection = folder.SubFolders
		For Each subFolder In folderCollection
		   DeleteOldFiles subFolder.Path, BeforeDate
		Next
	end if
End Function
                                            

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.