Is it possible

Posted on 2005-04-05
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

    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\"

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    758 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now