【问题标题】:Find and replace multiple file in vbs在vbs中查找和替换多个文件
【发布时间】:2012-02-02 20:20:27
【问题描述】:

我正在尝试使用 vbs 脚本对文件夹中的所有文件进行查找和替换,这是我目前所拥有的

Dim fso,folder,files,oldFileContents,newFileContents,FIND,REPLACE

FIND = "textToBeReplaced"
REPLACE = "localhost"

Set fso = CreateObject("Scripting.FileSystemObject")

Set folder = fso.GetFolder("HTML")
Set files = folder.Files

For each item In files
    oldFileContents = GetFile(item.Path)
    newFileContents = replace(oldFileContents,FIND,REPLACE,1,-1,1)
    WriteFile FileName,newFileContents
Next

但是当我尝试运行它时,出现错误“类型不匹配:'GetFile'”,我做错了什么?

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    问题应该用如下代码解决:

    ' Constants Mr Gates forgot (but cf. vbTextCompare)
      Const ForReading = 1
      Const ForWriting = 2
    ' Configuration constants  
      Const csFind     = "pdf"
      Const csRepl     = "puf"
    ' Dim & init for vars needed on *this* level   
      Dim oFS   : Set oFS = CreateObject("Scripting.FileSystemObject")
      Dim sTDir : sTDir   = oFS.GetAbsolutePathName("..\data\test")
      Dim oFile
      For Each oFile In oFS.GetFolder(sTDir).Files
          WScript.Echo "looking at", oFile.Name
          ' Dim & init for vars needed on *this* level   
          Dim sContent : sContent = goFS.GetFile(oFile.Path)
          ' For Skytunnels and other air-coders
          WScript.Echo "content is not", sContent
          ' you got oFile, so use it; no need for .GetFile()
          sContent = oFile.OpenAsTextStream(ForReading).ReadAll()
          WScript.Echo "qed! content is", sContent
          ' Replace(expression, find, replacewith[, start[, count[, compare]]])
          ' don't use magic numbers; vbTextCompare is even pre-defined
          sContent = Replace(sContent, csFind, csRepl, 1, -1, vbTextCompare)
          WScript.Echo "new content", sContent
          oFile.OpenAsTextStream(ForWriting).Write sContent
          sContent = oFile.OpenAsTextStream(ForReading).ReadAll()
          WScript.Echo "new content straight from file", sContent
          WScript.Echo "------------------"
      Next
    

    输出:

    ...
    ------------------
    looking at 0000000000012345.20120302.pdf
    content is not E:\trials\SoTrials\answers\9117277\data\test\0000000000012345.20120302.pdf
    qed! content is This is the content of 0000000000012345.20120302.pdf
    
    new content This is the content of 0000000000012345.20120302.puf
    
    new content straight from file This is the content of 0000000000012345.20120302.puf
    

    要点:

    1. 不要在脚本顶部使用 Dim-all-vars-ever-used-line
    2. 避免创建不必要的变量(文件夹、文件、*内容),使用 您正确拥有的 vars (item==oFile)
    3. .GetFile() 返回一个 File 对象,而不是文件的内容

    【讨论】:

      【解决方案2】:

      你错过了fso.

      oldFileContents = fso.GetFile(item.Path)
      

      fso.WriteFile FileName,newFileContents
      

      编辑:根据下面的讨论,请注意此答案仅用于显示您的错误发生的位置。假设您的意图是在您克服此错误后进一步开发您的代码,如果是这样,我相信您已经看到 Ekkehard 为他的回答提供了一些非常有用的指导。

      【讨论】:

      • -1 - .GetFile() 返回一个 File 对象,而不是文件的内容
      • @ekkehard 我只是在说明GetFile 下降的原因。这是所有被问到的。之后用来做什么和我无关
      • 对于“之后它的用途与我无关”以及在尝试显示 .GetFile() 的用法时忘记了 Set,您应该再次投反对票。
      • 我同意你已经给出了一个写得很好的完整答案,为此你值得 +1。但我会指出这个网站的常见问题解答; “任何让提问者朝着正确方向前进的答案都是有帮助的......”,诚然,它确实继续说你的答案类型“更好”,但给出“更好”的答案并不意味着“这答案没用”,值得一票否决
      • 我仍然认为您的答案根本没有指向正确的方向,但是如果您对其进行编辑,我愿意收回反对票(可能显示 .GetFile() 的正确用法或更正.Write文件)。
      猜你喜欢
      • 1970-01-01
      • 2013-01-29
      • 2013-12-26
      • 2014-01-14
      • 2011-07-29
      • 2012-08-29
      • 2017-08-27
      • 2013-09-08
      • 2015-10-06
      相关资源
      最近更新 更多