Is it possible

Posted on 2005-04-05
Medium Priority
Last Modified: 2010-04-23
I have a ton of directories with sub-directories that contain files.
Is it possible in VB (or how would I go about) to pull only the files out of the sub-directories into a
new directory I create on the C:\?  I would like to automate this as there are thousands.

Any help or suggestions would be great.

Question by:Jedidia
1 Comment
LVL 12

Accepted Solution

S-Twilley earned 500 total points
ID: 13711130
Ok... haven't tested this... but this should move files 'X' levels down (no shorter or deeper).
I've quoted the actual code that does the moving, and put in a msgbox about the move... I've tried to make sure the code doesn't pass through the folder you are trying to move to... let me know of any pobs

    Sub MoveSubLevel(ByVal sRoot As String, ByVal SubLevel As Integer, ByVal NewTargetFolder As String)
        Dim diThisDir As New IO.DirectoryInfo(sRoot)

        If SubLevel = 0 Then
            Dim fiThisFile As IO.FileInfo
            Dim fiFileList() As IO.FileInfo = diThisDir.GetFiles()
            If Not NewTargetFolder.EndsWith("\") Then NewTargetFolder &= "\"
            If NewTargetFolder.ToLower = sRoot.ToLower Then Exit Sub

            For Each fiThisFile In fiFileList
                'fiThisFile.MoveTo(NewTargetFolder & fiThisFile.Name)
                MsgBox("Moving: " & fiThisFile.FullName & vbCrLf & "To: " & NewTargetFolder & fiThisFile.Name)
            Dim diSubDir As IO.DirectoryInfo

            For Each diSubDir In diThisDir.GetDirectories
                If diSubDir.FullName.ToLower <> NewTargetFolder.ToLower Then
                    MoveSubLevel(diSubDir.FullName, SubLevel - 1, NewTargetFolder)
                End If
        End If
    End Sub


MoveSubLevel("F:\", 2, "F:\Test\")

would move files withing "F:\Images\Home\" into "F:\Test\"

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

616 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