Solved

How to delete files in a folder and subfolder

Posted on 2011-09-08
13
352 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
  • 8
  • 5
13 Comments
 
LVL 52

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 52

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
 
LVL 52

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 52

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
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.

 
LVL 52

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 52

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 52

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 52

Expert Comment

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

~bp
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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

948 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

18 Experts available now in Live!

Get 1:1 Help Now