?
Solved

Loop Through Files in Folder Including SubFolder

Posted on 2009-05-19
3
Medium Priority
?
3,381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 9

Accepted Solution

by:
tetorvik earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

800 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