WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: sst001 on July 18, 2015, 01:07:02 PM

Title: Compare files two files
Post by: sst001 on July 18, 2015, 01:07:02 PM
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

Title: Re: Compare files two files
Post by: JTaylor on July 18, 2015, 02:17:41 PM
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
Title: Re: Compare files two files
Post by: td on July 20, 2015, 06:45:45 AM
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.