if-statement - vb脚本if语句

标签 if-statement vbscript

如果文件中必须存在的单词不存在,我想用 VB 编辑文件。 当执行这个文件时,我希望如果条件为真,什么都不做,但所有文件内容都被删除。

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

最佳答案

因为当您的条件为真时,strNewText将为空。仍然将 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

关于if-statement - vb脚本if语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28825394/

相关文章:

java - 你好,我正在努力理解这段代码中 if 语句的 O 表示法运行时

javascript - 经典 ASP : how to convert a RecordSet to json notation using AXE json implementation

windows - 如何检索 LAN 适配器 MAC 地址?

java - 在 "OR"语句中使用 "IF"

vba - Excel VBA 中的 IF ElseIF 语句

javascript - 如何在 angularjs 的 View 中使用 ng-if 值检查 null?

r - if...else 在 for 循环内

ms-access - 使用 Vbscript 将记录批量插入 Access

batch-file - 如何关闭不可见的VBS文件

sql-server - ADO 参数化查询不返回任何结果