views:

24

answers:

2

Hello,

i try to copy the second line from a txt, that works great problem is now i try to make an if-statement that check the txt, is there something in line 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

The if row = 0 will not work, has some one an idea?

Error Message: Incompatible types: '[string:""]'

Line: If row = 0 Then

Regards, matthias

+2  A: 

row is a string because the ReadLine method returns a string. Probably you want to say If row = "" or If Len(row) = 0 instead.

Here is an improved version of your scripts:

  • Script for writing:

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set File = FSO.CreateTextFile("testfile.txt", True)
    File.WriteLine LastMessage  ' Why "" + ... + ""?
    File.Close
    
  • Script for reading:

    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
    
Philipp
+2  A: 

You are comparing a string to a number - the content of row will be a string, so there is a type mismatch when comparing it to a number.

Try this to find out if the line contains the number 0:

If row = "0" Then

If you want to know if the row is empty, try this:

If row = "" Then
Oded
thanks If row = "0" Then was the right one :-D
matthias