Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Mass rename files

Posted on 2014-01-03
9
Medium Priority
?
183 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 57

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

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

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 57

Accepted Solution

by:
Bill Prew earned 2000 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

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!

Question has a verified solution.

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

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

670 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