Rename DOCX files using the first line of the document

Hey all,

I have about 600 files that I need to batch rename. I want to rename the file using the full first line of each doc, but am unsure how to do that. I just want to run a script that would automatically take the first line of the doc and add a .DOCX to the end.

The files are in one folder, but broken up into many subfolders. If possible to do this so that it retains the folder structure, that would be ideal.

I am fairly novice when it comes to macros and VBS, so any step-by-step help would be greatly appreciated!!

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.

I have tweaked some existing VBA macro code to do what you need. It creates a new folder structure to match the existing one and puts the renamed files in the new folders.  You will have to edit the paths in the CallGetFiles procedure to suit your situation.

Note that the only check on the text is that it has at least one character. Illegal filename characters aren't checked for, and if the name is the same as for a previous file in the same folder, the earlier file will be overwritten.

Sub CallGetFiles()
    GetFiles "C:\MyFolder", "C:\MyNewFolder", "*.doc*"
End Sub

Sub GetFiles(strInFolder As String, strOutFolder As String, strFilePattern As String)
    Dim strFileName As String
    Dim strInFolders() As String
    Dim strOutFolders() As String
    Dim iFolderCount As Integer
    Dim iFileCount As Integer
    Dim i As Integer
    Dim strFileNames() As String
    Dim strFileFullName As String
    Dim strText As String
    'collect child folders
    strFileName = Dir$(strInFolder & "\", vbDirectory)
    'On Error GoTo ErrHandle
    Do Until strFileName = ""
        If (GetAttr(strInFolder & "\" & strFileName) And vbDirectory) = vbDirectory Then
            If Left$(strFileName, 1) <> "." Then
                ReDim Preserve strInFolders(iFolderCount)
                ReDim Preserve strOutFolders(iFolderCount)
                strInFolders(iFolderCount) = strInFolder & "\" & strFileName
                strOutFolders(iFolderCount) = strOutFolder & "\" & strFileName
                iFolderCount = iFolderCount + 1
            End If
        End If
        strFileName = Dir$()
    'collect files in current folder
    iFileCount = -1
    strFileName = Dir$(strInFolder & "\" & strFilePattern)
    Do Until strFileName = ""
        DoEvents 'let other processes have time
        iFileCount = iFileCount + 1
        ReDim Preserve strFileNames(iFileCount)
        strFileNames(iFileCount) = strFileName
        strFileName = Dir$()
    'process files in current folder
    For i = 0 To iFileCount
        strFileFullName = strInFolder & "\" & strFileNames(i)
        RenameDoc strFileFullName, strOutFolder
    Next i
    'walk through child folders
    For i = 0 To iFolderCount - 1
        GetFiles strInFolders(i), strOutFolders(i), strFilePattern
    Next i
    Exit Sub
    Writelog Err.Number & ", " & Err.Description
    Writelog strInFolder & "\" & strFileName
End Sub
Sub RenameDoc(strFileFullName As String, strOutFolder As String)
    Dim strNewName As String
    Dim doc As Document
    If Right(strOutFolder, 1) <> ":" Then
        If Dir$(strOutFolder, vbDirectory) = "" Then
            MkDir strOutFolder
        End If
    End If
    Set doc = Documents.Open(strFileFullName)
    If Len(doc.Paragraphs.First.Range.Text) > 1 Then
        strNewName = Replace(doc.Paragraphs.First.Range.Text, vbCr, "")
        doc.SaveAs strOutFolder & "\" & strNewName & ".docx", wdFormatXMLDocument
        MsgBox "First paragraph is empty in document: " & strFileFullName
    End If
    doc.Close wdDoNotSaveChanges

End Sub

Open in new window

Bill PrewIT / Software Engineering ConsultantCommented:
Here is a small stand alone VBS script that should do the job.  As always test first before running against real files since their current names will be changed (lost) in the process.

Saves as a VBS file, adjust the base path for your files near the top, and run as follows.

cscript EE28696976.vbs

' Define folder to look for files in
strBaseDir = "B:\EE\EE28696976\Files"

' Create needed objects
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWord = CreateObject("Word.Application") 
objWord.Visible = False
objWord.DisplayAlerts = False

' Recursively process folders

' Done

Sub ProcessFolder(objFolder)
   ' Process each file and rename it
   For Each objFile In objFolder.Files
      strNewName = NewName(objFile)
      If LCase(objFile.Name) <> LCase(strNewName) Then
         objFile.Name = strNewName
      End If

   ' Process any subfolders of this one
   For Each objSubFolder In objFolder.Subfolders
      ProcessFolder objSubFolder
End Sub

' Get new name from first line of Word file
Function NewName(objFile)
   ' Open the Word document
   objWord.Documents.Open objFile.Path, False, True
   Set objDocument = objWord.Documents(1)

   ' Get the 1st line
   NewName = Replace(objDocument.Paragraphs(1).Range, vbCr, "") & ".docx"

   ' Close the Word document
   Set objDocument = Nothing
End Function

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
Martin LissOlder than dirtCommented:
I've requested that this question be closed as follows:

Accepted answer: 500 points for GrahamSkan's comment #a40880753

for the following reason:

No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: GrahamSkan (http:#a40880753)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Experts-Exchange Cleanup Volunteer
Bill PrewIT / Software Engineering ConsultantCommented:
Since the poster tagged both VBA and VBS zones, and since they mentioned both in their actual post, I don't think it is know which is preferred.  Since we have a working solution for each, I would suggest both should be accepted as viable solutions, rather than just one.

I have no objection to Bill's proposal
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
Microsoft Word

From novice to tech pro — start learning today.