WinBatch: Need to convert junk EOF chars to spaces in the file

Started by allalevkov, August 30, 2018, 05:42:19 PM

Previous topic - Next topic

allalevkov

Hi,
We receive files from the French company's main frame and it has *EOF* characters embedded in the names, which breaks the processing of the file in the middle.  The problem is that the program does not read past this character, so I cannot replace it.  The hex character we are getting is 1A. 

Any help would be greatly appreciated.
Alla
Life isn't about waiting for the storm to pass, it's about learning to dance in the rain! 
Author unknown to me

JTaylor

Have you tried the Binary Functions?   BinaryReplace() will handle any of the characters.

Jim

td

A crude example:

Code (winbatch) Select
strFile = 'c:\temp\EmbeddEof.txt'
strBuf = 'This file has an ':Num2Char(26):' in the middle':@crlf
; Creat a file with embedded EOF
FilePut(strFile, strBuf)

nSize = FileSize(strFile)
hBuf = BinaryAlloc(nSize*3)
BinaryReadEx(hBuf, 0, strFile, 0, nSize)
BinaryReplace(hBuf, Num2Char(26), 'EOF', 0)
strText = BinaryPeekStr(hBuf, 0, nSize)
BinaryFree(hBuf)


Message("File Text", strText)
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

allalevkov

Thank you very much!  My issue was Num2Char(26), did not know how to tell WB to replace '1A'.  Learned a lot about binary buffer use, great set of tools.  Here is what I ended with and it works:

   filename = 'C:\Develop\BadChar TEST File.txt'
   newfile  = 'C:\Develop\BadChar TEST File Fixed.txt'

debug(1)
   
   nSize = FileSize(filename)
   hBuf = BinaryAlloc(nSize+200)
   
   BinaryReadEx(hBuf, 0, filename, 0, nSize)
   BinaryReplace(hBuf, Num2Char(26), ' ', 0)
   
   BinaryWrite( hBuf, newfile )

   BinaryFree(hBuf)
Exit

I think to make a UDF out of it with IntControl(73...) just in case.  But that one is later.  Thank you all!
Life isn't about waiting for the storm to pass, it's about learning to dance in the rain! 
Author unknown to me

td

Another approach is to convert directly from the hex value to an ANSI character:

Code (winbatch) Select
strEof = ChrHexToString('1A')
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

allalevkov

Oh, this is great, as I have A4, A5, and expect some more.  Thank you!
Life isn't about waiting for the storm to pass, it's about learning to dance in the rain! 
Author unknown to me

td

If you have multiple characters to remove, you might consider using a combination of the FileGet and StrClean functions to perform the task in one pass over the file.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

allalevkov

StrClean did not work while 1A was there, it would not go beyond 1A.  After it was removed StrClean should work, I will try it on the next change.
Thank you,
Alla
Life isn't about waiting for the storm to pass, it's about learning to dance in the rain! 
Author unknown to me

td

I have no problems getting StrClean to remove multiple characters in a file containing the 0x1a character.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade