Solved

VBSCRIPT to delete all files and folers in a directory

Posted on 2009-07-16
11
574 Views
Last Modified: 2012-05-07
I have a vbscript that I can't get to work properly. I need to delete all files and folders in a directory. When this runs, I get:

line 12
char 7
error permission denied
code 800a0047
microsoft vbscript runtime error


I really thought this was working for a while...Can anyone fix this for me?

dim fso, exchfolder,folder,collfolder,collfiles,file

Set fso   = WScript.CreateObject("Scripting.FileSystemObject")

set exchfolder = fso.GetFolder("E:\Exchange Backups\")  ' the target folder
 

Set collfiles=exchfolder.Files  'delete files under target folder

      for each file in collfiles

      file.delete

      next
 

Set collfolder=exchfolder.subfolders 'delete subfolders and files under targetfolder

      for each folder in collfolder

      folder.delete

      next

Open in new window

0
Comment
Question by:FIFBA
  • 6
  • 5
11 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 24870252
Try this slight modification...
dim fso, exchfolder,folder,collfolder,collfiles,file

Set fso   = WScript.CreateObject("Scripting.FileSystemObject")

set exchfolder = fso.GetFolder("E:\Exchange Backups\")  ' the target folder

 

Set collfiles=exchfolder.Files  'delete files under target folder

      for each file in collfiles

      file.delete

      next

 

Set collfolder=exchfolder.subfolders 'delete subfolders and files under targetfolder

      for each folder in collfolder

      fso.DeleteFolder folder, True

      next

Open in new window

0
 

Author Comment

by:FIFBA
ID: 24870345
Thanks for the fast response. I'm getting the same results with the modification in place.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24870364
Can you add an echo/popup to that loop to determine which folder?
Presumably it's permissions to the folder - I'd check security:
Set collfolder=exchfolder.subfolders 'delete subfolders and files under targetfolder

      for each folder in collfolder

wscript.echo "Attempting to delete " & folder

      fso.DeleteFolder folder, True

      next

Open in new window

0
 

Author Comment

by:FIFBA
ID: 24870500
I see now...This is actually deleting files inside of a Microsoft DFS folder. Admin has permissions to it but it is choking on a DFS system file. How can I exclude the C:\Exchange Backups\dfsrprivate file?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24870518
Place a

If Not lcase(folder)="c:\exchange backups\dfsrprivate" then
before the delete...

That or try scheduling it to run under the system credentials - that might work...
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:FIFBA
ID: 24870574
Could you post the entire script for me? Not a vbscript guy... thanks.

0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 24870599

dim fso, exchfolder,folder,collfolder,collfiles,file

Set fso   = WScript.CreateObject("Scripting.FileSystemObject")

set exchfolder = fso.GetFolder("E:\Exchange Backups\")  ' the target folder

 

Set collfiles=exchfolder.Files  'delete files under target folder

      for each file in collfiles

      file.delete

      next

 

Set collfolder=exchfolder.subfolders 'delete subfolders and files under targetfolder

      for each folder in collfolder

      If Not lcase(folder)="c:\exchange backups\dfsrprivate" then fso.DeleteFolder folder, True

      next

Open in new window

0
 

Author Comment

by:FIFBA
ID: 24870623
I get line 12 char 67 error...perm denied.
0
 

Author Comment

by:FIFBA
ID: 24870689
Nevermind....the path was wrong....directory is on E drive. Thanks for the help!!
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24870750
more perhaps that are preventing the deletion?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24870757
Ah - didn't refresh...
Glad I could help - thanx for the grade! :^)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

919 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now