Solved

Loop Through Files in Folder Including SubFolder

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

840 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