【问题标题】:Getting "Bad Filename or Number" when renaming file with a variable for the destination filename使用目标文件名的变量重命名文件时出现“错误的文件名或编号”
【发布时间】:2013-06-25 12:11:51
【问题描述】:

我正在尝试循环一个充满 .html 文件的文件夹并在文件的开头添加一些代码(尽管在我插入的代码之前我得到了一些不需要的换行符)并且还获取了 @ 的内容987654321@ 标签并使用它来重命名每个文件。

我将空格和不需要的字符替换为 -'s

所有这些都有效,但我也在尝试将现有文件(Default0010.html 是一个示例)重命名为 <title> 中的文本。

这也有效,但是当我尝试将现有文件移动到新文件时,我得到一个 Bad File name or Number,但是当我明确地将目标文件名设置为一个简单的字符串时,它可以工作。

这让我觉得我的字符串不干净,或者你不能为目的地使用变量。

另外请忽略Dim ii = i + 1If i=1 Then Exit For 行。

这是在我测试脚本时添加的,然后当我很高兴它按照我的意愿运行时,我会在所有 HTML 文件上运行它。

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

【问题讨论】:

    标签: vbscript windows-7-x64


    【解决方案1】:

    不要替换已知的坏字符。替换所有已知的好字符,例如通过使用正则表达式:

    Set re = New RegExp
    re.Pattern = "[^a-z0-9+._-]+"
    re.Global  = True
    re.IgnoreCase = True
    
    NewFilename = re.Replace(OldFilename, "_")
    

    下划线 (_) 通常是这种替换的安全字符。

    此外,除非必须,否则不要尝试手动解析 HTML 文件中的元素。在您的情况下,可以更容易地提取标题,如下所示:

    Set html = CreateObject("HTMLFile")
    html.Write objFso.OpenTextFile(File.Name).ReadAll
    title = html.Title
    

    它甚至会为你折叠和修剪空白。

    当您已经拥有该文件的句柄时,只需更改其Name 属性即可重命名该文件:

    objFile.Name = NewFilename
    

    脚本的简化版本(不包含修改文件内容的部分):

    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
    

    【讨论】:

    • 这看起来很棒,我现在正在尝试你的建议,一旦我对它们感到满意,我会给你一个漂亮的绿色大勾号!但也请注意,我忘了提到我需要遍历所有文件并用新文件名替换任何超链接(这会将网站上的任何链接更新到新页面)
    • 我现在得到 Object doesn't support this property or method: Code 800A01B6 'Rename Old file with the new filename If objFso.FileExists("C:\My Web Sites\Dodderhill\www.dodderhillhistory.org.uk\html\" &amp; OldFilename) Then Set ObjFile = ObjFsoFile.OpenTextFile(File.Name) objFile.Name = NewFilename 'Closing the file ObjFile.Close End If
    • 您将objFile 设为Text Stream 对象,而不是File 对象。只有后者有 Name 属性。并且已经适当地注意到您更新了文件中的链接。因此,我说简化代码不包括这些部分(IOW 你需要再次添加它们)。示例代码中省略了它们以保持示例简单。
    猜你喜欢
    • 2023-01-30
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 2021-03-31
    • 2019-01-04
    • 2016-04-18
    • 2011-04-10
    • 2015-12-11
    相关资源
    最近更新 更多