获取“错误的文件名或数字"使用目标文件名的变量重命名文件时 [英] Getting "Bad Filename or Number" when renaming file with a variable for the destination filename

查看:44
本文介绍了获取“错误的文件名或数字"使用目标文件名的变量重命名文件时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图循环一个充满 .html 文件的文件夹,并在文件的开头添加一些代码(尽管在我插入的代码之前我得到了一些不需要的换行符)并且还获取了 <title> 标记并使用它来重命名每个文件.

I am trying to loop a folder full of .html files and add some code at the beginning of the files (although I am getting some unwanted line breaks before the code I am inserting) and also to grab the contents of the <title> tag and use this for the renaming each file.

我正在用 - 替换空格和不需要的字符

I am replacing the spaces and unwanted characters with -'s

所有这些都有效,但我也在尝试将现有文件(Default0010.html 是一个示例)重命名为 </code> 中的文本.<em class="showen"></em></p> <p class="en">All of this works but I am also trying to rename the existing file (<code>Default0010.html</code> is one example) to the text from the <code><title></code>.</p> <p class="cn">这也有效,但是当我尝试将现有文件移动到新文件时,我得到一个 <code>Bad File name or Number</code> 但是当我明确地将目标文件名设置为一个简单的字符串时它可以工作.<em class="showen"></em></p> <p class="en">This works too but when I am trying to move the existing file to the new file I get a <code>Bad File name or Number</code> but when I explicilty set the destination file name to a simple string it works.</p> <p class="cn">这让我觉得我的字符串不干净或者你不能使用目标变量.<em class="showen"></em></p> <p class="en">It makes me thing my string is not clean or you cannot use a variable for the destination.</p> <p class="cn">也请忽略<code>Dim i</code>、<code>i = i + 1</code> 和<code>If i=1 Then Exit For</code> 行.</p> <p class="cn">这是在我测试脚本时添加的,然后当我感到满意时,我会在所有 HTML 文件上运行它.<em class="showen"></em></p> <p class="en">This was added whilst I test the script then when I was happy it does what I wanted I would run it on all the HTML files.</p> <pre><code><code>Set objFso = CreateObject("Scripting.FileSystemObject") Set Folder = objFSO.GetFolder("C:\My Web Sites\test\www.test.org.uk\html") Dim i Dim ObjFsoFile Dim ObjFile Dim StrData Dim StrTitleTag Dim OldFilename Dim NewFilename Set ObjFsoFile = CreateObject("Scripting.FileSystemObject") 'Loop all of the files For Each File In Folder.Files 'Get contents of the file and store in a string 'Opening the file in READ mode Set ObjFile = ObjFsoFile.OpenTextFile(File.Name) 'Reading from the file StrData = ObjFile.ReadAll 'Add the Perch include to the beginning StrData = replace(StrData,"<?php include('cms/runtime.php');?>","") 'Remove the Perch include in-case we are re-running this StrData = replace(StrData,"<!DOCTYPE HTML PUBLIC " & Chr(34) & "-//W3C//DTD HTML 4.0 Transitional//EN" & Chr(34) & ">","<?php include('cms/runtime.php');?>" & vbcrlf & "<!DOCTYPE HTML PUBLIC " & Chr(34) & "-//W3C//DTD HTML 4.0 Transitional//EN" & Chr(34) & ">") 'Msgbox StrData 'Closing the file ObjFile.Close 'Write the changes to the current file Set objFile = objFSO.CreateTextFile(File.Name,True) objFile.Write StrData objFile.Close 'Re-write the contents of the current file and replace with the StrData Above 'Grab the contents between <title> and </title> parse_string1 = StrData 'see above post parse_string1 = replace(parse_string1,"<title>","¦") parse_string = split(parse_string1,"¦") parse = parse_string(1) parse_string1 = replace(parse,"</title>","¦") parse_string = split(parse_string1,"¦") parsed_string = parse_string(0) StrTitleTag = parsed_string 'gives final result 'Save old filename of current file to a string OldFilename = File.Name 'Msgbox OldFilename 'Rename current file to the above contents of between <title> and </title> 'Replace spaces with - characters in the filename. Dim divider divider = "-" 'Replace & with and NewFilename = Replace((StrTitleTag & ".php"),"&","and") 'Replace triple space with single space NewFilename = Replace(NewFilename," "," ") 'Replace double space with single space NewFilename = Replace(NewFilename," "," ") 'Replace - with space NewFilename = Replace(NewFilename," ",divider) 'Replace ---- with - NewFilename = Replace(NewFilename,divider & "-" & divider,divider) 'Replace ---- with - NewFilename = Replace(NewFilename,divider & divider & divider,divider) 'Replace ,- with - NewFilename = Replace(NewFilename,"," & divider,divider) 'Replace LineBreaks with nothing (remove line breaks) NewFilename = Replace(NewFilename,vbCrLf,"") NewFilename = Replace(NewFilename,vbLf,"") NewFilename = Replace(NewFilename,vbCr,"") NewFilename = LCase(NewFilename) 'Msgbox NewFilename 'Loop through all files For Each File2 In Folder.Files 'Opening the file in READ mode Set ObjFile = ObjFsoFile.OpenTextFile(File2.Name) 'Get contents of the file and store in a string 'Reading from the file StrData = ObjFile.ReadAll 'Closing the file ObjFile.Close 'Replace all occurences of the old filename with the new filename StrData = Replace(StrData, OldFilename, NewFilename) 'How to write file Set objFile = objFSO.CreateTextFile(File2.Name,True) objFile.Write StrData objFile.Close Next 'Rename Old file with the new filename If objFso.FileExists("C:\My Web Sites\test\www.test.org.uk\html\" & OldFilename) Then 'NewFileName = "test.php" 'NewFileName = "test-test-test-test-test-test-test-test-test.php" Msgbox "Renaming the file " & OldFilename & " (Length: " & Len(OldFilename) & ") with the following name: " & NewFilename & " (Length: " & Len(NewFilename) & ")" Msgbox "Compare: test-test-test-test-test-test-test-test-test.php " & NewFilename objFso.MoveFile "C:\My Web Sites\test\www.test.org.uk\html\" & OldFilename, "C:\My Web Sites\test\www.test.org.uk\html\" & NewFileName End If i = i + 1 If i=1 Then Exit For Next </code></code></pre> <p class="cn"></p> <h3 class="best_ans mt-1">推荐答案</h3> <p class="cn">不要替换已知的坏字符.替换所有<strong>不是</strong><strong>已知好的</strong>字符,例如通过使用正则表达式:<em class="showen"></em></p> <p class="en">Don't replace known bad characters. Replace everything that is <strong>not</strong> a <strong>known good</strong> character, e.g. by using a regular expression:</p> <pre><code><code>Set re = New RegExp re.Pattern = "[^a-z0-9+._-]+" re.Global = True re.IgnoreCase = True NewFilename = re.Replace(OldFilename, "_") </code></code></pre> <p class="cn">下划线 (<code>_</code>) 通常是这种替换的安全字符.<em class="showen"></em></p> <p class="en">The underscore (<code>_</code>) usually is a safe character for this kind of replacement.</p> <p class="cn">此外,除非万不得已,否则不要尝试手动解析 HTML 文件中的元素.在您的情况下,可以更轻松地提取标题,如下所示:<em class="showen"></em></p> <p class="en">Also, don't try to manually parse elements from an HTML file unless you have to. In your case the title can be extracted far easier, like this:</p> <pre><code><code>Set html = CreateObject("HTMLFile") html.Write objFso.OpenTextFile(File.Name).ReadAll title = html.Title </code></code></pre> <p class="cn">它甚至会为您折叠和修剪空白.<em class="showen"></em></p> <p class="en">It will even collapse and trim whitespace for you.</p> <p class="cn">当您已经拥有该文件的句柄时,只需更改其 <code>Name</code> 属性即可重命名文件:<em class="showen"></em></p> <p class="en">And a file can be renamed by simply changing its <code>Name</code> property when you already have a handle to that file:</p> <pre><code><code>objFile.Name = NewFilename </code></code></pre> <p class="cn">脚本的简化版本(没有修改文件内容的那些部分):<em class="showen"></em></p> <p class="en">Simplified version of your script (without those parts that modify the content of the files):</p> <pre><code><code>Set fso = CreateObject("Scripting.FileSystemObject") htmlFolder = "C:\My Web Sites\test\www.test.org.uk\html" Set re = New RegExp re.Pattern = "[^a-z0-9+._-]+" re.Global = True re.IgnoreCase = True For Each f In objFso.GetFolder(htmlFolder).Files data = f.OpenAsTextStream.ReadAll Set html = CreateObject("HTMLFile") html.Write data oldname = f.Name newname = re.Replace(f.Name, "_") f.Name = newname Next </code></code></pre> <p>这篇关于获取“错误的文件名或数字"使用目标文件名的变量重命名文件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!</p> </div> <div class="arc-body-main-more"> <span onclick="unlockarc('2465256');">查看全文</span> </div> </div> <div> </div> <div class="wwads-cn wwads-horizontal" data-id="166" style="max-width:100%;border: 4px solid #666;"></div> </div> </article> <div id="arc-ad-2" class="mb-1"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5038752844014834" crossorigin="anonymous"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5038752844014834" data-ad-slot="3921941283"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="widget bgwhite radius-1 mb-1 shadow widget-rel"> <h5>相关文章</h5> <ul> <li> <a target="_blank" title="逐步重命名文件名" href="/2251359.html"> 逐步重命名文件名; </a> </li> <li> <a target="_blank" title="使用python重命名文件名" href="/2374957.html"> 使用python重命名文件名; </a> </li> <li> <a target="_blank" title="用BAT文件重命名文件名" href="/1967341.html"> 用BAT文件重命名文件名; </a> </li> <li> <a target="_blank" title="重命名zip文件中的文件名" href="/1940067.html"> 重命名zip文件中的文件名; </a> </li> <li> <a target="_blank" title="重命名 zip 文件中的文件名" href="/2573540.html"> 重命名 zip 文件中的文件名; </a> </li> <li> <a target="_blank" title="参数命名:文件名还是文件名?" href="/1671725.html"> 参数命名:文件名还是文件名?; </a> </li> <li> <a target="_blank" title="使用重命名替换文件名" href="/2374996.html"> 使用重命名替换文件名; </a> </li> <li> <a target="_blank" title="使用数据列重命名文件名" href="/1847744.html"> 使用数据列重命名文件名; </a> </li> <li> <a target="_blank" title="使用数据列重命名文件名" href="/2569519.html"> 使用数据列重命名文件名; </a> </li> <li> <a target="_blank" title="使用C#重命名文件名" href="/1424907.html"> 使用C#重命名文件名; </a> </li> <li> <a target="_blank" title="如何重命名名为“."的文件名." href="/2079934.html"> 如何重命名名为“."的文件名.; </a> </li> <li> <a target="_blank" title="使用PowerShell重命名文件时,如何使用"["处理文件名?" href="/2079897.html"> 使用PowerShell重命名文件时,如何使用"["处理文件名?; </a> </li> <li> <a target="_blank" title="Mercurial:重命名后识别文件名" href="/1619146.html"> Mercurial:重命名后识别文件名; </a> </li> <li> <a target="_blank" title="如何使用.net代码重命名文件名?" href="/1347921.html"> 如何使用.net代码重命名文件名?; </a> </li> <li> <a target="_blank" title="重命名CMD中的多个文件名" href="/2001492.html"> 重命名CMD中的多个文件名; </a> </li> <li> <a target="_blank" title="Python程序在覆盖文件名(如果已经存在)的同时重命名文件名" href="/2079902.html"> Python程序在覆盖文件名(如果已经存在)的同时重命名文件名; </a> </li> <li> <a target="_blank" title="使用批处理文件递归重命名文件名+文件夹名" href="/2666753.html"> 使用批处理文件递归重命名文件名+文件夹名; </a> </li> <li> <a target="_blank" title="从变量获取文件名" href="/2199702.html"> 从变量获取文件名; </a> </li> <li> <a target="_blank" title="PowerShell 重命名文件名并保留扩展名" href="/2340971.html"> PowerShell 重命名文件名并保留扩展名; </a> </li> <li> <a target="_blank" title="获取文件名" href="/1442429.html"> 获取文件名; </a> </li> <li> <a target="_blank" title="下载时获取文件名" href="/654208.html"> 下载时获取文件名; </a> </li> <li> <a target="_blank" title="如何从savefiledialogue输入文件名获取文件名" href="/1281333.html"> 如何从savefiledialogue输入文件名获取文件名; </a> </li> <li> <a target="_blank" title="使用文件句柄获取文件名(或删除文件)" href="/1743789.html"> 使用文件句柄获取文件名(或删除文件); </a> </li> <li> <a target="_blank" title="PHP重命名文件名如果存在附加数字结束" href="/755815.html"> PHP重命名文件名如果存在附加数字结束; </a> </li> <li> <a target="_blank" title="php检查文件名是否存在,重命名该文件" href="/1824407.html"> php检查文件名是否存在,重命名该文件; </a> </li> </ul> </div> <div class="mb-1"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5038752844014834" crossorigin="anonymous"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5038752844014834" data-ad-slot="3921941283"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="side"> <div class="widget widget-side bgwhite mb-1 shadow"> <h5>其他开发最新文章</h5> <ul> <li> <a target="_blank" title="拒绝显示一个框架,因为它将'X-Frame-Options'设置为'sameorigin'" href="/893060.html"> 拒绝显示一个框架,因为它将'X-Frame-Options'设置为'sameorigin'; </a> </li> <li> <a target="_blank" title="什么是&QUOT; AW&QUOT;在部分标志属性是什么意思?" href="/303988.html"> 什么是&QUOT; AW&QUOT;在部分标志属性是什么意思?; </a> </li> <li> <a target="_blank" title="在运行npm install命令时获取'npm WARN弃用'警告" href="/840917.html"> 在运行npm install命令时获取'npm WARN弃用'警告; </a> </li> <li> <a target="_blank" title="cmake无法找到openssl" href="/516280.html"> cmake无法找到openssl; </a> </li> <li> <a target="_blank" title="从Spark的scala中的* .tar.gz压缩文件中读取HDF5文件" href="/850628.html"> 从Spark的scala中的* .tar.gz压缩文件中读取HDF5文件; </a> </li> <li> <a target="_blank" title="Twitter :: Error :: Forbidden - 无法验证您的凭据" href="/630061.html"> Twitter :: Error :: Forbidden - 无法验证您的凭据; </a> </li> <li> <a target="_blank" title="我什么时候需要一个fb:app_id或者fb:admins?" href="/747981.html"> 我什么时候需要一个fb:app_id或者fb:admins?; </a> </li> <li> <a target="_blank" title="将.db文件导入R" href="/902960.html"> 将.db文件导入R; </a> </li> <li> <a target="_blank" title="npm通知创建一个lockfile作为package-lock.json。你应该提交这个文件" href="/744854.html"> npm通知创建一个lockfile作为package-lock.json。你应该提交这个文件; </a> </li> <li> <a target="_blank" title="拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“script-src'self'”" href="/819167.html"> 拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“script-src'self'”; </a> </li> </ul> </div> <div class="widget widget-side bgwhite mb-1 shadow"> <h5> 热门教程 </h5> <ul> <li> <a target="_blank" title="Java教程" href="/OnLineTutorial/java/index.html"> Java教程 </a> </li> <li> <a target="_blank" title="Apache ANT 教程" href="/OnLineTutorial/ant/index.html"> Apache ANT 教程 </a> </li> <li> <a target="_blank" title="Kali Linux教程" href="/OnLineTutorial/kali_linux/index.html"> Kali Linux教程 </a> </li> <li> <a target="_blank" title="JavaScript教程" href="/OnLineTutorial/javascript/index.html"> JavaScript教程 </a> </li> <li> <a target="_blank" title="JavaFx教程" href="/OnLineTutorial/javafx/index.html"> JavaFx教程 </a> </li> <li> <a target="_blank" title="MFC 教程" href="/OnLineTutorial/mfc/index.html"> MFC 教程 </a> </li> <li> <a target="_blank" title="Apache HTTP客户端教程" href="/OnLineTutorial/apache_httpclient/index.html"> Apache HTTP客户端教程 </a> </li> <li> <a target="_blank" title="Microsoft Visio 教程" href="/OnLineTutorial/microsoft_visio/index.html"> Microsoft Visio 教程 </a> </li> </ul> </div> <div class="widget widget-side bgwhite mb-1 shadow"> <h5> 热门工具 </h5> <ul> <li> <a target="_blank" title="Java 在线工具" href="/Onlinetools/details/4"> Java 在线工具 </a> </li> <li> <a target="_blank" title="C(GCC) 在线工具" href="/Onlinetools/details/6"> C(GCC) 在线工具 </a> </li> <li> <a target="_blank" title="PHP 在线工具" href="/Onlinetools/details/8"> PHP 在线工具 </a> </li> <li> <a target="_blank" title="C# 在线工具" href="/Onlinetools/details/1"> C# 在线工具 </a> </li> <li> <a target="_blank" title="Python 在线工具" href="/Onlinetools/details/5"> Python 在线工具 </a> </li> <li> <a target="_blank" title="MySQL 在线工具" href="/Onlinetools/Dbdetails/33"> MySQL 在线工具 </a> </li> <li> <a target="_blank" title="VB.NET 在线工具" href="/Onlinetools/details/2"> VB.NET 在线工具 </a> </li> <li> <a target="_blank" title="Lua 在线工具" href="/Onlinetools/details/14"> Lua 在线工具 </a> </li> <li> <a target="_blank" title="Oracle 在线工具" href="/Onlinetools/Dbdetails/35"> Oracle 在线工具 </a> </li> <li> <a target="_blank" title="C++(GCC) 在线工具" href="/Onlinetools/details/7"> C++(GCC) 在线工具 </a> </li> <li> <a target="_blank" title="Go 在线工具" href="/Onlinetools/details/20"> Go 在线工具 </a> </li> <li> <a target="_blank" title="Fortran 在线工具" href="/Onlinetools/details/45"> Fortran 在线工具 </a> </li> </ul> </div> </div> </div> <script type="text/javascript">var eskeys = '获取,错误,的,文件名,或,数字,使用,目标,文件名,的,变量,重命名,文件,时'; var cat = 'cc';';//other-dev</script> </div> <div id="pop" onclick="pophide();"> <div id="pop_body" onclick="event.stopPropagation();"> <h6 class="flex flex101"> 登录 <span onclick="pophide();">关闭</span> </h6> <div class="pd-1"> <div class="wxtip center"> <span>扫码关注<em>1秒</em>登录</span> </div> <div class="center"> <img id="qr" src="https://huajiakeji.com/Content/Images/qrydx.jpg" alt="" style="width:150px;height:150px;" /> </div> <div style="margin-top:10px;display:flex;justify-content: center;"> <input type="text" placeholder="输入验证码" id="txtcode" autocomplete="off" /> <input id="btngo" type="button" onclick="chk()" value="GO" /> </div> <div class="center" style="margin: 4px; font-size: .8rem; color: #f60;"> 发送“验证码”获取 <em style="padding: 0 .5rem;">|</em> <span style="color: #01a05c;">15天全站免登陆</span> </div> <div id="chkinfo" class="tip"></div> </div> </div> </div> <script type="text/javascript" src="https://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script> <script type="text/javascript" src="https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script> <script type="text/javascript" src="https://img01.yuandaxia.cn/Scripts/highlight.min.js"></script> <script type="text/javascript" src="https://img01.yuandaxia.cn/Scripts/base.js?v=0.22"></script> <script type="text/javascript" src="https://img01.yuandaxia.cn/Scripts/tui.js?v=0.11"></script> <footer class="footer"> <div class="container"> <div class="flink mb-1"> 友情链接: <a href="https://www.it1352.com/" target="_blank">IT屋</a> <a href="https://huajiakeji.com/" target="_blank">Chrome插件</a> <a href="https://www.cnplugins.com/" target="_blank">谷歌浏览器插件</a> </div> <section class="copyright-section"> <a href="https://www.it1352.com" title="IT屋-程序员软件开发技术分享社区">IT屋</a> ©2016-2022 <a href="http://www.beian.miit.gov.cn/" target="_blank">琼ICP备2021000895号-1</a> <a href="/sitemap.html" target="_blank" title="站点地图">站点地图</a> <a href="/Home/Tags" target="_blank" title="站点标签">站点标签</a> <a target="_blank" alt="sitemap" href="/sitemap.xml">SiteMap</a> <a href="/1155981.html" title="IT屋-免责申明"><免责申明></a> 本站内容来源互联网,如果侵犯您的权益请联系我们删除. </section> <!--统计代码--> <script type="text/javascript"> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?0c3a090f7b3c4ad458ac1296cb5cc779"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <script type="text/javascript"> (function () { var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </div> </footer> </body> </html>