房天下 > 房天下问答 > 家居装修 > 装修前
  • 纠正答案:求提取txt文件第一行作为文件名的批处理命令bat

    纠正答案:求提取txt文件第一行作为文件名的批处理命令bat 上次给的答案不太直接,Shortway 大师给的方法很不错。Shortway 大师,您能否再加段代码,文件名太长了, 30个字节以内最好积分不足,改天一定奉送60分

    油漆涂料 举报

    提问者:尤龙工作室

    发布于2010-12-27

相关搜索推荐

共1个回答
  • 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
    +11 2010-12-28 举报
免责声明:问答内容均来源于互联网用户,房天下对其内容不负责任,如有版权或其他问题可以联系房天下进行删除。