Solved

Vbscript to delete files in unknown directories

Posted on 2012-04-04
11
636 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 

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
 

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 13

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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
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…

734 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