找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 21240|回复: 12

[问题求助] 求教两个字符串之间内容命名文件名

[复制链接]
发表于 2019-3-9 15:42:03 | 显示全部楼层 |阅读模式
我有一堆.html文件都是以数字命名,但不规则。我要以里面<title>...</title>的内容为html文件名,若用bat写经常出现响应错误,里面东西都没了。还有用vbs的话replace不支持通配符,该咋弄。
发表于 2019-3-9 16:40:03 | 显示全部楼层
本帖最后由 ivor 于 2019-3-9 21:06 编辑
  1. @Powershell "& {[ScriptBlock]::Create("'#' + (gc '%~f0' -raw)").Invoke()}" & pause & goto :eof
  2. dir *.html | %{ren $_.Name ([regex]::Replace(([regex]::Match((gc $_),'(?<=<title>).*(?=</title>)').Value),'[\\/:*?"<>|]',"")+$_.Extension)}
复制代码
已经过滤了特殊字符
 楼主| 发表于 2019-3-9 16:56:22 | 显示全部楼层
我试了,提示无效字符。
发表于 2019-3-9 17:05:27 | 显示全部楼层
  1. @echo off
  2. powershell -nologo -noprofile -command "foreach( $file in ( dir *.html -r ) ){([regex] '<title>(.*)</title>').Match( (type $file -ReadCount 0)).Groups[1]|foreach-object{ren -Path $file -NewName  "$($_.Value).html"}}"
复制代码
 楼主| 发表于 2019-3-9 20:39:38 | 显示全部楼层
本帖最后由 jieyuan_1981 于 2019-3-9 22:22 编辑

回复 4# yhcfsr


    谢谢你的回答,但提示语句未结束。
发表于 2019-3-9 21:35:30 | 显示全部楼层
已经过滤了特殊字符
 楼主| 发表于 2019-3-9 21:55:22 | 显示全部楼层
回复 6# ivor
 楼主| 发表于 2019-3-9 22:07:40 | 显示全部楼层
本帖最后由 jieyuan_1981 于 2019-3-9 22:16 编辑

回复 2# ivor


    我在本论坛看了几篇无效字符的帖子,好像是关键字或赋值方法的问题,还有是vba代码不能用在vbs中。比如:open Filename 就不行,open就行。你给我的答案是不是这块有问题。另外我是xp系统,是否不识别。
发表于 2019-3-9 22:17:40 | 显示全部楼层
我们给的都是批处理代码,你放到vbs里面肯定不能运行了
加我qq250193966,帮你看看
发表于 2019-3-9 22:41:32 | 显示全部楼层
本帖最后由 523066680 于 2019-3-9 22:44 编辑

即使是XP,涉及HTML解析的问题仍然建议换别的语言工具(lua, python, ahk, 什么都好,lua还能转exe呢)。
提取 <title> 示例
  1. use Mojo::DOM;
  2. use File::Slurp;
  3. grep { printf "%s %s\n", $_, Mojo::DOM->new(read_file $_)->at("title")->text } glob "*.html";
复制代码
发表于 2019-3-10 00:32:38 | 显示全部楼层
  1. '代码可用,扫码头像,随意赞助;有什么问题,可加QQ956535081及时沟通
  2. If LCase(Right(WSH.FullName,12)) = "\wscript.exe" Then
  3.     CreateObject("WScript.Shell").Run "cmd /c cscript.exe -nologo """ & WSH.ScriptFullName & """&pause&exit"
  4.     WSH.Quit
  5. End If

  6. Set fso=CreateObject("Scripting.FileSystemObject")
  7. RootPath=fso.GetFile(WSH.ScriptFullName).ParentFolder.Path
  8. arr=split("\ / : * ? \ "" < > | " & Chr(9)," ")
  9. getfile RootPath

  10. Function getfile(path)
  11.     Set oFolder=fso.GetFolder(path)
  12.     Set oSubFolders=oFolder.SubFolders
  13.       
  14.     Set oFiles=oFolder.Files
  15.     For Each oFile In oFiles
  16.         ext=fso.GetExtensionName(oFile.Path)
  17.         If Lcase(ext) = "html" Or Lcase(ext) = "htm" Then
  18.             text=gethtml(oFile.Path)
  19.             title=gettitle(text)
  20.             NewName=title & "." & ext
  21.             WSH.echo oFile.Path & " --> " & NewName
  22.             If title <> "" Then
  23.                 If Not fso.FileExists(oFile.ParentFolder.Path & "" & NewName) Then
  24.                     oFile.Name=NewName
  25.                 Else
  26.                     WSH.echo "文件重名"
  27.                 End If
  28.             End If
  29.         End If
  30.     Next
  31.       
  32.     For Each oSubFolder In oSubFolders
  33.         getfile(oSubFolder.Path)
  34.     Next
  35.     Set oFolder=Nothing
  36.     Set oSubFolders=Nothing
  37. End Function

  38. Function gethtml(ByVal htmlfile)
  39.     htmltext=""
  40.     Set ado=CreateObject("ADODB.Stream")
  41.     ado.Type=2
  42.     '注意html文件的编码,如果是gbk或gb2312,修改下面的"utf-8"为"gb2312"
  43.     ado.Charset="utf-8"
  44.     ado.Open
  45.     ado.LoadFromFile htmlfile
  46.     ado.Position=0
  47.     htmltext=ado.ReadText
  48.     ado.Close
  49.     Set ado=Nothing
  50.     gethtml=htmltext
  51. End Function

  52. Function gettitle(ByVal htmltext)
  53.     htmltitle=""
  54.     Set regex=New RegExp
  55.     regex.Pattern="<title>(.+?)<\/title>"
  56.     regex.IgnoreCase=True
  57.     regex.Global = True
  58.     Set matches=regex.Execute(htmltext)
  59.     If matches.Count = 1 Then
  60.         htmltitle=matches(0).SubMatches(0)
  61.         For i=0 To Ubound(arr)
  62.             htmltitle=replace(htmltitle,arr(i),"_")
  63.         Next
  64.     End If
  65.     Set regex=Nothing
  66.     gettitle=htmltitle
  67. End Function
复制代码
 楼主| 发表于 2019-3-10 09:21:19 | 显示全部楼层
回复 11# zaqmlp


    谢谢你的是正确答案,但确实编码要改我的是ansi。
发表于 2019-9-28 20:11:32 | 显示全部楼层
以前VBS能运行,后来有部分不能了,不知道怎么回事
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 00:33 , Processed in 0.012303 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表