?
Solved

Loop Through Files in Folder Including SubFolder

Posted on 2009-05-19
3
Medium Priority
?
3,383 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

649 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