Solved

Loop Through Files in Folder Including SubFolder

Posted on 2009-05-19
3
3,378 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 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

726 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