Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to delete files in a folder and subfolder

Posted on 2011-09-08
13
Medium Priority
?
360 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 57

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 57

Accepted Solution

by:
Bill Prew earned 2000 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
Independent Software Vendors: 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 57

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 57

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 57

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 57

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 57

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 57

Expert Comment

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

~bp
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

722 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