求教两个字符串之间内容命名文件名
我有一堆.html文件都是以数字命名,但不规则。我要以里面<title>...</title>的内容为html文件名,若用bat写经常出现响应错误,里面东西都没了。还有用vbs的话replace不支持通配符,该咋弄。 [i=s] 本帖最后由 ivor 于 2019-3-9 21:06 编辑 [/i][code]@Powershell "& {[ScriptBlock]::Create("'#' + (gc '%~f0' -raw)").Invoke()}" & pause & goto :eof
dir *.html | %{ren $_.Name ([regex]::Replace(([regex]::Match((gc $_),'(?<=<title>).*(?=</title>)').Value),'[\\/:*?\"<>|]',"")+$_.Extension)}[/code]已经过滤了特殊字符 我试了,提示无效字符。 [code]@echo off
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\"}}"
[/code] [i=s] 本帖最后由 jieyuan_1981 于 2019-3-9 22:22 编辑 [/i]
[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=218071&ptid=52222]4#[/url] [i]yhcfsr[/i] [/b]
谢谢你的回答,但提示语句未结束。 已经过滤了特殊字符 [b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=218078&ptid=52222]6#[/url] [i]ivor[/i] [/b] [i=s] 本帖最后由 jieyuan_1981 于 2019-3-9 22:16 编辑 [/i]
[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=218069&ptid=52222]2#[/url] [i]ivor[/i] [/b]
我在本论坛看了几篇无效字符的帖子,好像是关键字或赋值方法的问题,还有是vba代码不能用在vbs中。比如:open Filename 就不行,open就行。你给我的答案是不是这块有问题。另外我是xp系统,是否不识别。 我们给的都是批处理代码,你放到vbs里面肯定不能运行了
加我qq250193966,帮你看看 [i=s] 本帖最后由 523066680 于 2019-3-9 22:44 编辑 [/i]
即使是XP,涉及HTML解析的问题仍然建议换别的语言工具(lua, python, ahk, 什么都好,lua还能转exe呢)。
提取 <title> 示例[code]use Mojo::DOM;
use File::Slurp;
grep { printf "%s %s\n", $_, Mojo::DOM->new(read_file $_)->at("title")->text } glob "*.html";[/code] [code]'代码可用,扫码头像,随意赞助;有什么问题,可加QQ956535081及时沟通
If LCase(Right(WSH.FullName,12)) = "\wscript.exe" Then
CreateObject("WScript.Shell").Run "cmd /c cscript.exe -nologo """ & WSH.ScriptFullName & """&pause&exit"
WSH.Quit
End If
Set fso=CreateObject("Scripting.FileSystemObject")
RootPath=fso.GetFile(WSH.ScriptFullName).ParentFolder.Path
arr=split("\ / : * ? \ "" < > | " & Chr(9)," ")
getfile RootPath
Function getfile(path)
Set oFolder=fso.GetFolder(path)
Set oSubFolders=oFolder.SubFolders
Set oFiles=oFolder.Files
For Each oFile In oFiles
ext=fso.GetExtensionName(oFile.Path)
If Lcase(ext) = "html" Or Lcase(ext) = "htm" Then
text=gethtml(oFile.Path)
title=gettitle(text)
NewName=title & "." & ext
WSH.echo oFile.Path & " --> " & NewName
If title <> "" Then
If Not fso.FileExists(oFile.ParentFolder.Path & "\" & NewName) Then
oFile.Name=NewName
Else
WSH.echo "文件重名"
End If
End If
End If
Next
For Each oSubFolder In oSubFolders
getfile(oSubFolder.Path)
Next
Set oFolder=Nothing
Set oSubFolders=Nothing
End Function
Function gethtml(ByVal htmlfile)
htmltext=""
Set ado=CreateObject("ADODB.Stream")
ado.Type=2
'注意html文件的编码,如果是gbk或gb2312,修改下面的"utf-8"为"gb2312"
ado.Charset="utf-8"
ado.Open
ado.LoadFromFile htmlfile
ado.Position=0
htmltext=ado.ReadText
ado.Close
Set ado=Nothing
gethtml=htmltext
End Function
Function gettitle(ByVal htmltext)
htmltitle=""
Set regex=New RegExp
regex.Pattern="<title>(.+?)<\/title>"
regex.IgnoreCase=True
regex.Global = True
Set matches=regex.Execute(htmltext)
If matches.Count = 1 Then
htmltitle=matches(0).SubMatches(0)
For i=0 To Ubound(arr)
htmltitle=replace(htmltitle,arr(i),"_")
Next
End If
Set regex=Nothing
gettitle=htmltitle
End Function
[/code] [b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=218089&ptid=52222]11#[/url] [i]zaqmlp[/i] [/b]
谢谢你的是正确答案,但确实编码要改我的是ansi。 以前VBS能运行,后来有部分不能了,不知道怎么回事
页:
[1]