2015-03-03 5 views
2

Мне очень жаль, мой английский язык слишком плохой
Я хочу отредактировать файл с VB, если слово, которое должно быть в файле, не существует. при выполнении этого файла, я хочу, чтобы условие было правдой, ничего не делает, кроме всего содержимого файла. , пожалуйста, помогите мне.vb script if statement

Const ForReading = 1 
Const ForWriting = 2 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("C:\path\to\file.html", ForReading) 

strText = objFile.ReadAll 
objFile.Close 
strSearchFor = "this word must to exist" 

If InStr(1, strText, strSearchFor) > 0 then 
    'do nothing 
else 
    strNewText = Replace(strText,"this word must to delete","this word must to exist") 
End If 
Set objFile = objFSO.OpenTextFile("C:\path\to\file.html", ForWriting) 
objFile.WriteLine strNewText 
objFile.Close 

ответ

1

это becouse strNewText будет null если ваши условия истинны. и все еще заменяйте strText пустым strNewText. Держите свои работы в стороне if(). поэтому он решит проблему.

Const ForReading = 1 
Const ForWriting = 2 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("C:\path\to\file.html", ForReading) 

strText = objFile.ReadAll 
objFile.Close 
strSearchFor = "this word must to exist" 

    If InStr(1, strText, strSearchFor) > 0 then 
     'do nothing 
    else 
     strNewText = Replace(strText,"this word must to delete","this word must to exist") 

     Set objFile = objFSO.OpenTextFile("C:\path\to\file.html", ForWriting) 
     objFile.WriteLine strNewText 
     objFile.Close 
    End If 
+0

Tanx wicky its true – mini323

+0

@ mini323 этот ответ приемлемый ?. – mhs

+0

да мой код сейчас работает – mini323