Re: Need a script to compare two files
- From: Tom Lavedas <tglbatch@xxxxxxx>
- Date: Wed, 30 Apr 2008 06:15:09 -0700 (PDT)
On Apr 29, 5:51 pm, DKS <D...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Thanks for the script. The script is not working for some reasons. Here are
details
File1:
tom
peter
joseph
File2:
192.168.0.0, this is sql users, tom, testmachine
192.168.1.2, this is mysql users, john, testmachine2
192.168.0.0, this is sql users, joseph, testmachine5
192.168.0.0, this is sql users, maria, testmachine6
192.168.0.0, this is sql users, donald, testmachine7
Your Script:
Const ForReading = 1
with CreateObject("Scripting.FileSystemObject")
with .OpenTextFile("C:\Audit\file1.txt", ForReading)
arrData = Split(.ReadAll, vbNewline)
end with ' Data lines
with .OpenTextFile("C:\Audit\file2.txt", ForReading)
arrNames = Split(.ReadAll, vbNewline)
end with ' Names
for each data in arrData
for each fname in arrNames
if InStr(data, trim(fname)) > 0 Then
strOut = strOut & data & vbCrLf
End If
next ' fname
next ' data
with .CreateTextFile("C:\Audit\User_New_Info.txt", True)
.Write strOut
end with ' Out file
end with ' FSO
wsh.echo strOut
'---------------------8<------------------- with corrections
and I am getting this output:
M:\Scripts\Audit>cscript two_files.vbs
tom
peter
joseph
and I want
192.168.0.0, this is sql users, tom, testmachine
192.168.0.0, this is sql users, joseph, testmachine5
Thanks in advance for the help!
"Tom Lavedas" wrote:
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
I have a sample script that matches but I don't know how to extract the line
File1:
tom
peter
joseph
File2:
192.168.0.0, this is sql users, tom, testmachine
192.168.1.2, this is mysql users, john, testmachine2
192.168.0.0, this is sql users, joseph, testmachine5
192.168.0.0, this is sql users, maria, testmachine6
192.168.0.0, this is sql users, donald, testmachine7
Output:
192.168.0.0, this is sql users, tom, testmachine
192.168.0.0, this is sql users, joseph, testmachine5
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Here is the sample code:
{snip}
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)
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
for each fname in arrNames
if InStr(data, trim(fname)) > 0 Then
strOut = strOut & data & vbCrLf
End If
next ' fname
next ' data
with .CreateTextFile("C:\Audit\User_New_Info.txt", True)
.Write strOut
end with ' Out file
end with ' FSO
wsh.echo strOut
'---------------------8<------------------- with corrections
Sorry,
Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
You reversed the meaning of the two files. Either change their names
or reverse the names in the script. That is, either file1 needs to
contain the data records ...
192.168.0.0, this is sql users, tom, testmachine
etc.
and file2 the names, or the first file reference in the script should
be to C:\Audit\file2 and the second to C:\Audit\file1.
I tried to use descriptive terms in naming the variables in the code,
but apparently it wasn't clear enough.
Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
.
- References:
- Re: Need a script to compare two files
- From: Tom Lavedas
- Re: Need a script to compare two files
- From: Tom Lavedas
- Re: Need a script to compare two files
- From: DKS
- Re: Need a script to compare two files
- Prev by Date: Re: Logon Scripts for Dept. and Printers based on Groups and IP
- Next by Date: Re: Need a script to compare two files
- Previous by thread: Re: Need a script to compare two files
- Next by thread: Re: Need a script to compare two files
- Index(es):
Relevant Pages
|