Solved

Mass rename files

Posted on 2014-01-03
9
176 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 51

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 51

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 51

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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