Consulting

Results 1 to 6 of 6

Thread: Move files from different folders to single folder and delete the original files

  1. #1

    Move files from different folders to single folder and delete the original files

    Hello,

    I have more than 100 different folders in a common folder and inside which there are PDF are stored.

    The list of file names stored inside each folder is known and name of the folders are also known.

    I want to get back PDF files and delete original files inside the folders.

    Need your help to VBA code for the above scenario.I hereby attach the excel file for your reference.

    Move and Delete Original Files.xlsx

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Sub test()
        Dim r As Range
        Dim k As Long
        Dim src As String, dst As String, fn As String
        
        Set r = Cells(1).CurrentRegion
        
        For k = 2 To r.Rows.Count
            fn = r.Cells(k, 2).Value
            src = r.Cells(k, 1).Value & "\" & fn
            dst = r.Cells(k, 3).Value & "\" & fn
    
            If (Dir(src) <> "") * (Dir(dst) = "") Then
                Name src As dst
                r.Cells(k, 4).Value = "Moved"
            End If
        Next
        
    End Sub


    マナ

  3. #3
    Dear Mana,
    Thanks for the support.
    If the source folder have different files((file names),then it is working fine.
    But if one or more source folders having same files(file names),then it is not working
    Pls help..

  4. #4
    Dear Mana,
    Sorry now the code is working well
    I create seperate folder in destination to retrive the files from source folders
    Now files having same filename also moved to destination
    Thanks for your exreme support.

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    @mana

    No need for an object variable: an array suffices.
    No further slow interaction with the sheet required.
    .

    Sub test()
      sn = Cells(1).CurrentRegion
        
      For j = 2 To ubound(sn)
       sn(j,4)=sn(j,1) & "\" & sn(j,2)
       sn(j,3)=sn(j,3) & "\" & sn(j,2)
       If (Dir(sn(j,1)) <> "") * (Dir(sn(j,3)) = "") Then Name sn(j,1) As sn(j,3)
      Next
    
      Cells(1).CurrentRegion = sn
    End Sub

  6. #6
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Thanks for the useful advice.

    Quote Originally Posted by snb View Post
    @mana

    No need for an object variable: an array suffices.
    No further slow interaction with the sheet required.
    .

Posting Permissions

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