Re: Need a script to compare two files

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Thanks Tom for your help!

"Tom Lavedas" wrote:

On Apr 29, 5:48 pm, "ekkehard.horner" <ekkehard.hor...@xxxxxxxx>
wrote:
Tom Lavedas schrieb:

On Apr 29, 4:16 pm, Tom Lavedas <tglba...@xxxxxxx> wrote:
On Apr 29, 3:23 pm, "DKS" <dksa...@xxxxxxxxxxx> wrote:

Hello,
I am looking for a script to compare two files.
Scan second file using the words from first file.
Extract the line that matches the word from second file
[...]

Here is how I would do what you asked for ...

'---------------------8<------------------- with corrections
Const ForReading = 1

with CreateObject("Scripting.FileSystemObject")
with .OpenTextFile("C:\Audit\User_Info.txt", ForReading)
arrData = Split(.ReadAll, vbNewline)

Doing such a Split is dangerous because of the (possible) trailing
vbCrLf(s); these generate empty elements in arrData, that will be
'found' by InStr() in every line.

Your point is well taken. I didn't work to make it robust. I think
the addition of an IF Empty type test in the loop below would solve
the problem nicely ...

end with ' Data lines

with .OpenTextFile("C:\Audit\User_First_Name.txt", ForReading)
arrNames = Split(.ReadAll, vbNewline)
end with ' Names

for each data in arrData

if not data = "" Then

for each fname in arrNames

if not fname = "" Then

if InStr(data, trim(fname)) > 0 Then
strOut = strOut & data & vbCrLf

As a name found means that no other name is contained in that line
an "Exit For" would make the script more efficient.

Agreed. It crossed my mind, but I didn't act on it.


End If ' InStr
next ' fname

end if ' fname test

next ' data

end if ' data string test

with .CreateTextFile("C:\Audit\User_New_Info.txt", True)
.Write strOut
end with ' Out file

end with ' FSO

wsh.echo strOut

[...]

Despite my gripe/carping I do like this script very much; thanks
for the inspiring food for thought.

Thanks. I like to keep the code compact, while trying to also balance
its clarity. I find that compact code can be easier to grok when I
come back to it (with a minimum of commenting required) - if I haven't
gotten too cute in squeezing it.

Tom Lavedas
===========

.



Relevant Pages

  • Re: Need a script to compare two files
    ... I am looking for a script to compare two files. ... Scan second file using the words from first file. ... if not fname = "" Then ... strOut = strOut & data & vbCrLf ...
    (microsoft.public.scripting.vbscript)
  • Re: Need a script to compare two files
    ... I am looking for a script to compare two files. ... Scan second file using the words from first file. ... these generate empty elements in arrData, ... strOut = strOut & data & vbCrLf ...
    (microsoft.public.scripting.vbscript)
  • Re: [OT] Accepting files (question)
    ... The way I operate the program under Windows XP is to make a shortcut icon ... it detect and use that second file. ... A shell script can solve the problem. ... The first time it creates a temporary driver script to later call your ...
    (comp.os.msdos.djgpp)
  • Re: [OT] Accepting files (question)
    ... The way I operate the program under Windows XP is to make a shortcut icon to my program on the task bar, then I select the input filewith Wndows Explorer, drag it onto the shortcut icon, and drop them. ... What I want to do is drop the document to be encrypted on the program's shortcut icon to fire it up, then have it wait till a second file is dropped on it, and have it detect and use that second file. ... The first time it creates a temporary driver script to later call your program with the dropped file as the first argument. ...
    (comp.os.msdos.djgpp)
  • RE: Compare two files and output results to third
    ... The script actually works - another part of the script was performing a test ... second field from the second file and then output the ... contents of the first file and then second field from the second file. ... field is a list of values that will equal strFourth from First file ...
    (microsoft.public.scripting.vbscript)