Solved

Loop Through Files in Folder Including SubFolder

Posted on 2009-05-19
3
3,374 Views
Last Modified: 2012-05-07
Hi,

This shouldn't be difficult but I just don't know.

I want to loop through all files in a folder including those in the subfolder and replace the filename. I got the below code but it works only for the parent folder.

Please help. Thx.


Dim folder As New DirectoryInfo(dest)

Dim tmpName As String = ""
 

For Each f As FileInfo In folder.GetFiles

    tmpName = f.Name.Replace(strFrom, strTo)

    f.MoveTo(f.DirectoryName & "\" & tmpName)

Next

Open in new window

0
Comment
Question by:NicksonKoh
3 Comments
 
LVL 9

Accepted Solution

by:
tetorvik earned 500 total points
ID: 24428789
you should be able to use GetFiles overload that enables searching subfolders as well. Something like:

GetFiles("*.*", SearchOption.AllDirectories)
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24428796
Try recursion
Private Sub GetFiles(ByVal Location As String)
 

        Dim folder As New IO.DirectoryInfo(Location)

        Dim tmpName As String = ""
 

        For Each f As IO.FileInfo In folder.GetFiles

            tmpName = f.Name.Replace(strFrom, strTo)

            f.MoveTo(f.DirectoryName & "\" & tmpName)

        Next
 

        For Each folders As IO.DirectoryInfo In folder.GetDirectories

            GetFiles(folders.FullName)

        Next
 

    End Sub

Open in new window

0
 
LVL 39

Expert Comment

by:appari
ID: 24428798
try this


Dim folder As New DirectoryInfo(dest)
Dim tmpName As String = ""
 
For Each f As FileInfo In folder.GetFiles("*", IO.SearchOption.AllDirectories)
    tmpName = f.Name.Replace(strFrom, strTo)
    f.MoveTo(f.DirectoryName & "\" & tmpName)
Next

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

26 Experts available now in Live!

Get 1:1 Help Now