Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Vbscript to delete files in unknown directories

Posted on 2012-04-04
11
Medium Priority
?
648 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
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…

721 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