Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need help with a vbs script that does not work

Posted on 2009-04-28
10
Medium Priority
?
202 Views
Last Modified: 2012-05-06
Here is the code.  I put this together from things I found on the internet.  I thought it used to work but now it does not.  I am trying to delete files in the Local Settings\Temp\*.* and Temporary internet files\*.*, also the java cache in Application Data\Sun\Java\Deployment\cache & log.
Option explicit
on error resume next
Dim fso, sProfileRoot, oProfilesDir, oProfileDir, FolderName, oTempFolder, oTempFolderSubFolders, oFolder, oFile, oTempFiles, sDebug
 
'This script will delete all files in all temp directories for all profiles.
 
sDebug = "false"
set fso = CreateObject("Scripting.FileSystemObject")
sProfileRoot = "C:\Documents and Settings"
 
'iterate thru Profile Directories
set oProfilesDir = fso.GetFolder(sProfileRoot)
 
For Each oProfileDir In oProfilesDir.SubFolders
	if sDebug = "true" Then
		WScript.StdOut.Writeline "Checking Profile " & oProfileDir.Path
	End IF
	call deleteFolders(oProfileDir.Path & "\Local Settings\Temp")
	call deleteFolders(oProfileDir.Path & "\Local Settings\Temporary Internet Files")
             call deleteFolders(oProfileDir.Path & "\Application Data\Sun\Java\Deployment\cache")
             call deleteFolders(oProfileDir.Path & "\Application Data\Sun\Java\Deployment\log")
Next
set fso = nothing
wscript.quit
 
Sub deleteFolders(FolderName)
	Set oTempFolder = fso.GetFolder(FolderName)
	if sDebug = "true" Then
		WScript.StdOut.Writeline "Checking " & FolderName
	End if
	'delete folders in temp Folder
	Set oTempFolderSubFolders = oTempFolder.SubFolders
	
	If Not IsNull(oTempFolderSubFolders) Then
		For Each oFolder in oTempFolderSubFolders
			if sDebug = "true" Then
				WScript.StdOut.Writeline "Deleting " & oFolder.Path
			End IF
			fso.DeleteFolder oFolder.Path, True
		Next
	End If
	
	'delete files in temp folder
	Set oTempFiles = oTempFolder.Files
	If Not IsNull(oTempFiles) Then
		For Each oFile in oTempFiles
			if sDebug = "true" Then
				WScript.StdOut.Writeline "Deleting " & oFile.Path
			End If
			fso.DeleteFile oFile.Path, True
		Next
	End If
End Sub

Open in new window

0
Comment
Question by:MedCentral
  • 5
  • 4
10 Comments
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 24258728
unless run as admin, you dont have access or delete rights to other users profile files.

is this the problem ?

what doesnt work or what errors are generated.

0
 

Author Comment

by:MedCentral
ID: 24259428
I am running it as an administrator, in fact I have run it from two different administrative accounts.  It writes the script saying the files are there but it does not delete them.
0
 
LVL 12

Expert Comment

by:Krys_K
ID: 24261722
Hi there
 can you comment out the On Error Resume Next statement and run it again.
What errors occur?
Regards
Krystian
0
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!

 

Author Comment

by:MedCentral
ID: 24262456
When I do that it goes down to the Sub deleteFolders below and sometimes says path not found and other times says variable 'deleteFolders' is not declared.  I am so new at this that I have no idea how to fix it.
0
 
LVL 12

Expert Comment

by:Krys_K
ID: 24263151
Hi no probs, that's what we're here for :-)
Could you post the exact error message(s), all the details will help.
Cheers
Krystian
0
 

Author Comment

by:MedCentral
ID: 24264296
I put the error from VbsEdit.

vbs-script-error.pdf
0
 
LVL 12

Expert Comment

by:Krys_K
ID: 24264615
HI Thanks, I guess it comes down to whetehr the path really does exists or not.  What path is supposedly in teh variable FolderName of the following line: Sub deleteFolders(FolderName)
To determine this can you put a Messagebox under teh following line:
 Sub deleteFolders(FolderName)
MsgBox FolderName
 
I suspect its probably either an empty variable which of course, means no such path. If it is an empty variable, we need to see at what point it happens so that we can solve the problem. Your On error Resume Next was skipping this problem.
Regards
Krystian
0
 

Author Comment

by:MedCentral
ID: 24270229
I think I am starting to understand it more.  The message box says that FolderName is C:\Documents and Settings\Administrator\Local Settings\Temp and that folder does not exist, therefore it sees it as an error and does not go on to the other profiles.  So how can we get it to function if some of the folders or files are not present?  Also, what does the sdebug = true do? Why is it needed?
0
 

Author Comment

by:MedCentral
ID: 24270923
When I removed the comment from On Error Resume Next, The message box comes up for each folder in the profile and that appears to be correct, and it seems to work on all but the logged on profile.
0
 
LVL 12

Accepted Solution

by:
Krys_K earned 500 total points
ID: 24272680
HI There
I guess that would be correct in that the logged on user profile is in use and may not allow certain things to be deleted. Can you do it manually? - delete the folder that the script fails on?
If not then the script is working exactly as it shouold which is great for the initial problem you asked.
The line that cntains sDebug = "false"
is saying that if you set it to "true", then all the debug sections in your code will start to output to the command line, the messages. Bear in mind though, that those Messages such as
if sDebug = "true" Then
   WScript.StdOut.Writeline "Checking Profile " & oProfileDir.Path
End IF
can only work if you run your script in a command window CMD.exe otherwise they will fail (The on error resume next will mask the failures actually)
To run in a command window you need to type it as:
cscript.exe YourScriptName.vbs
Hope that helps
Regards
Krystian
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month20 days, 22 hours left to enroll

810 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