Finding PDFs and Renaming

I’m looking for a possible procedure...

I have a list of PDF documents in a text file and I want to search for that PDF document in another folder. When it finds a match, i want  it to renamed with a new name corresponding to the name in the original.


Text File

Test.pdf, NewName.pdf
Test1.pdf, NewName1.pdf
Test2.pdf, NewName2.pdf

The procedure should work by going through each line to the find the file and rename with the new name.
Nick CollinsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

What do you want to happen when the source file isn't found?
What do you want to happen if the source file can't be renamed?
What do you want to happen when the target file already exists?
Bill PrewCommented:
This should give you a good starting place.  Save as a VBS and adjust paths near top as needed.  run from command line as:

cscript EE29098040.vbs

Option Explicit

' Define files and folders to work with
Const cBaseDir = "b:\ee\ee29098040\files"
Const cControl = "b:\ee\ee29098040\list.csv"
Const cDelim = ","

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

' Declare global variables
Dim objFSO
Dim arrControl, strControl, arrFields
Dim strOldName, strNewName, strOldPath, strNewPath

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

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

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

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

' Loop over each control record and process as needed
For Each strControl In arrControl

    ' Split line at delimiter, gather fields
    arrFields = Split(strControl, cDelim)
    If UBound(arrFields) = 1 Then
        strOldName = arrFields(0)
        strNewName = arrFields(1)
        ' Build full paths for old and new file names
        strOldPath = cBaseDir & "\" & strOldName
        strNewPath = cBaseDir & "\" & 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

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.