Page 1 of 2 12>
Topic Options
#125094 - 2004-08-16 03:54 PM Need Script, compare fileA with fileB & write fileB to fileC
DMiles Offline
Fresh Scripter

Registered: 2004-08-16
Posts: 10
I am Totally new with Kixtart. Need script to read records in FileA (SSN). For each occurence of SSN in FileB, write FileB record in FileC. dick.miles@nortonhealthcare.org
Top
#125095 - 2004-08-16 03:59 PM Re: Need Script, compare fileA with fileB & write fileB to fileC
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
This is a pretty common requirement, search the board for some examples of how it's been done in the past.

The easiest way is:
  • Read entire FileA into an array.
  • Read FileB, search array for each record.
  • If the record is present, write to FileC


This works much better if the files are sorted.

Top
#125096 - 2004-08-16 05:16 PM Re: Need Script, compare fileA with fileB & write fileB to fileC
DMiles Offline
Fresh Scripter

Registered: 2004-08-16
Posts: 10
Have searched, can't find. Can you provide example?
Thanks

Top
#125097 - 2004-08-16 05:26 PM Re: Need Script, compare fileA with fileB & write fileB to fileC
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
I'm away home now, but I'm sure someone will jump in to give you a hand.

To help us help you, post the version of KiXtart you are using, and a sample of the data in the files that you want to compare.

Top
#125098 - 2004-08-16 05:44 PM Re: Need Script, compare fileA with fileB & write fileB to fileC
DMiles Offline
Fresh Scripter

Registered: 2004-08-16
Posts: 10
Version 4.02. File A will have 9 position SSN #. FileB will have records with 9 position SSN# plus other data. I am familiar with Substr to find SSN in FileB.
Thanks

Top
#125099 - 2004-08-16 05:45 PM Re: Need Script, compare fileA with fileB & write fileB to fileC
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
Here is a quick example to help get you started...

Code:

$fileA = loadfile("fileA.txt")
$fileb = loadfile("fileb.txt")

$ = Open(1,"Filec.txt",5)

For Each $line In $fileB
If AScan($filea,$line)+1
? "A line In file b was found In file a, writting the line To file c"
$ = WriteLine(1,$line+@CRLF)
EndIf
Next


Function loadfile($file)
DIM $fso,$f,$fs
$fso = CreateObject("Scripting.FileSystemObject")
$f = $fso.GetFile($file)
If @ERROR Exit(2) EndIf
$fs = $f.OpenAsTextStream(1)
$loadfile = Split($fs.Read($f.size),@CRLF)
Exit(@ERROR)
EndFunction




Edited by Bryce (2004-08-16 05:47 PM)

Top
#125100 - 2004-08-16 05:48 PM Re: Need Script, compare fileA with fileB & write fileB to fileC
DMiles Offline
Fresh Scripter

Registered: 2004-08-16
Posts: 10
Thanks Bryce, i will give it a shot........
Top
#125101 - 2004-08-16 05:59 PM Re: Need Script, compare fileA with fileB & write fileB to fileC
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
Quote:

Thanks Bryce, i will give it a shot........




hmm i just read, your post regarding kix version and sample file data...

be sure to isolate the SSN from the $line variable in the for next loop before running the ascan() command.

Making the assumption that fileB data is like the following...

Code:

123-45-6789,smith,bill,1313 mocking bird lane,blahh..,blah,blah



you will need to isolate the SSN first.

if it is comma delimited.... and the SSN is the first data field...

Code:

For Each $line In $fileB
If AScan($filea,split($line,",")[0])+1
? "A SSN In file b was found In file a, writting the line To file c"
$ = WriteLine(1,$line+@CRLF)
EndIf
Next



Bryce


Edited by Bryce (2004-08-16 06:02 PM)

Top
#125102 - 2004-08-16 06:46 PM Re: Need Script, compare fileA with fileB & write fileB to fileC
DMiles Offline
Fresh Scripter

Registered: 2004-08-16
Posts: 10
Bryce, here is what I am atempting, but not working for me.

cls
$fileA = loadfile("c:\ssn.txt")
$fileb = loadfile("c:\data.txt")
$filec = Open(1,"c:\outdata.txt",5)


For Each $line In $fileb
$ssn = substr($line,1,9)

If AScan($filea,"$ssn")+1
? "A SSN In file b was found In file a, writting the line To file c"
$ = WriteLine(1,$line+@CRLF)
EndIf

Function loadfile($file)
DIM $fso,$f,$fs
$fso = CreateObject("Scripting.FileSystemObject")
$f = $fso.GetFile($file)
If @ERROR Exit(2) EndIf
$fs = $f.OpenAsTextStream(1)
$loadfile = Split($fs.Read($f.size),@CRLF)
Exit(@ERROR)
EndFunction


Edited by DMiles (2004-08-16 06:47 PM)

Top
#125103 - 2004-08-16 06:52 PM Re: Need Script, compare fileA with fileB & write fileB to fileC
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
No need for quotes around vars. In fact, vars in quotes are evil.
If AScan($filea,"$ssn")+1
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#125104 - 2004-08-16 06:59 PM Re: Need Script, compare fileA with fileB & write fileB to fileC
DMiles Offline
Fresh Scripter

Registered: 2004-08-16
Posts: 10
Les, removed Quotes. still no work. Does not even create outdata.txt file.
Top
#125105 - 2004-08-16 07:15 PM Re: Need Script, compare fileA with fileB & write fileB to fileC
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Quote:

but not working for me



I missed that bit of detail entirely. Maybe you added it when editing?

Did not mean to imply that removing the quotes would make it work.

I cannot see any NEXT to match your FOR.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#125106 - 2004-08-16 07:21 PM Re: Need Script, compare fileA with fileB & write fileB to fileC
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Yes I also noticed the missing Next...But it seemed to work fine for me even without it. I did add it below however...The following works fine on my machine. I added the get $ after the end of the loop so that you can see on the console if it does indeed find any matches or not. Other than that just verify that the paths for the $fila, b, and c are correct and you have the necessary rights to create the filec on the C:\.

Code:
cls 
$fileA = loadfile("c:\ssn.txt")
$fileb = loadfile("c:\data.txt")
$filec = Open(1,"c:\outdata.txt",5)


For Each $line In $fileb
$ssn = substr($line,1,9)
If AScan($filea,"$ssn")+1
? "A SSN In file b was found In file a, writting the line To file c"
$ = WriteLine(1,$line+@CRLF)
EndIf
Next

? "DONE - Press any key to exit"
get $

Function loadfile($file)
DIM $fso,$f,$fs
$fso = CreateObject("Scripting.FileSystemObject")
$f = $fso.GetFile($file)
If @ERROR Exit(2) EndIf
$fs = $f.OpenAsTextStream(1)
$loadfile = Split($fs.Read($f.size),@CRLF)
Exit(@ERROR)
EndFunction


Top
#125107 - 2004-08-16 07:40 PM Re: Need Script, compare fileA with fileB & write fileB to fileC
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
He just silences the return code from the Open()
$filec = Open(1,"c:\outdata.txt",5)

Could also be the 4.02 version of KiX. AScan was not added until 4.20 but that does not explain the failure to create ,"c:\outdata.txt". Could be that this is not the entire script and that there is an earlier Open() that uses file handle 1.

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#125108 - 2004-08-16 08:33 PM Re: Need Script, compare fileA with fileB & write fileB to fileC
DMiles Offline
Fresh Scripter

Registered: 2004-08-16
Posts: 10
Dummy me. It was creating the Out file. I was just looking for it in wrong place. Now, when I execute script, i am getting:

Script error: expected sxpression
If AScan($fileb$ssn,1,9)+1

Here is my code:

cls
$fileA = loadfile("c:\ftp\ssn.txt")
$fileb = loadfile("c:ftp\data.txt")
$filec = Open(1,"c:\ftp\outdata.txt",5)

For Each $line In $filea
$ssn = substr($line,1,9)

For Each $line In $filea
If AScan($fileb,$ssn,1,9)+1
? "A SSN In file b was found In file a, writting the line To file c"
$ = WriteLine(1,$line+@CRLF)
EndIf

Next
? "DONE - Press any key to exit"
get $
Function loadfile($file)
DIM $fso,$f,$fs
$fso = CreateObject("Scripting.FileSystemObject")
$f = $fso.GetFile($file)
If @ERROR Exit(2) EndIf
$fs = $f.OpenAsTextStream(1)
$loadfile = Split($fs.Read($f.size),@CRLF)
Exit(@ERROR)
EndFunction

Top
#125109 - 2004-08-16 08:46 PM Re: Need Script, compare fileA with fileB & write fileB to fileC
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
And the version of KiX is?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#125110 - 2004-08-16 08:51 PM Re: Need Script, compare fileA with fileB & write fileB to fileC
DMiles Offline
Fresh Scripter

Registered: 2004-08-16
Posts: 10
4.02
Top
#125111 - 2004-08-16 08:54 PM Re: Need Script, compare fileA with fileB & write fileB to fileC
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
I think you got an extra For-Next in there for starters...Not sure if the extra parameters are needed in your AScan either since you are already SubStr the SSN...Try replacing this...

Code:

For Each $line In $filea
$ssn = substr($line,1,9)

For Each $line In $filea
If AScan($fileb,$ssn,1,9)+1
? "A SSN In file b was found In file a, writting the line To file c"
$ = WriteLine(1,$line+@CRLF)
EndIf



With this...

Code:

For Each $line In $filea
$ssn = substr($line,1,9)
If AScan($fileb,$ssn)+1
? "A SSN In file a was found In file b, writting the line To file c"
$ = WriteLine(1,$line+@CRLF)
EndIf


Top
#125112 - 2004-08-16 08:57 PM Re: Need Script, compare fileA with fileB & write fileB to fileC
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Quote:

4.02



Exactly!
Like I already said, AScan() is NOT supported in 4.02!
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#125113 - 2004-08-16 09:01 PM Re: Need Script, compare fileA with fileB & write fileB to fileC
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Oh yea...forgot about the AScan not working until after your version. This should work for you to get around the ascan limitation i believe. There was also a missing '\' in one of the paths in your posted code...So make sure to check that.

Code:
cls 
$fileA = loadfile("c:\ftp\ssn.txt")
$fileb = loadfile("c:\ftp\data.txt")
$filec = Open(1,"c:\ftp\outdata.txt",5)

For Each $line In $filea
$ssn = substr($line,1,9)
For Each $lineb In $fileb
If InStr ($lineb,$ssn)
? "A SSN In file b was found In file a, writting the line To file c"
$ = WriteLine(1,$line+@CRLF)
Endif
Next
Next

? "DONE - Press any key to exit"
get $

Function loadfile($file)
DIM $fso,$f,$fs
$fso = CreateObject("Scripting.FileSystemObject")
$f = $fso.GetFile($file)
If @ERROR Exit(2) EndIf
$fs = $f.OpenAsTextStream(1)
$loadfile = Split($fs.Read($f.size),@CRLF)
Exit(@ERROR)
EndFunction


Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 1198 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.083 seconds in which 0.034 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org