• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

Need help with a vbs script that does not work

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
MedCentral
Asked:
MedCentral
  • 5
  • 4
1 Solution
 
Robberbaron (robr)Commented:
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
 
MedCentralAuthor Commented:
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
 
Krys_KCommented:
Hi there
 can you comment out the On Error Resume Next statement and run it again.
What errors occur?
Regards
Krystian
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
MedCentralAuthor Commented:
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
 
Krys_KCommented:
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
 
MedCentralAuthor Commented:
I put the error from VbsEdit.

vbs-script-error.pdf
0
 
Krys_KCommented:
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
 
MedCentralAuthor Commented:
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
 
MedCentralAuthor Commented:
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
 
Krys_KCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now