Extract till end of file

Started by hdsouza, December 29, 2017, 06:19:28 AM

Previous topic - Next topic

hdsouza

Hi,
I want to search for Loan_StartDate in the file and if it exists then extract everything  from that line till end of file.
I am getting an error on BinaryPokeStr :  invalid binary data handle passed.
The Save_Section is fine.
Any ideas what I am doing wrong

Code (winbatch) Select

Log_LoanExtracted = "C:\temp\Log_LoanExtracted.txt"
Loan_StartDate = TimeSubtract(TimeYmdHms ( ), "0000:00:05"); 5 days
Loan_StartDate = StrSub(Loan_StartDate, 1, 10)
Bkp_LoanExtracted = "c:\temp\Bkp_LoanExtracted.txt"
Filecopy(Log_LoanExtracted, Bkp_LoanExtracted, @false)
fs_LoanExtracted = FileSize(Log_LoanExtracted)
binbuf_LoanExtracted = BinaryAlloc( fs_LoanExtracted )
binbuf_Saved  = BinaryAlloc( fs_LoanExtracted ) + 10000
BinaryRead( binbuf_LoanExtracted, Log_LoanExtracted)
While @true
      Exist_Date = BinaryIndexNc( binbuf_LoanExtracted, 0, Loan_StartDate, @FWDSCAN)
      if Exist_Date == 0 then break
      StrDate_Begin = BinaryIndexNc(binbuf_LoanExtracted, Exist_Date, @crlf, @BackSCAN)
      ;Now we peek everything  till eof
      Save_Section = BinaryPeekStr(binbuf_LoanExtracted, StrDate_Begin, fs_LoanExtracted)
      BinaryPokeStr( binbuf_Saved, 0, Save_Section )
      BinaryWrite(binbuf_Saved, Bkp_LoanExtracted)
      Break
Endwhile
BinaryFree(binbuf_LoanExtracted)
BinaryFree(binbuf_Saved)


JTaylor

Not sure on the error but in this situation, assuming the file isn't HUGE, you might find simple String Functions more efficient in code and time.  Not sure what the FileCopy accomplishes so left it in there.   Perhaps (untested)

Jim

Code (winbatch) Select


Log_LoanExtracted = "C:\temp\Log_LoanExtracted.txt"
Loan_StartDate = StrSub(TimeSubtract(TimeYmdHms ( ), "0000:00:05"), 1, 10); 5 days
Bkp_LoanExtracted = "c:\temp\Bkp_LoanExtracted.txt"
Filecopy(Log_LoanExtracted, Bkp_LoanExtracted, @false)

txt = FileGet(Log_LoanExtracted)
pos = StrIndex(txt,Loan_StartDate,0,@FWDSCAN)
If pos > 0 Then
  FilePut(Bkp_LoanExtracted,StrSub(txt,pos,StrLen(txt)))
EndIf


td

I may be missing the obvious but nothing jumps out as the cause of the error you are receiving.  Are you certain that the BinaryPokeStr line in the script you posted is the cause of the error?  Can you produce the error with only the script you posted in your topic or is this just a copy and paste from a larger script?  Also, which version of WinBatch are you using?

If you are certain that the error is coming from the BianryPokeStr above and the script as posted will reproduce the error, you may want to consider sending a sample of your Log_LoanExtracted.txt file to support@winbatch.com or attach it to a follow-up post so we can look at for ourselves.  Make the sample as small as possible but be sure that it will produce the error.  Don't forget to obscure any sensitive information in the file.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

Did I mention that I was missing the obvious?  The line

Code (winbatch) Select
binbuf_Saved  = BinaryAlloc( fs_LoanExtracted ) + 10000

is the source of your problems.  You probably meant for it to be

Code (winbatch) Select
binbuf_Saved  = BinaryAlloc( fs_LoanExtracted + 10000)
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

hdsouza

Jim, Thanks a lot for the code. Love the way you did it.
Yes this is a lot simpler. In truth I hate dealing with Binary, but I do realize they are a lot faster especially when they come to large files (the current file is smaller and your code is great)

Quote from: JTaylor on December 29, 2017, 07:20:49 AM
Not sure on the error but in this situation, assuming the file isn't HUGE, you might find simple String Functions more efficient in code and time.  Not sure what the FileCopy accomplishes so left it in there.   Perhaps (untested)

Jim

Code (winbatch) Select


Log_LoanExtracted = "C:\temp\Log_LoanExtracted.txt"
Loan_StartDate = StrSub(TimeSubtract(TimeYmdHms ( ), "0000:00:05"), 1, 10); 5 days
Bkp_LoanExtracted = "c:\temp\Bkp_LoanExtracted.txt"
Filecopy(Log_LoanExtracted, Bkp_LoanExtracted, @false)

txt = FileGet(Log_LoanExtracted)
pos = StrIndex(txt,Loan_StartDate,0,@FWDSCAN)
If pos > 0 Then
  FilePut(Bkp_LoanExtracted,StrSub(txt,pos,StrLen(txt)))
EndIf




hdsouza

Thanks TD. That did it !!!!!

Quote from: td on December 29, 2017, 08:05:08 AM
Did I mention that I was missing the obvious?  The line

Code (winbatch) Select
binbuf_Saved  = BinaryAlloc( fs_LoanExtracted ) + 10000

is the source of your problems.  You probably meant for it to be

Code (winbatch) Select
binbuf_Saved  = BinaryAlloc( fs_LoanExtracted + 10000)