• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

List file path from folder name in excel and export all files in a particular folder

Hi I have data of LAN nos in excel which is available in my folder directory. I want excel to get the file path of the same. Also I want excel to do export it into a separate folder. Is there any Macro that performs the complex observation.
 
Hi I am searching for a macro code that

Finds matching PDF files (listed in excel) in different folders and copy it to another location.

File Sample attached for your location. There are around 51 pdf files that user has to search and Copy it into separate folder.

I want macro code that serves the above purpose.

I got some clue from the below link but it search only file name not PDF files listed. Hence Require code.
 

Attachments

  • Sample LAN nos.xlsx
    9.6 KB · Views: 5
Yes PDF that's to be moved as LAN no as file name. For eg, Please find below screenshot of the same. Also I want Macro not to restrict search in the parent folder but also its subfolder.
 

Attachments

  • First File.JPG
    First File.JPG
    49.1 KB · Views: 8
  • Second file.JPG
    Second file.JPG
    44.1 KB · Views: 6
  • Third file.JPG
    Third file.JPG
    43.9 KB · Views: 5
See if this meets your need. It should loop through folders pretty quickly.
Codes are in module2 in attached file. Edit path in "Test1" as needed.

Code:
Sub PdfMove(path As String, dpath As String)
    Dim currPath As String
    Dim dirCollection As Collection
    Dim fileCollection As Collection
    Dim directory As Variant, pdffile As Variant, fnArry() As Variant
    Dim lRow As Long, i As Long, j As Long
   
    Set dirCollection = New Collection
    Set fileCollection = New Collection
    lRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
    fnArry = Sheet1.Range("A2:A" & lRow)

    currPath = Dir(path, vbDirectory)
   
    'loop through current path and add pdf files to file collection and subfolders to directory collection
    Do Until currPath = vbNullString
        If InStr(currPath, ".pdf") > 0 Then
            fileCollection.Add path & currPath
        End If
        If Left(currPath, 1) <> "." And _
            (GetAttr(path & currPath) And vbDirectory) = vbDirectory Then
            dirCollection.Add currPath
        End If
        currPath = Dir()
    Loop
   
    'Explore subfolders
    For Each directory In dirCollection
        PdfMove path & directory & "\", dpath
    Next directory
   
    'Using fileCollection copy files, to move files replace FileCopy line with below
    'Name fileCollection(j) As dpath & pdffile
    For i = LBound(fnArry, 1) To UBound(fnArry, 1)
        findString = fnArry(i, 1)
        For j = 1 To fileCollection.Count
            If InStr(fileCollection(j), fnArry(i, 1)) > 0 Then
                Sheet1.Cells(i + 1, 2) = fileCollection(j)
                pdffile = Split(fileCollection(j), "\")
                pdffile = Trim(pdffile(UBound(pdffile)))
                FileCopy fileCollection(j), dpath & pdffile
            End If
        Next j
    Next i
End Sub

Sub Test1()
    ' First is parent directory for all files, second is destination folder
    ' There is no error check so make sure you have the destination folder created beforehand
    PdfMove "C:\Test\", "C:\Test\Destination\"
End Sub
 

Attachments

  • Sample LAN nos.xlsm
    20.4 KB · Views: 15
Hi I have changed copy pasted the above VBA code but it did not work.

I have changed the

PdfMove "C:\Test\", "C:\Test\Destination\" to
PdfMove "D:\Berhampur Scan\", "D:\Wrong deletion cases\" but it did not work.
 
Can you be specific? What line gave error?

And did you make sure both directory exists beforehand?
 
Hi I am very sorry, Its mistake from my end. I was not working on updated folder. The listed no which I was searching was not at all there in the source folder.
 
Back
Top