+ Reply to Thread
Results 1 to 4 of 4

Moving Files from Folder and Subfolders

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-20-2012
    Location
    Hyderabad
    MS-Off Ver
    Excel 2007 & Excel 2010
    Posts
    115

    Moving Files from Folder and Subfolders

    Hello All,

    I need to move all the "Excel" files which are available in Folder and also the Subfolders from this location.

    I need all the Excel files.

    I found a code which is able to move only from Folder but not from the Subfolder.

    Kindly help me.

    HTML Code: 

  2. #2
    Forum Contributor codeslizer's Avatar
    Join Date
    05-28-2013
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2003 - 2010
    Posts
    245

    Re: Moving Files from Folder and Subfolders

    Hello mvinay,

    Give it a try to the attached file. I've used the following code to make it work. Here, vFrom and vTo are the range names that I've given them. The code would not only move Excel based files but also the csv files that are also frequently used in excel. Could remove it from the array if you don't want. That means, if you want you can also add the extensions which you'd like to move along with Excel files.

    Let me know for if things don't work out for you.

    Sub Move_XL_Files()
    
        Dim sourcePath As String
        Dim extn, fil
        Dim filCount As Long
        
        If Range("vFrom").Value <> "" And Range("vTo").Value <> "" Then
            If Range("vFrom").Value <> Range("vTo").Value Then
                ' Checks for path entries
                If Right(Range("vFrom").Value, 1) = "\" Then
                    sourcePath = Left(Range("vFrom").Value, Len(Range("vFrom").Value) - 1)
                Else
                    sourcePath = Range("vFrom").Value
                End If
                
                ' XL files can be in either csv, xls, xlsx, xlsm, xlsb, etc..
                extn = Array("*.xl*", "*.csv")
                filCount = 0
                For Each ext In extn
                    fil = Dir(sourcePath & "\" & ext)
                    Do While fil <> ""
                        'MsgBox sourcePath & "\" & fil
                        FileCopy sourcePath & "\" & fil, Range("vTo").Value & "\" & fil
                        Kill sourcePath & "\" & fil
                        fil = Dir(sourcePath & "\" & ext)
                        filCount = filCount + 1
                    Loop
                Next ext
                MsgBox "Files successfully moved!", vbInformation, "Success!"
            Else
                MsgBox "Same Path. Files Not Moved"
            End If
        Else
            MsgBox "Path field empty", vbExclamation, "Empty Path"
        End If
        
    End Sub
    Attached Files Attached Files
    Last edited by codeslizer; 01-29-2014 at 11:37 AM.
    cOdEsLiZeR - Back after a long break.. Let's sLiZe some more cOdEs!!

  3. #3
    Forum Contributor
    Join Date
    12-20-2012
    Location
    Hyderabad
    MS-Off Ver
    Excel 2007 & Excel 2010
    Posts
    115

    Re: Moving Files from Folder and Subfolders

    Hello Codeslizer,

    This code is able to move the Files only from Selected folder. But I have Other subfolders in the Selected folder and I need have the all the Files need to moved from Selected folder and also from the Sub-Folders.

    Kindly check the attached image, which shows the files and subfolder. I also have files in these subfolders and all these need to be moved in single click.

    Please provide the code.

    Thanks in Advance.
    Attached Images Attached Images

  4. #4
    Forum Contributor codeslizer's Avatar
    Join Date
    05-28-2013
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2003 - 2010
    Posts
    245

    Re: Moving Files from Folder and Subfolders

    Hello mvinay,

    Below is the link to the code to iterate through each sub-folders in a given path.

    http://www.excelguru.ca/forums/showthread.p....-that-contains-folder-name-as-Numeric

    Use the main code for moving within the code in the link.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] list of subfolders in folder - without files and sub-subfolders
    By MartyZ in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-11-2022, 10:56 AM
  2. [SOLVED] Moving of *.xls or *.xlsx from folder and subfolders to another location
    By mvinay in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-27-2014, 12:45 AM
  3. Moving subfolders to another folder.
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 12-22-2012, 06:45 PM
  4. Counting files in folder including subfolders also and folder size
    By mido609 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-12-2012, 03:26 PM
  5. [SOLVED] Opening Excel files in same folder through a Macro - but not subfolders?
    By toffee_madman in forum Excel General
    Replies: 9
    Last Post: 10-19-2011, 11:09 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1