Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Batch rename Word (.doc) files based on first line of text

Posted on 2008-11-20
Medium Priority
Last Modified: 2012-05-05
I have recently recovered a large amount of "lost" data from a drive, but was unable to recover the files with their proper names. (The recovery software just named them 0000A0A0.DOC, 0000A0A1.DOC, 0000A0A3.DOC...)
There are over a thousand documents, so I would like to rename the file with text extracted from the document itself. I was thinking, from the first line of text. This would making sorting much easier.
Does anyone know if this is possible with a batch file or something?
Question by:icsbudapest
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 23005493
Well here's one from one of our MVPS

Also....you'd have to modify this one as well
LVL 38

Accepted Solution

Shift-3 earned 2000 total points
ID: 23006592
Paste the script below into a text file with a .vbs extension.  Customize the value of the strFolder variable with the folder containing the documents.  Customize the value of the intChars variable with the number of characters to use from the first line.  Running the script will rename each .doc file in the folder.

'Adapted from http://www.microsoft.com/technet/scriptcenter/resources/qanda/apr06/hey0404.mspx
strFolder = "c:\files"
intChars = 20
arrRemove = Array("\", "/", ":", "*", "?", Chr(34), "<", ">", "|")
On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)
Set objWord = CreateObject("Word.Application")
For Each strFile in objFolder.Files
    arrNames = Split(strFile.Name, ".")
    strExt = arrNames(UBound(arrNames))
    If LCase(strExt) = "doc" Then
        Set objDoc = objWord.Documents.Open(strFile.Path)
        Set objRange = objDoc.Range(0,intChars)
        strRange = objRange.Text
        For Each strChar in arrRemove
            strRange = Replace(strRange, strChar, "")
        strNewName = strFolder & "\" & strRange & "." & strExt
        Wscript.Sleep 3000
        objFSO.MoveFile strFile.Path, strNewName
    End If

Open in new window


Author Comment

ID: 23011501
The script seemed to work well. Thank you. Just what I needed. (It may take all day to run it, but will be a time saver in the long run.)

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
Suggested Courses

581 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