Author Topic: 3057: Variable could not be converted to a valid number.  (Read 227 times)

jmburton2001

  • Newbie
  • *
  • Posts: 15
3057: Variable could not be converted to a valid number.
« on: May 15, 2018, 05:55:59 pm »
Please assume I know nothing and really need your help. I've been reading and searching for the better part of 3 hours because I figure it's a really simple thing. Even so, I've looked at, and studied it to the point that I know I'm blind to the solution at this point...

Code: [Select]
filename = "C:\Binary Read Test\abc.txt"
StrToFind = "Exiting importMxfFile"
OutputFile = "C:\Binary Read Test\Output.txt"
size = FileSize(filename)
hnd = BinaryAlloc(size)
ret = BinaryRead(hnd,filename)
start = 0

While @TRUE
 
  found = BinaryIndexEx(hnd,start,@CRLF,@FWDSCAN,0)
  If found == -1
    If start >= size - 1
      Break
    EndIf
    found = size - 1  ; last line does not end in a crlf
  EndIf
  line = BinaryPeekStr(hnd,start,found-start+1)
  Foundit = StrIndexNC (line,StrToFind,0,@FWDSCAN) ;This returns an integer greater than 1
  If Foundit != "0" Then FileOpen (OutputFile, "WRITE") ;This creates the OutputFile
  If Foundit != "0" Then FileWrite(OutputFile, line) , ;This returns Error 3057 (Argument number 1 could not be converted)
  If Foundit != "0" Then FileClose (OutputFile)

  ;Message("read",line)
 
  start = found + 2
  If start >= size Then Break
 
EndWhile

BinaryFree(hnd)

Exit

The FileOpen line creates the OutputFile without fail. It's the next line "FileWrite(OutputFile, line)" that keeps giving me the 3057: Variable could not be converted to a valid number. error.

At the point it fails Foundit = 23 which (according to the old math) is not equal to 0. I've tried it as 0, "0", a predefined variable.

All I'm trying to do is write out the "line" that matches "StrToFind" into an output file. I've always used the FileRead loop for this type of analysis, but the log files I'm working with now are getting pretty big and I wanted to see if the binary operations would speed up processing.

I'm sure there's a more elegant way to do this but I'm stumped on this... Thank you in advance for your wisdom!

JTaylor

  • Pundit
  • *****
  • Posts: 907
    • Data & Stuff Inc.
Re: 3057: Variable could not be converted to a valid number.
« Reply #1 on: May 15, 2018, 06:26:12 pm »
You need to use FileOpen() and then use that with FileWrite().

Jim

td

  • Tech Support
  • *****
  • Posts: 2538
    • WinBatch
Re: 3057: Variable could not be converted to a valid number.
« Reply #2 on: May 15, 2018, 09:16:41 pm »
The Consolidated WIL Help file topic for FileOpen states:

Returns:
(i) filehandle.

The filehandle returned by the FileOpen function may be subsequently used by the FileRead, FileWrite, and FileClose functions.

For FileWrite the help file states:

Parameters:
(i) filehandle same integer that was returned by FileOpen.  <---------------------------

(s) output-data data to write to file.

"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


jmburton2001

  • Newbie
  • *
  • Posts: 15
Re: 3057: Variable could not be converted to a valid number.
« Reply #3 on: May 16, 2018, 04:03:34 am »
Thank you for your replies...

For my future reference:

filename = "C:\Binary Read Test\abc.txt"
StrToFind = "Exiting importMxfFile"
OutputFile = "C:\Binary Read Test\Output.txt"
OutputFile = FileOpen ("C:\Binary Read Test\Output.txt", "WRITE")
size = FileSize(filename)
hnd = BinaryAlloc(size)
ret = BinaryRead(hnd,filename)
start = 0

While @TRUE
 
  found = BinaryIndexEx(hnd,start,@CRLF,@FWDSCAN,0)
  If found == -1
    If start >= size - 1
      Break
    EndIf
    found = size - 1  ; last line does not end in a crlf
  EndIf
  line = BinaryPeekStr(hnd,start,found-start+1)
  Foundit = StrIndexNC (line,StrToFind,0,@FWDSCAN)
  If Foundit != "0" Then FileOpen (OutputFile, "WRITE")
  If Foundit != "0" Then FileWrite(OutputFile, line)
  If Foundit != "0" Then FileClose (OutputFile)

  ;Message("read",line)
 
  start = found + 2
  If start >= size Then Break
 
EndWhile

BinaryFree(hnd)
FileClose (OutputFile)

Exit