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

Posted on 2008-11-20
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
    Well here's one from one of our MVPS'd have to modify this one as well
    LVL 38

    Accepted Solution

    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
    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

    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

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    This article was inspired by a question here at Experts Exchange ( The requirements stated in that question are (1) reduce the file size of a large number of…
    I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
    This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

    746 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

    16 Experts available now in Live!

    Get 1:1 Help Now