Parse and rename multiple .pdf files

Discussion in 'Coding' started by ANDRE726, Feb 21, 2018.

  1. ANDRE726

    ANDRE726

    Joined:
    Feb 21, 2018
    Likes Received:
    0
    I have several hundred .pdf file and many have names as follows:
    0000000123_ABCD (United States)_234_5678.pdf
    I would like to rename this file, and others like it
    0000000123_ABCD_234_5678.pdf
    How may I accomplish this with vba?
    Thanks in advance.
     
    ANDRE726, Feb 21, 2018
    #1
    1. Advertisements

  2. ANDRE726

    AmjiBhai

    Joined:
    Feb 21, 2018
    Likes Received:
    42
    Here you are....
    Thanks.ref: https://stackoverflow.com/questions...g-files-that-meet-specific-criteria-using-vba


    Sub Tester()

    Dim fls, f, crit
    crit = "(UNITED STATES)" 'make appropriate changes on this line

    Range("A1").Select

    Set fls = GetFiles("D:\Amjad\VBA RND\", crit, "*.pdf") ' 'make appropriate changes on this line
    For Each f In fls
    ActiveCell = f
    ActiveCell.EntireColumn.AutoFit
    nf = Replace(f, crit, "")
    If Trim(f) <> "" Then
    FileCopy f, nf
    ActiveCell.Offset(0, 1) = nf
    ActiveCell.Offset(0, 1).EntireColumn.AutoFit
    Kill f
    ActiveCell.Offset(1, 0).Select
    End If
    Next f

    End Sub



    Function GetFiles(path As String, crit As Variant, Optional pattern As String = "") As Collection
    Dim rv As New Collection, f
    If Right(path, 1) <> "\" Then path = path & "\"
    f = Dir(path & pattern)
    Do While Len(f) > 0
    If InStr(1, f, crit) Then
    rv.Add path & f
    Else
    rv.Add " "
    End If
    f = Dir() 'no parameter

    Loop

    Set GetFiles = rv

    End Function
     
    AmjiBhai, Feb 22, 2018
    #2
    Ian likes this.
    1. Advertisements

  3. ANDRE726

    ANDRE726

    Joined:
    Feb 21, 2018
    Likes Received:
    0
    If I am working with pdf files, where does the A1 select come in here? I am not working with excel files.
     
    ANDRE726, Feb 23, 2018
    #3
  4. ANDRE726

    AmjiBhai

    Joined:
    Feb 21, 2018
    Likes Received:
    42
    The code which I provide will get to those pdf files and will eventually rename them as you sought. but for this you will open a blank excel file and follow these steps:
    1) press alt+F11
    2) insert a module
    3) Copy paste the following code and make appropriate changes in the red-highlighted lines
    4) Run the code ...Tester

    Sub Tester()

    Dim fls, f, crit
    crit = "(UNITED STATES)" 'make appropriate changes on this line

    Range("A1").Select

    Set fls = GetFiles("D:\Amjad\VBA RND\", crit, "*.pdf") ' 'make appropriate changes on this line
    For Each f In fls
    ActiveCell = f
    ActiveCell.EntireColumn.AutoFit
    nf = Replace(f, crit, "")
    If Trim(f) <> "" Then
    FileCopy f, nf
    ActiveCell.Offset(0, 1) = nf
    ActiveCell.Offset(0, 1).EntireColumn.AutoFit
    Kill f
    ActiveCell.Offset(1, 0).Select
    End If
    Next f

    End Sub



    Function GetFiles(path As String, crit As Variant, Optional pattern As String = "") As Collection
    Dim rv As New Collection, f
    If Right(path, 1) <> "\" Then path = path & "\"
    f = Dir(path & pattern)
    Do While Len(f) > 0
    If InStr(1, f, crit) Then
    rv.Add path & f
    Else
    rv.Add " "
    End If
    f = Dir() 'no parameter

    Loop

    Set GetFiles = rv

    End Function
     
    AmjiBhai, Feb 23, 2018
    #4
  5. ANDRE726

    Abraham Andres Luna

    Joined:
    Mar 14, 2018
    Likes Received:
    107
    Location:
    Spring Hill, FL 34608
    You could also use the Replace function on strings to remove the "(United States)" from the file name.
     
    Abraham Andres Luna, Mar 14, 2018
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.