如果vbscript出现问题很容易

时间:2010-07-08 07:22:02

标签: vbscript if-statement

我尝试从txt复制第二行,这很有用,现在我尝试制作一个检查txt的if语句,是否有第2行的内容。

 Set fso = CreateObject("Scripting.FileSystemObject")
 Set MyFile = fso.CreateTextFile("testfile.txt", True)
 MyFile.WriteLine("" + LastMessage + "")
 MyFile.Close

 rownumber = 0

 file = "testfile.txt"
 Set fs = CreateObject("Scripting.FileSystemObject")
 Set File = fs.OpenTextFile(file , 1, true)
 Do While not file.AtEndOfStream
  rownumber = rownumber+1
  row = (file.ReadLine)
  if rownumber = 2 then
   new =  row
   msgbox row
  end if
  If row = 0 Then
   msgbox "nothing found"
  else
   msgbox "found"
  end if
 Loop

if row = 0不起作用,有一个想法吗?

错误消息:不兼容的类型:'[string:“”]'

行:如果row = 0那么

此致 的Matthias

2 个答案:

答案 0 :(得分:2)

row是一个字符串,因为ReadLine方法返回一个字符串。您可能想要说If row = ""If Len(row) = 0

以下是您脚本的改进版本:

  • 写作脚本:

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set File = FSO.CreateTextFile("testfile.txt", True)
    File.WriteLine LastMessage  ' Why "" + ... + ""?
    File.Close
    
  • 阅读脚本:

    RowNumber = 0
    FileName = "testfile.txt"
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set File = FSO.OpenTextFile(FileName, 1, True)
    Do Until File.AtEndOfStream
        RowNumber = RowNumber + 1
        Row = File.ReadLine
        If RowNumber = 2 Then
            NewRow = Row   ' NewRow is nowhere used
            MsgBox Row
        End If
        If Len(Row) = 0 Then
            MsgBox "nothing found"
        Else
            MsgBox "found"
        End If
    Loop
    

答案 1 :(得分:2)

您正在将字符串与数字进行比较 - row的内容将是一个字符串,因此在将其与数字进行比较时存在类型不匹配。

尝试此操作以查明该行是否包含数字0:

If row = "0" Then

如果您想知道该行是否为空,请尝试以下操作:

If row = "" Then