Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Vbscript to delete files in unknown directories

Posted on 2012-04-04
11
Medium Priority
?
651 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:Meir Rivkin
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:Meir Rivkin
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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:Meir Rivkin
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:Meir Rivkin
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:
Meir Rivkin earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

916 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