Solved

Mass rename files

Posted on 2014-01-03
9
177 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
  • 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 52

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
 

Author Comment

by:antonioking
ID: 39765627
Hi billprew
Have the attachments helped you out?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 52

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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now