Solved

Mass rename files

Posted on 2014-01-03
9
181 Views
Last Modified: 2014-04-25
I have 100s of pdf's in a folder.
Please can someone help me out with a script that can interrogate each file and rename it based on certain content found?

The PDFs were created with Distiller.
When opened in notepad I can see a line of text that contains the names I'd like to use for the pdfs...
/Title (TITLE HEADING)/

Open in new window



I'd like the script to take the text inside those brackets and rename the file to that text.
Thanks in advance!

Regards
0
Comment
Question by:antonioking
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 

Author Comment

by:antonioking
ID: 39754047
If the string '/title(' appears more than once, can the script work from the bottom upwards so it takes the first occurrence from the bottom rather than the first occurrence from the top?
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39754638
Can you provide several PDF example files for testing?

~bp
0
 

Author Comment

by:antonioking
ID: 39754691
Sure
See attached zip

Thanks
example-pdfs.zip
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:antonioking
ID: 39765627
Hi billprew
Have the attachments helped you out?
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39770140
Nearly have it, but for some reason one of the PDFs acts differently than the others, chasing that down...

~bp
0
 

Author Comment

by:antonioking
ID: 39770397
Thanks bp!
I appreciate your help!
0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39774190
Okay, I haven't been able to figure out why one of the sample files you provided doesn't process properly in the VBS script. The PDF files do contain binary data so maybe that's confusing things.  But I figured I'd give you what I had and you could try it there.  It will write output to the console of the process, so you want to run this from a command console (MS-DOS) using cscript or in a BAT file. You might also want to write the console output to a file so you can read it easier afterwards, like:

cscript EE28329923.vbs > logfile.txt

Sorry I haven't nailed the problem yet, but thought something might be better than anything, and perhaps another Expert will see a problem.

' Define needed constants
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

' Define default folder to search
sBaseDir = "C:\EE\EE28329923\Files\"
sBaseExt = ".pdf"

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

' If a parm is passed on command line override the default folder path
If (WScript.Arguments.Count > 0) Then
   sBaseDir = WScript.Arguments(0)
End If

' Make sure the folder exists, if not exit
If Right(sBaseDir, 1) <> "\" Then sBaseDir = sBaseDir & "\"
If Not oFSO.FolderExists(sBaseDir) Then
   WScript.Echo "*ERROR* - Specified folder does not exist."
   WScript.Quit
End If

' Access the folder
Set oFolder = oFSO.GetFolder(sBaseDir)

' Process each file in the folder
For Each oFile in oFolder.Files

   ' See if it matches the pattern of filenames we want
   If LCase(Right(oFile.Name, Len(sBaseExt))) = LCase(sBaseExt) Then

      ' Save current name of file
      sOldName = oFile.Name

      ' Open the input and output files
      Set oInfile = oFSO.OpenTextFile(oFile.Path, ForReading, False, TriStateFalse)

      ' Read entire input file into a variable and close it
      sData = oInfile.ReadAll
      oInfile.Close

      ' Find matching patterns via a regex
      Set oRegExp = New RegExp
      oRegExp.Global = True
      oRegExp.IgnoreCase = True
      oRegExp.Pattern = "(/Title *\()([^\)]+)\)"

      ' If we found a match, rename using the last match in the file
      Set oFound = oRegExp.Execute(sData)
      If oFound.Count > 0 Then
         sNewName = oFound.Item(oFound.Count-1).Submatches.Item(1)& sBaseExt
         ' Check for any errors renaming the file
         On Error Resume Next
         oFile.Name = sNewName
         If Err.Number = 0 Then
            Wscript.Echo "Renamed: '" & sOldName & "' -> '" & sNewName & "'"
         Else
            Wscript.Echo "Error [" & Err.Number & "] renaming: '" & sOldName & "' -> '" & sNewName & "'"
         End If
         On Error Goto 0
      Else
         Wscript.Echo "No /Title in: '" & sOldName & "'"
      End If

   End If

Next

Open in new window

~bp
0
 

Author Comment

by:antonioking
ID: 39782431
Fantastic! Seems to be working for most, but some not renamed.
I've attached a couple that haven't been renamed

Could you change the script so it can rename all pdf's in the sub-folders too?

Thanks so much for your help.
More-examples.zip
0
 

Author Closing Comment

by:antonioking
ID: 40022013
Thanks bp!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question