2012-02-02 5 views
1

나는 발견을 구현하고 VBS 스크립트를 사용하여 폴더의 모든 파일을 대체하기 위해 노력하고 여러 파일을 대체, 여기에 내가 실행하려고하면 지금까지찾아 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"형식 불일치, 내가 뭘 잘못하고 있니?

답변

1

당신은

oldFileContents = fso.GetFile(item.Path) 

fso.WriteFile FileName,newFileContents 

fso.을 놓친 편집 : 아래의 설명에 따라,이 대답을 유의하시기 바랍니다하는 것은에만 오류가 발생 된 위치 보여주기 위해 의미되었다. 당신의 의도는 과거에이 에러가 발생했다면 더 나아가서 코드를 개발하는 것이 었습니다. 만약 그렇다면 이미 Ekkehard가 그의 대답에 대해 도움이되는 지침을 제공 한 것을 보았을 것입니다.

+0

-1을 반환 객체가 아닌 파일의 내용입니다. –

+0

@ekkehard 왜'GetFile'이 떨어지는지를 보여주고있었습니다. 그게 다 묻혔습니다. 그 후 사용되는 것은 나와 관계가 없습니다. – Skytunnel

+0

"그 후에 사용하게되는 것은 나와 아무 관계가 없습니다."그리고 .GetFile()의 사용법을 보여 주려고 시도 할 때 Set을 잊어 버리면 다른 downvote를 사용할 자격이 있습니다. . –

3
문제는 다음과 같은 코드로 해결해야

:

' 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 0000000000.20120302.pdf 
content is not E:\trials\SoTrials\answers\9117277\data\test\0000000000.20120302.pdf 
qed! content is This is the content of 0000000000.20120302.pdf 

new content This is the content of 0000000000.20120302.puf 

new content straight from file This is the content of 0000000000.20120302.puf 

중요 포인트 :

  1. 이 희미 모든 바르 - 지금까지 사용하지 마십시오 스크립트 맨 위에 -used-line을 지정하십시오.
  2. 불필요한 변수 (폴더,) .GetFile (파일을 반환 - 파일, * 내용), 제대로이 바르를 사용 (항목 == oFile)
  3. .GetFile는() File 객체가 아닌 파일의 내용