Search code examples
vbscripthp-uftfso

VBS File Read: Change CR FF to CR LF


I have txt and csv files to be read per line. A line ends with CR-LF. But in some files, there is CR and no LF; instead, next physical line starts with FF. UFT 12 reads them together, as if it was one line. I read files using fso:

Dim FileRead : Set FileRead = fso.OpenTextFile(file1)
Dim file2Read : Set file2Read = fso.OpenTextFile(file2)

FileStrR = FileRead.ReadLine
File2StrR = FileRead.ReadLine

I need to compare each line of these file with another text file:

if FileStrR = File2Str Then...

I tried to separate the FileStrR as array:

FileStrA = REPLACE(FileStrR, ChrW(12),"**")
strarray = split(FileStrA,"**")
For h = 0 to UBound(strarray)
    FileStr = strarray(h)
    if FileStr = File2Str Then...
...

But here I stuck to read next line from the File2 to compare with whatever comes after FF.

UPDATE Tried to SkipLine:

Do Until fileRead.AtEndOfStream
ln=ln+1
FileStrA = REPLACE(FileStrR, ChrW(12),"**")
strarray = split(FileStrA,"**")
For h = 0 to UBound(strarray)
    FileStr = strarray(h)
    For s=1 to (ln+h)-1
       File2Read.SkipLine
    Next

print ln&"-"&ln+h&"-"&h

    File2Str = File2Read.ReadLine
    if FileStr1 = File2Str Then...
print "F1: "&FileStr
print "F2: "&File2str
    Next
    Loop

In this peace of code, the line print ln&"-"&ln+h&"-"&hprints correct numbers (ln should be the number of the line currently read). But the string print (print "F1: "&FileStr & VBNewLine & "F2: "&File2str)gives the following:

F1: 2|8122|TX|...

F2: 4|8123|FG|...

It seems even if ln+h is 'ln' while 'h' is 0, but the fso skips one more line.


Solution

  • See this to learn that you can't use

    1. the FileSystemObject to read/write UTF-8
    2. .ReadLine if the EOLs are messed up (not CrLf or Lf)

    If your files are ANSI/UTF-16 and not to big, you can use

    1. .ReadAll to slurp the 'bad' file
    2. Replace CrFF with CrLf
    3. Split on CrLf to get an array of lines
    4. these lines to compare to the .ReadLines from the 'good' file

    If .ReadAll is not possible, you must write your own version of .ReadLine that scans for CrLf or CrFF and returns the data before those EOLs.