-
-
popi153
丨Lv 3
'bat对文件的读写、判断等操作极弱,所以推荐VBS。'使用也很简单:记事本输入→保存到目标文件夹,命名为“RenameText.vbs”文件→双击运行。----代码开始-----Option Explicit On Error Resume Next ' 容错语句,避免程序崩溃 '有可能重命名文件已经存在,忽略错误。 Dim fso,fs,f Dim i Dim strNewName Const strCurrentPath = "." '当前目录Msgbox "根据文本文件第一行批量重命名的VBS程序" & vbcrlf & vbcrlf & "Created By Shortway",0,"QQ:380710203" Set fso = Wscript.CreateObject("Scripting.FileSystemObject") Set fs = fso.GetFolder(strCurrentPath).Files i = 0 For Each f In fs '遍历当前文件夹内每个文件 If LCase(right(f.name,3))="txt" Then '判断是否是文本文件 strNewName = Trim(fso.OpenTextFile(f, 1, False).ReadLine) '得到第一行 '以下剔除不能作文件名的特殊字符 strNewName = Replace(strNewName, "\", "") strNewName = Replace(strNewName, "/", "") strNewName = Replace(strNewName, ":", "") strNewName = Replace(strNewName, "*", "") strNewName = Replace(strNewName, "?", "") strNewName = Replace(strNewName, """", "") strNewName = Replace(strNewName, ">", "") strNewName = Replace(strNewName, "<", "") strNewName = Replace(strNewName, "|", "") strNewName = left(strNewName,30) '有时第一行文字太多了,就选30个字符了 f.name=strNewName & ".txt" i = i + 1 End if Next Msgbox i & "个文件改名完成!(忽略重名)" Set fs = Nothing Set fso = Nothing 参考文献:Shortway 大师原创。回答是辛苦,爱问是幸福o(∩_∩)o...附件:RenameText.vbs