Consulting

Results 1 to 10 of 10

Thread: Copy and Move files to existing Sub-folders based on filename/Sub-folders's Name

  1. #1

    Copy and Move files to existing Sub-folders based on filename/Sub-folders's Name

    Hi, there
    Would you please to advise VBA code for copying and moving files to existing Sub-folders based on Filename/Sub-folders's Name? The date of Filenames will be changed daily. Meanwhile, it's better to have path selection for selecting the source folder to copy those files and paste them to destination folder.
    Attached Images Attached Images

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Option Explicit
    
    
    Sub test()
        Dim pFld As String, cFld As String, myFile As String
           
        With Application.FileDialog(msoFileDialogFolderPicker)
            If .Show Then
                pFld = .SelectedItems.Item(1) & "\"
            Else
                Exit Sub
            End If
        End With
        
        myFile = Dir(pFld & "*.*")
      
        Do While myFile <> ""
            cFld = Mid(myFile, 1, InStrRev(myFile, " ") - 1)
            Name pFld & myFile As pFld & cFld & "\" & myFile
            myFile = Dir()
        Loop
        
    End Sub

  3. #3
    Thanks Mana!! It works great! Nevertheless, those files are located in another other drive (not same as Folder location). Could you please help to advise the code again?

  4. #4
    Thanks Mana!! It works great! Nevertheless, those files are located in another other drive (not same as Folder location). Could you please help to advise the code again?

  5. #5
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Option Explicit
      
    Sub test2()
        Dim srcFld As String, dstFld As String, subFld As String, myFile As String
         
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = "C:\*****\*****"
            .Title = "select source folder"
            If .Show Then
                srcFld = .SelectedItems.Item(1) & "\"
            Else
                Exit Sub
            End If
        End With
        
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = "D:"
            .Title = "select destination folder"
            If .Show Then
                dstFld = .SelectedItems.Item(1) & "\"
            Else
                Exit Sub
            End If
        End With
         
        myFile = Dir(srcFld & "*.*")
         
        Do While myFile <> ""
            subFld = Mid(myFile, 1, InStrRev(myFile, " ") - 1)
            Name srcFld & myFile As dstFld & subFld & "\" & myFile
            myFile = Dir()
        Loop
         
    End Sub

  6. #6
    Thanks Mana!! Nevertheless, It has error message after running new code.


    Quote Originally Posted by mana View Post
    Option Explicit
      
    Sub test2()
        Dim srcFld As String, dstFld As String, subFld As String, myFile As String
         
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = "C:\*****\*****"
            .Title = "select source folder"
            If .Show Then
                srcFld = .SelectedItems.Item(1) & "\"
            Else
                Exit Sub
            End If
        End With
        
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = "D:"
            .Title = "select destination folder"
            If .Show Then
                dstFld = .SelectedItems.Item(1) & "\"
            Else
                Exit Sub
            End If
        End With
         
        myFile = Dir(srcFld & "*.*")
         
        Do While myFile <> ""
            subFld = Mid(myFile, 1, InStrRev(myFile, " ") - 1)
            Name srcFld & myFile As dstFld & subFld & "\" & myFile
            myFile = Dir()
        Loop
         
    End Sub
    Attached Images Attached Images

  7. #7
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    vOption Explicit 
    
    Sub test3()
        Dim srcFld As String, dstFld As String, subFld As String, myFile As String
        Dim n As Long
         
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = "C:\*****\*****"
            .Title = "select source folder"
            If .Show Then
                srcFld = .SelectedItems.Item(1) & "\"
            Else
                Exit Sub
            End If
        End With
         
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = "D:"
            .Title = "select destination folder"
            If .Show Then
                dstFld = .SelectedItems.Item(1) & "\"
            Else
                Exit Sub
            End If
        End With
         
        myFile = Dir(srcFld & "*.*")
      
        Do While myFile <> ""
            n = InStrRev(myFile, " ") - 1
            If n > 0 Then
                subFld = Mid(myFile, 1, n)
                Name srcFld & myFile As dstFld & subFld & "\" & myFile
            End If
            myFile = Dir()
        Loop
         
    End Sub

  8. #8
    Run Time Error "53" after running the revised code.

    Quote Originally Posted by mana View Post
    vOption Explicit 
    
    Sub test3()
        Dim srcFld As String, dstFld As String, subFld As String, myFile As String
        Dim n As Long
         
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = "C:\*****\*****"
            .Title = "select source folder"
            If .Show Then
                srcFld = .SelectedItems.Item(1) & "\"
            Else
                Exit Sub
            End If
        End With
         
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = "D:"
            .Title = "select destination folder"
            If .Show Then
                dstFld = .SelectedItems.Item(1) & "\"
            Else
                Exit Sub
            End If
        End With
         
        myFile = Dir(srcFld & "*.*")
      
        Do While myFile <> ""
            n = InStrRev(myFile, " ") - 1
            If n > 0 Then
                subFld = Mid(myFile, 1, n)
                Name srcFld & myFile As dstFld & subFld & "\" & myFile
            End If
            myFile = Dir()
        Loop
         
    End Sub
    Attached Images Attached Images

  9. #9
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    rule of flename


    Sub-folders's Name & space & date & ,extension


    Option Explicit
     
    Sub test4()
        Dim srcFld As String, dstFld As String, subFld As String, myFile As String
        Dim fso As Object, f As Object
        Dim n As Long
         
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = "C:\*****\*****"
            .Title = "select source folder"
            If .Show Then
                srcFld = .SelectedItems.Item(1)
            Else
                Exit Sub
            End If
        End With
         
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = "D:"
            .Title = "select destination folder"
            If .Show Then
                dstFld = .SelectedItems.Item(1)
            Else
                Exit Sub
            End If
        End With
        
        Set fso = CreateObject("Scripting.FileSystemObject")
        
        For Each f In fso.GetFolder(srcFld).Files
            myFile = f.Name
            n = InStrRev(myFile, " ") - 1
            If n > 1 Then
                subFld = dstFld & "\" & Mid(myFile, 1, n)
                If fso.FolderExists(subFld) Then
                    f.Move subFld & "\"
                End If
            End If
        Next
         
    End Sub

  10. #10
    Below code works great. Thank you very much!!

    Quote Originally Posted by mana View Post
    rule of flename


    Sub-folders's Name & space & date & ,extension


    Option Explicit
     
    Sub test4()
        Dim srcFld As String, dstFld As String, subFld As String, myFile As String
        Dim fso As Object, f As Object
        Dim n As Long
         
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = "C:\*****\*****"
            .Title = "select source folder"
            If .Show Then
                srcFld = .SelectedItems.Item(1)
            Else
                Exit Sub
            End If
        End With
         
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = "D:"
            .Title = "select destination folder"
            If .Show Then
                dstFld = .SelectedItems.Item(1)
            Else
                Exit Sub
            End If
        End With
        
        Set fso = CreateObject("Scripting.FileSystemObject")
        
        For Each f In fso.GetFolder(srcFld).Files
            myFile = f.Name
            n = InStrRev(myFile, " ") - 1
            If n > 1 Then
                subFld = dstFld & "\" & Mid(myFile, 1, n)
                If fso.FolderExists(subFld) Then
                    f.Move subFld & "\"
                End If
            End If
        Next
         
    End Sub

Tags for this Thread

Posting Permissions

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