+ Reply to Thread
Results 1 to 13 of 13

Moving subfolders to another folder.

Hybrid View

  1. #1
    Registered User
    Join Date
    06-12-2012
    Location
    Verona, New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    22

    Question Moving subfolders to another folder.

    I have a folder named Directory. Within the Directory folder, there are about 3,000 subfolders. I have to pull out about 1,000 of these subfolders. Each subfolder is named after an employee; last name, first name.
    I have an Excel spreadsheet and in column A is the name of the employee that needs to be pulled out of the Directory folder. So for example, if Jones, Mary is in column A2, then the macro needs to identify Jones, Mary subfolder and move (not copy) the subfolder and its contents to another folder. So in the end, I will have a thousand subfolders in one folder. Also in Column B there are some employee ID numbers associated with each name in Column A. If there is an employee ID number, can that be attached to the file name, eg. Jones, Mary – 11223?

    If you have another idea on how to do this, I am open for suggestions. Thank you very much.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Moving subfolders to another folder.

    Completely untested:

    Sub lwk()
        Const sDirFr    As String = "C:\myPathFr\"
        Const sDirTo    As String = "C:\myPathTo\"
        Dim sFile       As String
        Dim iRow        As Long
    
        For iRow = 2 To Cells(Rows.Count, "A")
            sFile = Cells(iRow, "A").Value2
            If Len(Dir(sDirFr & sFile)) Then
                Name sDirFr & sFile As _
                     sDirTo & sFile & " - " & Cells(iRow, "A").Value2
            End If
        Next iRow
    End Sub
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    06-12-2012
    Location
    Verona, New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Moving subfolders to another folder.

    Hi. Thank you but nothing is happening when I test this macro.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Moving subfolders to another folder.

    That's not much help.

    What's the exact code you're using? What happens when you step through it?

  5. #5
    Registered User
    Join Date
    06-12-2012
    Location
    Verona, New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Moving subfolders to another folder.

    I am using the code below. Changing the to and from path. No action occurs when i run it. No errors either.


    Sub lwk()
        Const sDirFr    As String = "C:\Users\Lucas\Desktop\From"
        Const sDirTo    As String = "C:\Users\Lucas\Desktop\To"
        Dim sFile       As String
        Dim iRow        As Long
    
        For iRow = 2 To Cells(Rows.Count, "A")
            sFile = Cells(iRow, "A").Value2
            If Len(Dir(sDirFr & sFile)) Then
                Name sDirFr & sFile As _
                     sDirTo & sFile & " - " & Cells(iRow, "A").Value2
            End If
        Next iRow
    End Sub
    Last edited by Leith Ross; 12-21-2012 at 10:49 PM. Reason: Added Code Tags

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Moving subfolders to another folder.

    You're missing the trailing path separators on the directories.

  7. #7
    Registered User
    Join Date
    06-12-2012
    Location
    Verona, New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Moving subfolders to another folder.

    OK.. I added the trailing path separators but still nothing is happening. I have the From folder sitting on my desktop as well to To folder. In the From folder I have a subfolder named Abbatiello,Phillip.

    Nothing is happening when I run the code below. I also uploaded the Excel file I am using.

    Sub lwk()
        Const sDirFr    As String = "C:\Users\Lucas\Desktop\From\"
        Const sDirTo    As String = "C:\Users\Lucas\Desktop\To\"
        Dim sFile       As String
        Dim iRow        As Long
    
        For iRow = 2 To Cells(Rows.Count, "A")
            sFile = Cells(iRow, "A").Value2
            If Len(Dir(sDirFr & sFile)) Then
                Name sDirFr & sFile As _
                     sDirTo & sFile & " - " & Cells(iRow, "A").Value2
            End If
        Next iRow
    End Sub
    .
    Attached Files Attached Files
    Last edited by Leith Ross; 12-22-2012 at 06:10 PM. Reason: Added Code Tags

  8. #8
    Registered User
    Join Date
    06-12-2012
    Location
    Verona, New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Moving subfolders to another folder.

    Hi. Also I found some VBA code that just moves subfolders to another folder. Does this help?

    Sub Copy_Folder()
    'This example copy all files and subfolders from FromPath to ToPath.
    'Note: If ToPath already exist it will overwrite existing files in this folder
    'if ToPath not exist it will be made for you.
        Dim FSO As Object
        Dim FromPath As String
        Dim ToPath As String
    
        FromPath = "C:\Users\Lucas\Desktop\From"  '<< Change
        ToPath = "C:\Users\Lucas\Desktop\To"  '<< Change
    
        'If you want to create a backup of your folder every time you run this macro
        'you can create a unique folder with a Date/Time stamp.
        'ToPath = "C:\Users\Ron\" & Format(Now, "yyyy-mm-dd h-mm-ss")
    
        If Right(FromPath, 1) = "\" Then
            FromPath = Left(FromPath, Len(FromPath) - 1)
        End If
    
        If Right(ToPath, 1) = "\" Then
            ToPath = Left(ToPath, Len(ToPath) - 1)
        End If
    
        Set FSO = CreateObject("scripting.filesystemobject")
    
        If FSO.FolderExists(FromPath) = False Then
            MsgBox FromPath & " doesn't exist"
            Exit Sub
        End If
    
        FSO.CopyFolder Source:=FromPath, Destination:=ToPath
        MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
    
    End Sub
    Last edited by Leith Ross; 12-22-2012 at 06:10 PM. Reason: Added Code Tags

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Moving subfolders to another folder.

    There's no extension in the filenames ...

  10. #10
    Registered User
    Join Date
    06-12-2012
    Location
    Verona, New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Moving subfolders to another folder.

    This is a folder name not a file.
    Const sDirFr As String = "C:\Users\Lucas\Desktop\From\"
    Const sDirTo As String = "C:\Users\Lucas\Desktop\To\"
    Last edited by Leith Ross; 12-22-2012 at 06:10 PM.

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Moving subfolders to another folder.

    I completely missed that.

    Sub lwk()
        Const sDirFr    As String = "C:\Users\Lucas\Desktop\From\"
        Const sDirTo    As String = "C:\Users\Lucas\Desktop\To\"
        Dim sFile       As String
        Dim iRow        As Long
    
        For iRow = 2 To Cells(Rows.Count, "A").End(xlUp).Row
            sFile = Cells(iRow, "A").Value2
            If Len(Dir(sDirFr & sFile, vbDirectory)) Then
                If GetAttr(sDirFr & sFile) And vbDirectory Then
                    Name sDirFr & sFile As _
                         sDirTo & sFile & " - " & Cells(iRow, "B").Value2
                End If
            End If
        Next iRow
    End Sub

  12. #12
    Registered User
    Join Date
    06-12-2012
    Location
    Verona, New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Moving subfolders to another folder.

    Hi. Thank you this worked great.

  13. #13
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Moving subfolders to another folder.

    You're welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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