# 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.

Example

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.
###### Who is Participating?

Commented:
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 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
Wscript.Quit
End If

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

' Load csv file into array
.Close
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
Else
Wscript.Echo Now & " WARNING: New file already exists - " & strNewName
End If
Else
Wscript.Echo Now & " WARNING: Old file does not exist - " & strOldName
End If
End If
Next


»bp
0

Commented:
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?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.