2010-03-31 77 views
0

我得到的代碼如何製作一個批處理文件,編輯一個文本文件

Set objFS = CreateObject("Scripting.FileSystemObject") 
strFile = "C:\test\file.txt" 
Set objFile = objFS.OpenTextFile(strFile) 
Do Until objFile.AtEndOfStream 
    strLine = objFile.ReadLine 
    If InStr(strLine,"ex3")> 0 Then 
     strLine = Replace(strLine,"ex3","ex5") 
    End If 
    WScript.Echo strLine 
Loop 

的strLine中更換部分我可以修復自己有我自己的目的使用,但我怎麼做這樣的事情,以便它不需要文件的名稱,它只是編輯文檔中的所有文本文件?

+0

您可以在文件名作爲參數傳遞給腳本。但是,「編輯文檔中的所有文本文件」是什麼意思? – 2010-03-31 16:57:15

+0

在文件夾內,我是速度打字。我很抱歉,但是我將文件夾中的所有文本文件放在「strFile」行中。 – William 2010-03-31 17:34:47

回答

1

,你可以像這樣做,

strFolder = "c:\myfolder" 
Set objFolder = objFS.GetFolder(strFolder) 
For Each strFile In objFolder.Files 
    strFileName =strFile.Name 
    strFilePath = strFile.Path 
    strFileExt = objFS.GetExtensionName(strFile) 
    If strFileExt = "txt" Then 
     Set objFile = objFS.OpenTextFile(strFile) 
      ' your current code here.. 
     objFile.Close() 
    End If 
Next 
相關問題