Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to delete filetype from particular directory 1

Status
Not open for further replies.

JustScriptIt

Technical User
Oct 28, 2011
73
US
I am trying to build a script that eventually deletes tmp, err, files from remote computers.

There are certain directories that Disk Cleanup just cannot touch, especially if these folders are from other programs installed on Windows.

Right now, I'm trying this on my own computer, and it is not deleting the files, even though the script outputs that the file has been deleted!

Code:
Option Explicit

Dim fso
Dim strDir, objDir, strComputer
Dim strArray, strDrive, strRel 
Dim objDelFile, strDelete




''''''''''Declare Sub Routines''''''''''

Sub HandleError()
     On Error Goto 0
     Err.Clear 
End Sub

Sub DeleteFiles(pCurrentDir)
	Dim aItem, sParentName, sBaseName
    
   	On Error Resume Next
	For Each aItem In pCurrentDir.Files
		If (Err.Number <> 0) Then
	  		HandleError()
			Wscript.echo aItem & " cannot be accessed"
			Wscript.echo
		Else
			If fso.GetExtensionName(aItem.Path) = "txt" Then
	
				If Right(fso.GetParentFolderName(aItem.path),1) = "\" Then
					sParentName = Replace(fso.GetParentFolderName(aItem.path),"\","")
				Else
					sParentName = fso.GetParentFolderName(aItem.path)
				End If

				sBaseName = sParentName & _
					"\" & fso.GetBaseName(aItem.path)

				'Wscript.echo sBaseName

				strArray = Split(sBaseName,":",-1,1)
	  			strDrive = strArray(0)
	  			strRel = strArray(1)

				'Create file object
	  			Set objDelFile = CreateObject("Scripting.FileSystemObject")
	  			strDelete = "\\" & strComputer & "\" & strDrive & "$" & strRel
	
				Wscript.echo sBaseName & " will be deleted"
				Wscript.echo

				'Delete the file
	  			objDelFile.DeleteFile strDelete,true

        
			End If
		End If
   	Next

 	On Error Resume Next
	For Each aItem In pCurrentDir.SubFolders	
		If (Err.Number <> 0) Then
	  		HandleError()
			Wscript.echo aItem & " cannot be accessed"
			Wscript.echo
		Else
			DeleteFiles(aItem)
	
		End If
   	Next


End Sub 


''''''''''End of Sub Routine Declaration''''''''''

Set fso = CreateObject("Scripting.FileSystemObject")
Set objDir = FSO.GetFolder(strDir)


strDir = "d:\windows\temp"
strComputer = "10.234.4.16"

DeleteFiles(objDir)
 
By the way, in past scripts I was able to successfully use UNC to delete files, but for some reason, it's not working here.
 
it is not deleting the files
Comment out this:
On Error Resume Next

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Which On Error Resume Next do I delete, and what error handling do I substitute it with?

Earlier today, the program raised run-time errors because certain files, folders weren't accessible.

I'd like to script to continue execution even if certain files, folders cannot be accessed.

HELP!
 
On Error Resume Next" is OK only if you check err.number at every line that could generate a runtime error, and then turn it off with "On Error Goto 0". You seem to be doing this to some extent, so I would'nt remove the "On Error Resume Next"s

However, you are not checking err.number after the delete, so you wouldn't know of any runtime errors (this is the danger of On Error Resume Next). I would check err.number, and if there is an error, echo it and and err.description for more information.

Also, you are deleting the file in strDelete, but you are echoing sBaseName & " will be deleted". Why not echo strDelete, may point out the problem.

 
I changed the code around delete as followed

Code:
' Turn on error checking in case file is in use
On Error Resume Next

'Delete the file
objDelFile.DeleteFile sBaseName,true

' Turn off error checking in case file is in use
On Error Goto 0

So far, no hiccups
 
>>you are not checking err.number after the delete<<

When I tried this, I got some strange error - will attempt again...
 
Just noticed; There is no need to create a new FileSystem Object (objDelFile) to perform the delete... you already created one called "fso". Plus, you would have to release it with "Set objDelFile = Nothing" or you would get errors after the first pass through the loop.
Code:
[s]Set objDelFile = CreateObject("Scripting.FileSystemObject")[/s]
.....
fso.DeleteFile strDelete,true
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top