Compare files two files

Started by sst001, July 18, 2015, 01:07:02 PM

Previous topic - Next topic

sst001

Hi guys,

I have two text files, file A and file B. Each file contains single line entries like E cat, E dog, E cow, etc. I would like the entries in file A to be commented out in file B, if they exist.

This is what I have so far, and this works great if file A has a single entry only, but the script fails if there are more than one entry that needs to be commented out. Can anyone see what I'm doing wrong?


;1004 Returns the full path and file name of the current WinBatch program
exepath = FilePath(IntControl(1004,0,0,0,0))
DirChange(exepath)

;read file A

handle = FileOpen("fileA.txt", "READ")
count = 0
While @TRUE ; Loop till break do us end
   line = FileRead(handle)
   If line == "*EOF*" Then Break
   count = count + 1

;comment out entry in file B
SearchString = line

FileRename("fileB.txt", "fileB.xxx")

InputFile = FileOpen("fileB.xxx", "Read")
OutPutFile = FileOpen("fileB.txt", "WRITE")

Line = FileRead(InputFile)
While Line != "*EOF*"
If StrIndex (Line, SearchString, 1, @FWDSCAN) Then
CheckRem = StrUpper(StrSub(Line, 1, 3))
If CheckRem != "#" Then
Line = StrCat("# ", Line)
EndIf
EndIf
FileWrite (OutPutFile, Line)
Line = FileRead(InputFile)      
EndWhile

EndWhile
FileClose(handle)

exit



Thanks,
ST


JTaylor

You might consider loading each file into an array and loop through the one array searching the other for the value and adding the comment where needed.  ArrayFileGet()/ArrayFilePut() along with ArraySearch() should get you started.

Jim

td

Also add ArraySort to the list.  Assuming your file being searched has more than a few rows, if you sort the file being searched, your script will perform its task more quickly.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade