Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete same file extensions in subfolders

Status
Not open for further replies.

icewiper

Technical User
Apr 8, 2005
30
0
0
US
Hello all,

I am trying to write something in vb.net that will delete all file extensions (example *.mov) in all directories and subdirectories.

I know the "kill" command deletes the files. It only deletes files within the specified folder.

lately I have been cheating and having VB write batch files to do this.

can anyone help me with this?

I have looked in the msdn library and no luck

Thanks in advance
 
You'll want to make a recursive method that takes the path and filter as parameters. Inside this method you would first loop through all of the folders in the specified path and call the method with those paths. Then you would loop through all of the files in the folder and delete any with the extension that was provided.

Also, instead of using kill, use the System.IO name space. There you have access to the Directory and File objects that easily allow you to grap paths, sub folders, file lists and extensions as well as functionality to create, copy and delete.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thanks ThatRickGuuy for the help. I am hoping more for an idea on what the code would look like.

I have this

Dim FSO As Scripting.FileSystemObject
Dim oFolder As Scripting.Folder
Dim oFile As Scripting.File
Dim varlen As Integer
Dim varstr As String
Dim varstr2 As String

FSO = CreateObject("Scripting.FileSystemObject")
oFolder = FSO.GetFolder("C:\")
FSO = Nothing

For Each oFolder In oFolder.SubFolders
For Each oFile In oFolder.Files
varstr = oFile.Name
varlen = Len(varstr)
varstr2 = varstr.Substring(varlen - 4)
If varstr2 = ".mov" Then
'Write(oFile.Name)
oFile.Delete(True)
End If
Next
Next

But it doesn't seem to delete any of the files past the first folder.
I am hoping someone can see what I am doing wrong on it.

thanks again
 
First, I would recommend ditching the FSO. That thing is a slow as tar left over from the COM era.

Code:
  Private Sub RecursiveDelete(ByVal Path As String, ByVal Filter As String)
    Dim s As String
    For Each s In System.IO.Directory.GetDirectories(Path)
      RecursiveDelete(s, Filter)
    Next

    For Each s In System.IO.Directory.GetFiles(Path, Filter)
      System.IO.File.Delete(s)
    Next
  End Sub

You might want to step through that and make sure that the variable s contains the fully qualified path for both the sub directories and files. But it should be close to what you are looking for. Also, you can put Imports System.IO at the top of the class and then you can refer to the Directory and File object with out the System.IO.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
ThatRickGuy, Thanks so much for helping out. That code did the trick. I threw in an array to remove differnet extentions and it works perfect. I'll post the code so others can see it if they wish. I do have one question.
If I use a base folder such as "C:\Documents and Settings" it goes through all the sub folders correctly. but If I start at the Root of "C:\" I get an error.
"An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll

Additional information: Access to the path "Y:\System Volume Information" is denied."

I tried running an error handler to bypass it but then it just freezes up.

any ideas?

again I can't thank you enough for your help.

here is sample of the code I used with the array incase others wish to use it.

Example:

Dim PicArray(6) As String
Dim p As Integer

PicArray(1) = "*.BMP"
PicArray(2) = "*.CLP"
PicArray(3) = "*.GIF"
PicArray(4) = "*.IMG"
PicArray(5) = "*.JPG"
PicArray(6) = "*.JPEG"


For p = 1 To 6
RecursiveDelete("C:\Documents and Settings", PicArray(p))
Next p
 
Code:
Private Sub RecursiveDelete(ByVal Path As String, ByVal Filter As String)
  Dim s As String
  For Each s In System.IO.Directory.GetDirectories(Path)
    try
      RecursiveDelete(s, Filter)
    catch dirEx as exception
      debug.writeline("Cannot Access " & s & " : " & dirEx.message
    end try
  Next

    For Each s In System.IO.Directory.GetFiles(Path, Filter)
      try
        System.IO.File.Delete(s)
      catch ex as exception
        debug.writeline("Cannot delete " & s & " : " & ex.description)
      end try
    Next

End Sub

See if that works any better. If you try to access other people's setting folders with out local admin rights it will likely pop a security error. And if you try to delete a protected or in use file, it will likely throw another error.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Rick,

again i thank you so much. it worked perfectly. Except I had to change the ex.dsecription to err.description. I still have a lot to learn but it great to know there is help from people like you.

Thank again from over here in Iraq
 
whoops, yeah, the ex.description should by ex.message. Sorry about that. I avoid using the legacy err object. Working with exceptions can be much easier, expecially when you want to get into centralized logging.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top