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

VBS to delete all files in a folder

I have nearly 10K folders in a TEMP folde, each of them with 50 to 200 PDFs.  These are created by an OCR program, and I can't seem to make it stop.  I need a script to run every week or so to just sweep them out.  The folders are in the User Temp folder.  Seems simple, but I keep getting a "Type mismatch" error message at the "objFile.Delete... " line.  Nothing fancy, just want to run this from time to time.
'******  	DELETE UNTITLED FOLDERS ********************
Const strSourceFolderName = "C:\Users\Mike\AppData\Local\Temp\"
Set objFSO = CreateObject("Scripting.fileSystemObject")
Set objFolder = objFSO.GetFolder(strSourceFolderName)
	For each objSubFolder In objFolder.Subfolders			' Now go move files
		FN = objSubFolder.Name
			if left(FN,8) = "Untitled" Then
				For Each objFile in objSubFolder.Files
				Call objFile.Delete(strSourceFolderName & bjFile.Name)	' Delete the file
		If (objSubFolder.Files.Count= 0) Then
			Call objSubFolder.Delete()		' Delete folder when empty
						End if
				Next
			Else
			End If
	Next

Open in new window

0
Mike Caldwell
Asked:
Mike Caldwell
  • 7
  • 6
1 Solution
 
winthropjCommented:
Call objFile.Delete(strSourceFolderName & bjFile.Name)

Your missing an o on the objFile.Name
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
Yep, but still not it.  Also I noticed I did not have the complete path.  That line is now:  

Call objFile.Delete(strSourceFolderName & FN & "\" & objFile.Name)

But still get the error message.
0
 
winthropjCommented:
How about this. It just deleted the folder with the files.


Const strSourceFolderName = "C:\Users\Kevin\AppData\Local\Temp\"
Set objFSO = CreateObject("Scripting.fileSystemObject")
Set objFolder = objFSO.GetFolder(strSourceFolderName)
      For each objSubFolder In objFolder.Subfolders
            FN = objSubFolder.Name
                  if left(FN,8) = "Untitled" Then
                  Call objSubFolder.Delete()
                  End if
      Next
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
winthropjCommented:
Replace Kevin with Mike.
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
Exactly what I'm trying to do, but when it hits the delete step I get a "Permission Denied."  Trying making a BAT file to call it and running as administrator, but same story.  Even went to CMD window as administrator and still getting permission error.  I hate it when Vista won't let me do what I want when I am an admistrator and the only user in the building.
0
 
winthropjCommented:
Not sure why you would be denied. Who is the owner on those folders?
 
Here is an exe from MS that you can use to change ownership. You could use this in the bat file maybe.
http://www.microsoft.com/downloadS/details.aspx?familyid=E8BA3E56-D8FE-4A91-93CF-ED6985E3927B&displaylang=en
0
 
winthropjCommented:
Supposedly that will change the owner on the folder and object within. It might be easier to run that against the temp folder first and then delete the folders you are after.
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
I'm the owner.  After all, it is my User folder.  The utility you suggest seems to be an XP thing; my OS is Vista Biz 32 bit.
0
 
winthropjCommented:
Is it User Account Control of maybe virus software stopping the delete?
0
 
winthropjCommented:
That's an or not of
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
UAC off; no change.  No virus SW running.
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
BTW: Vista will allow me to delete these files / folders manually, just won't allow the .VBS to do so.
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
I found that if I manually went to the TEMP folder and checked the Properties some files are read only.  So I unchecked it, and waited for over an hour (10K folders, about 500K files...).  The routine you provided then runs fine, no "permission" problem.  So now I need a programatic way to change the permission before I delete, but that is a separate question.  So thanks for this; works fine when Read Only is not set.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now