自动为批量Word文档重命名等

在网上下载的Word文档,名称往往是一长串数字,如果下载的数量多了,根本无法辨别每个文档是什么内容,如果逐一打开再重新命名,速度比较慢,工作量也比较大。如果能够自动提取文档的第一行,作为文件的名称就好了。

可以用脚本来做。

1、用记事本新建一个文本文件,把它保存为“批量重命名.vbs”(注意不要弄成了“批量重命名.vbs.txt”,也就是要确保其扩展名为“.vbs”);

2、把下列代码粘贴到这个VBS文件中:
Option Explicit

Const g_strRootPath = "c:\Temp\docs\Word\ToRename\" ' 指定存放所有文件的目录,可以有子目录
Const g_nTitleMaxLen = 16 ' 指定获取文档里面第一段中的前多少个字符来作为文件名

Call Main

' 主函数入口
Sub Main()

Dim fso, oFolder, oWordApp

Set oWordApp = CreateObject("Word.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(g_strRootPath)

RenameDocFilesUnderFolder oWordApp, fso, oFolder

oWordApp.Quit
Set oWordApp = Nothing

MsgBox "完成!"

End Sub

' 重命名指定文件夹(递归)下面的所有Word文件,按照文件里面的第一句可见的文字命名
Sub RenameDocFilesUnderFolder(oWordApp, fso, oFolder)

Dim oSubFolder, oFile, oDoc
Dim strTitle, strFileName

For Each oSubFolder In oFolder.SubFolders
RenameDocFilesUnderFolder oWordApp, fso, oSubFolder
Next

For Each oFile In oFolder.Files
Set oDoc = oWordApp.Documents.Open(oFile.Path)
strTitle = GetFirstVisibleTextContent(oDoc)
oDoc.Close
Set oDoc = Nothing

If Len(strTitle) <> 0 Then
strFileName = fso.BuildPath(fso.GetParentFolderName(oFile.Path), strTitle & "." & fso.GetExtensionName(oFile.Path))
strFileName = GetUniqueFileName(fso, strFileName)
fso.MoveFile oFile.Path, strFileName
End If
Next

End Sub

' 获取指定文档第一行可见文字
Function GetFirstVisibleTextContent(oDoc)

Dim oParagraph
Dim strContent

For Each oParagraph In oDoc.Paragraphs
strContent = GetSafeFileName(oParagraph.Range.Text)
If Len(strContent) <> 0 Then
GetFirstVisibleTextContent = strContent
Exit Function
End If
Next

GetFirstVisibleTextContent = ""

End Function

' 过滤文件名里面的无效字符
Function GetSafeFileName(strFileName)

Dim arrUnsafeCharacters, strUnsafeChar
Dim nIndex

arrUnsafeCharacters = Array("\", "/", ":", "*", "?", """", "<", ">", "|")

For nIndex = 0 To &H2F
strFileName = Replace(strFileName, Chr(nIndex), "")
Next

For Each strUnsafeChar In arrUnsafeCharacters
strFileName = Replace(strFileName, strUnsafeChar, "")
Next

GetSafeFileName = Left(Trim(strFileName), g_nTitleMaxLen)

End Function

' 获取不重复的文件名,如果有重名则在文件名后面附加“_1”、“_2”……
Function GetUniqueFileName(fso, strFullName)

Dim strParentFolder, strBaseName, strExtensionName
Dim nIndex

If Not fso.FileExists(strFullName) Then
GetUniqueFileName = strFullName
Exit Function
End If

strParentFolder = fso.GetParentFolderName(strFullName)
strBaseName = fso.GetBaseName(strFullName)
strExtensionName = fso.GetExtensionName(strFullName)

nIndex = 0

While fso.FileExists(strFullName)
nIndex = nIndex + 1
strFullName = fso.BuildPath(strParentFolder, strBaseName & "_" & nIndex & "." & strExtensionName)
Wend

GetUniqueFileName = strFullName

End Function

3、修改代码中开始部分的两个设置,即:存放等待重命名的Word文件的根目录,以及获取文档第一段内容时最多保留多少个字符。

4、保存这个VBS文件,在资源管理器中双击运行它,直到看见“完成”!

5、检查所有文件是否已自动重命名。

注意:如果有两个以上的文档依据其内容提取出来的文字相同,则会自动在文件名后面附加“_1”、“_2”、“_3”……。

如果有什么问题,请和我联系。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2022-11-03
亲测可用,好不容易找到的,希望下次搜的人也能找到:缺点就是遇到错误的情况不会跳过,不会整。
Dim fso,fc,f,strName
Set fso = CreateObject("scripting.filesystemobject")
Set fc=fso.GetFolder(".").Files
For Each f In fc
If LCase(fso.GetExtensionName(f))="doc" Then
f.Name=GetFileName(f.Path) & ".docx"
End If
If LCase(fso.GetExtensionName(f))="docx" Then
f.Name=GetFileName(f.Path) & ".docx"
End If
Next
Set fso=Nothing

Function GetFileName(FilePath)
Dim i,objWord,Doc
Set objWord=CreateObject("word.application")
Set Doc=objWord.Documents.Open(FilePath)
For i=1 To Doc.Paragraphs.Count
If Replace(Doc.Paragraphs(i).Range," ","")<>vbCr Then
GetFileName=Replace(Doc.Paragraphs(i).Range,vbCr,"")
Exit For
End If
Next
objWord.Quit
Set objWord=Nothing
End Function
第2个回答  2019-12-30

使用这款高速处理的。提取文档的首段作为文件名。

第3个回答  2010-06-27
还没有这个吧,你还要幸苦了