How to rename Thousands of Word Documents based on the contents of the Document

Due to a tech here reimaging a Hard drive and wiping the contents I have gotten back all the data but they are numbered File001  file 002 ect I have tried to do a script

Set objWord = CreateObject("Word.Application")

Set objDoc = objWord.Documents.Open("c:\scripts\test.doc")

Set objRange = objDoc.Range(0,5)
strNewName = "C:\Scripts\" & objRange.Text & ".doc"

Wscript.Sleep 3000

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "C:\Scripts\Test.doc", strNewName

Open in new window

Now that only renames the test .doc and try as I might I cannot get this to do Multiple files  as I dont have the Knowledge

Thank you in Advance
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
See if this helps, it loops through all DOC files in the specified folder (update to be yours) and renames to a destination folder (could be same as original location, but I left it flexible so it could be different too).

' Constants for file locations and replacement text
Const BaseDir = "B:\EE\EE28689779\Old"
Const DestDir = "B:\EE\EE28689779\New"
Const BaseExt = ".doc"
' Access the folder of files to process
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(BaseDir)

' Load Word
Set objWord = CreateObject("Word.Application")

' Process each file in the folder
For Each objFile in objFolder.Files

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

      ' Open this file in Word
      Set objDoc = objWord.Documents.Open(objFile.Path)

      ' Get text for new file name and build it
      Set objRange = objDoc.Range(0,5)
      strNewName = DestDir & "\" & objRange.Text & BaseExt

      ' Close document
      Wscript.Sleep 3000

      ' Rename document to new name from file contents
      objFSO.MoveFile objFile.Path, strNewName

   End If

' Close Word

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
astynesAuthor Commented:
Thank You so Very Much ,Much appreciated

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Word Processors

From novice to tech pro — start learning today.