Solved

Mass rename files

Posted on 2014-01-03
9
182 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 56

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

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

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 56

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

630 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