Solved

Vbscript to delete files in unknown directories

Posted on 2012-04-04
11
620 Views
Last Modified: 2012-04-10
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
0
Comment
Question by:ablsysadmin
11 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 37809773
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
 

Author Comment

by:ablsysadmin
ID: 37809801
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
 
LVL 42

Expert Comment

by:sedgwick
ID: 37809864
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
 

Author Comment

by:ablsysadmin
ID: 37809919
hi i'm getting this error

Line: 14
char: 4
error: permission denied
code: 800a0046
source: microsoft vbscript runtime error
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 37809968
add
 On Error Resume Next

Open in new window


on top of the script
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:ablsysadmin
ID: 37810184
i tried it but it doesn't delete the files under the users profile
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 37810216
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
 

Author Comment

by:ablsysadmin
ID: 37810225
thank you very very much!!!!!!!! it works like a bomb!!!!!!!!
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 37810250
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
 
LVL 12

Expert Comment

by:Daz_1234
ID: 37810670
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
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 37810785
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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes how to set permissions to allow a limited-permissions user to start and stop a particular System Service.   It is always best to give users only the permissions that they need to perform their job, so tweaking particular permi…
INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now