Link to home
Start Free TrialLog in
Avatar of bpl5000
bpl5000

asked on

How to delete files in a folder and subfolder

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

Avatar of Bill Prew
Bill Prew

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
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bpl5000

ASKER

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.
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
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
Avatar of bpl5000

ASKER

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.
Can you paste up a couple of the actual echo outputs of files that fail.

~bp
I did a test here, worked fine on a folder where I had full permission.

~bp
Avatar of bpl5000

ASKER

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).
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
Avatar of bpl5000

ASKER

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!
Avatar of bpl5000

ASKER

Works great!  Thanks for the help!
Glad that worked out, thanks.

~bp