Link to home
Start Free TrialLog in
Avatar of Nick Collins
Nick Collins

asked on

Searching & Renaming PDF Files

Searching & Renaming PDF Files

Bill you have provided some code in the past about searching and renaming PDF files...

Option Explicit

' Define files and folders to work with (NO TRAILING BACKSLASH)
Const cBaseDir = "b:\ee\ee29100342\files\[[DATE]]"
Const cControlFile = "b:\ee\ee29100342\list.csv"
Const cDelim = ","

' Text file I/O constants
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

' Declare global variables
Dim objFSO, strBaseDir, strControlFile, strDate
Dim arrControl, strControl, arrFields, i
Dim strOldName, strNewName, strOldPath, strNewPath

' Create file system object
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

' Get date in YYYYMMDD format
strDate = Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now), 2)

' Resolve file paths
strBaseDir = objFSO.GetAbsolutePathname(Replace(cBaseDir, "[[DATE]]", strDate, 1, -1, vbTextCompare))
strControlFile = objFSO.GetAbsolutePathname(cControlFile)

' Make sure base folder exists
If Not objFSO.FolderExists(strBaseDir) Then
    Wscript.Echo Now & " ERROR: Base folder does not exist - " & strBaseDir
End If

' Make sure control file exists
If Not objFSO.FileExists(strControlFile) Then
    Wscript.Echo Now & " ERROR: List file does not exist - " & strControlFile
End If

' Load csv file into array
With objFSO.OpenTextFile(strControlFile, ForReading)
    arrControl = Split(.ReadAll, vbCrLf)
End With

' Loop over each control record and process as needed (skip header row)
For i = 1 To UBound(arrControl)

    ' Split line at delimiter, gather fields
    arrFields = CSVParse(arrControl(i))
    If UBound(arrFields) = 1 Then
        strOldName = arrFields(0) & ".pdf"
        strNewName = arrFields(0) & "_" & CleanFilename(arrFields(1)) & ".pdf"
        ' Build full paths for old and new file names
        strOldPath = strBaseDir & "\" & strOldName
        strNewPath = strBaseDir & "\" & strNewName
        ' If old file exists, and new name is not used, rename.  Otherwise display warning.
        If objFSO.FileExists(strOldPath) Then
            If Not objFSO.FileExists(strNewPath) Then
                objFSO.MoveFile strOldPath, strNewPath
                Wscript.Echo Now & " WARNING: New file already exists - " & strNewName
            End If
            Wscript.Echo Now & " WARNING: Old file does not exist - " & strOldName
        End If
        End If

' Function to parse comma delimited line and return array of field values.
Function CSVParse(ByVal strLine)
    Dim arrFields
    Dim blnIgnore
    Dim intFieldCount
    Dim intCursor
    Dim intStart
    Dim strChar
    Dim strValue

    Const QUOTE = """"
    Const QUOTE2 = """"""

    ' Check for empty string and return empty array.
    If (Len(Trim(strLine)) = 0) then
        CSVParse = Array()
        Exit Function
    End If

    ' Initialize.
    blnIgnore = False
    intFieldCount = 0
    intStart = 1
    arrFields = Array()

    ' Add "," to delimit the last field.
    strLine = strLine & ","

    ' Walk the string.
    For intCursor = 1 To Len(strLine)
        ' Get a character.
        strChar = Mid(strLine, intCursor, 1)
        Select Case strChar
            Case QUOTE
                ' Toggle the ignore flag.
                blnIgnore = Not blnIgnore
            Case ","
                If Not blnIgnore Then
                    ' Add element to the array.
                    ReDim Preserve arrFields(intFieldCount)
                    ' Makes sure the "field" has a non-zero length.
                    If (intCursor - intStart > 0) Then
                        ' Extract the field value.
                        strValue = Mid(strLine, intStart, _
                            intCursor - intStart)
                        ' If it's a quoted string, use Mid to
                        ' remove outer quotes and replace inner
                        ' doubled quotes with single.
                        If (Left(strValue, 1) = QUOTE) Then
                            arrFields(intFieldCount) = _
                                Replace(Mid(strValue, 2, _
                                Len(strValue) - 2), QUOTE2, QUOTE)
                            arrFields(intFieldCount) = strValue
                        End If
                        ' An empty field is an empty array element.
                        arrFields(intFieldCount) = Empty
                    End If
                    ' increment for next field.
                    intFieldCount = intFieldCount + 1
                    intStart = intCursor + 1
                End If
        End Select
    ' Return the array.
    CSVParse = arrFields
End Function

Function CleanFilename(strName)
    Dim arrIllegal, strIllegal, i
    arrIllegal = Array("<",">",":","""","/","\","|","?","*")
    CleanFilename = Trim(strName)
    For Each strIllegal In arrIllegal
        CleanFilename = Replace(CleanFilename, strIllegal, "_")
    For i = 0 To 31
        CleanFilename = Replace(CleanFilename, Chr(i), "_")
End Function

Open in new window

I want to use the same bit of code again in an other scenario - This time the control file has slighted changed - see attached

This time i want to add the site details and date/time to the file name - removing any characters
Avatar of Bill Prew
Bill Prew

I didn't see a control file attached?

Avatar of Nick Collins


Based on the sample control file you provided, please provide examples of old and new file names for each line in the CSV control file.

Old File Name


New File Name

SC_000001_1234567_Persimmon Homes_Phase 1 Kings Copse_16th October 2018.pdf
Old File Name: SC_000001_1245367.pdf
New File Name: SC_000001_1234567_Persimmon Homes_Phase 1 Kings Copse_16th October 2018.pdf

I don't see any of those values in the sample CSV you posted earlier.  I would like a sample of the control file, and the corresponding renaming that should occur.  Just trying to save time and errors by being very clear on how the values in the CSV are used in the renaming, including spaces, underscores, special characters etc.  So show some comprehensive examples that touch on those please.

"Completed On:","Client / Site / Job Number","Internal Site Visit Number:"
"15 October 2018 02:06 PM","Site One Ltd | Bristol | Job Number: 1893","SV_000002_1067414"
"15 October 2018 02:21 PM","Mi Test Ltd | Farm Bristol | Job Number: 1871","SV_000003_1067414"

Open in new window

The file will be named "SV_000002_1067414.pdf"

Control file shows the following:

"15 October 2018 02:06 PM","Site One Ltd | Bristol | Job Number: 1893","SV_000002_1067414"

The file will be renamed as follows"SV_000002_1067414_Site One Ltd_Bristol_15_October_2018.pdf"

In the renaming process you ignore the Job Number:1893
Avatar of Bill Prew
Bill Prew

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial