Solved

How to delete files in a folder and subfolder

Posted on 2011-09-08
13
351 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 51

Expert Comment

by:Bill Prew
Comment Utility
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 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 51

Expert Comment

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

~bp
0
 
LVL 51

Expert Comment

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

~bp
0
 
LVL 5

Author Comment

by:bpl5000
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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
Comment Utility
Works great!  Thanks for the help!
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Glad that worked out, thanks.

~bp
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

763 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

12 Experts available now in Live!

Get 1:1 Help Now