Solved

Loop Through Files in Folder Including SubFolder

Posted on 2009-05-19
3
3,372 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
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…

743 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

11 Experts available now in Live!

Get 1:1 Help Now