Solved

How to delete files in a folder and subfolder

Posted on 2011-09-08
13
355 Views
Last Modified: 2012-05-12
I have code to delete files in subfolders, but not the folders themselves.  I have two problems... first the code does not work correctly.  It deletes files in the first subfolder, but not folders further up and not in the initial folder.

Second, I would like to delete only files that have not been modified in the last 2 months.  In other words, files older than 2 months.  If there is better code, then please disregard my code.  Here is the code I have (which does not work)... thanks for the help!
on error resume next
Set oFS = CreateObject("Scripting.FileSystemObject")

sFolder = "C:\_test"

For Each subfolder in oFS.GetFolder(sFolder).Subfolders
    For Each file in oFS.GetFolder(subfolder.Path).Files
        oFS.DeleteFile file.Path, True
    Next
    For Each folder in oFS.GetFolder(subfolder.Path).Subfolders
        oFS.DeleteFolder folder.Path
    Next
Next

Set oFS = Nothing

Msgbox "Done."

Open in new window

0
Comment
Question by:bpl5000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 54

Expert Comment

by:Bill Prew
ID: 36505521
So, is it the case that you want to delete all files from underneath c:\_test if they are older than 2 months, including files in any subfolders under c:\_test?  But not delete any of the subfolders them selves?

~bp
0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 36505562
If what I described was what you needed, here's an example of how to do that.

' Define folder to remove files from
strDir = "C:\_Test"

' Number of days to remove files after
intAge = 60

' Create file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Call recursive subroutine to start the delete process
DeleteAge objFSO.GetFolder(strDir), intAge

Sub DeleteAge(objFolder, intAge)
   ' Look at all files in this folder
   For Each objFile In objFolder.Files
     If objFile.DateLastModified < (Now - intAge) Then
        objFile.Delete
        Exit For
     End If
   Next	

   ' Drill down into each subfolder in this current folder
   For Each objSubFolder in objFolder.SubFolders
      DeleteAge objSubFolder, intAge
   Next
End Sub

Open in new window

~bp
0
 
LVL 5

Author Comment

by:bpl5000
ID: 36506205
Yes, bp... that's exactly what I want to do.  I tried your code, but it gives the following error...

Script:     C:\delete.vbs
Line:        17
Char:       9
Error:       Permission denied
Code:      800A0046
Source:   Microsoft VBScript runtime error

This is at the line where it would delete the file (objFile.Delete).  Any ideas why I would get "Permission denied"?  I can manually delete the files.
0
Technology Partners: 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!

 
LVL 54

Expert Comment

by:Bill Prew
ID: 36506222
Did it delete any files?  Typically that error is what it says, you don't have authority to delete the files involved.

You might try adding a display of the file right before the delete, like:

        Wscript.Echo objFile.Path
        objFile.Delete

to see whaich file it has a problem with.

~bp
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 36506245
Also just noticed one other item, you'll want to remove the following line:

        Exit For

I borrowed this code from a prior script and it was doing something slightly different.

~bp
0
 
LVL 5

Author Comment

by:bpl5000
ID: 36510020
I put in the echo statement and it errors on every file.  I can browse to the file and delete it, but then it errors on the next file.  I thought it was my virus protection, but I turned it off and it still fails.
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 36510032
Can you paste up a couple of the actual echo outputs of files that fail.

~bp
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 36510102
I did a test here, worked fine on a folder where I had full permission.

~bp
0
 
LVL 5

Author Comment

by:bpl5000
ID: 36510652
Ok, here is the output...

C:\_test\dell\drivers\Intel_NIC\unattend\PushXP.txt

When I browse to this file, I can delete it manually.  If I put in the statement "on error resume next", it does not delete any of the files.  I have my antivirus disabled.  It seems to me like something is stopping a vbscript from deleting files, but I only have my antivirus program (no anti malware programs).
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 36510843
Okay, that path looks fine, I don't see any problem with that.

Yes, the on error resume next just tells VBS to not display any errors that occur, and keep executing at the next statement as if nothing went wrong.  But the file wouldn't be deleted.

Try a test on a different set of files in a different folder just to see what happens.

Also, open a DOS command window and try to do a DEL command on the file and see if you get an error there.

~bp
0
 
LVL 5

Author Comment

by:bpl5000
ID: 36511349
Ok, tried using del command and I get access denied.  I have UAC disabled and I have no idea what's going on, but it does work on the server, which is what I needed.

Thanks for all the help!
0
 
LVL 5

Author Closing Comment

by:bpl5000
ID: 36511353
Works great!  Thanks for the help!
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 36511402
Glad that worked out, thanks.

~bp
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

Suggested Solutions

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…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

730 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