This is a multi-part message in MIME format.

------=_NextPart_000_0008_01C8A9F3.D5EC7650
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hello,

I am looking for a script to compare two files.
Scan second file using the words from first file.=20
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:
Const ForReading =3D 1

Set objFSO =3D CreateObject("Scripting.FileSystemObject")
Set objFile1 =3D objFSO.OpenTextFile("C:\Audit\User_Info.txt", =
ForReading)

strAddresses =3D objFile1.ReadAll
objFile1.Close

Set objFile2 =3D objFSO.OpenTextFile("C:\Audit\User_First_Name.txt", =
ForReading)

Do Until objFile2.AtEndOfStream
strCurrent =3D objFile2.ReadLine
If InStr(strAddresses, strCurrent) <> 0 Then
strNoAddress =3D strNoAddress & strCurrent & vbCrLf
End If
Loop

objFile2.Close

Wscript.Echo "Devices with no address: " & vbCrLf & strNoAddress

Set objFile3 =3D objFSO.CreateTextFile("C:\Audit\User_New_Info.txt")

objFile3.WriteLine strNoAddress
objFile3.Close
------=_NextPart_000_0008_01C8A9F3.D5EC7650
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.3790.3959" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3D"Arial Narrow" size=3D2>Hello,</FONT></DIV>
<DIV><FONT face=3D"Arial Narrow" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Arial Narrow" size=3D2>I am looking for a script to =
compare two=20
files.</FONT></DIV>
<DIV><FONT face=3D"Arial Narrow" size=3D2>Scan second file&nbsp;using =
the words from=20
first file.&nbsp;</FONT></DIV>
<DIV><FONT face=3D"Arial Narrow" size=3D2>Extract the line that matches =
the word=20
from second file</FONT></DIV>
<DIV><FONT face=3D"Arial Narrow" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Arial Narrow" size=3D2>I have a sample script that =
matches but I=20
don't know how to extract the line</FONT></DIV>
<DIV><FONT face=3D"Arial Narrow" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Arial Narrow" =
size=3D2><STRONG>File1:</STRONG></FONT></DIV>
<DIV><FONT face=3D"Arial Narrow" size=3D2>tom</FONT></DIV>
<DIV><FONT face=3D"Arial Narrow" size=3D2>peter</FONT></DIV>
<DIV><FONT face=3D"Arial Narrow" size=3D2>joseph</FONT></DIV>
<DIV><FONT face=3D"Arial Narrow" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Arial Narrow" =
size=3D2><STRONG>File2:</STRONG></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>192.168.0.0, this is sql users, tom,=20
testmachine</FONT></DIV>
<DIV>
<DIV><FONT face=3DArial size=3D2>192.168.1.2, this is mysql users, john, =

testmachine2</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>192.168.0.0, this is sql users, joseph, =

testmachine5</FONT></DIV>
<DIV>
<DIV>
<DIV><FONT face=3DArial size=3D2>192.168.0.0, this is sql users, maria,=20
testmachine6</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>
<DIV><FONT face=3DArial size=3D2>192.168.0.0, this is sql users, donald, =

testmachine7</FONT></DIV></FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><STRONG>Output:</STRONG></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>
<DIV><FONT face=3DArial size=3D2>192.168.0.0, this is sql users,=20
<STRONG>tom</STRONG>, testmachine</FONT></DIV>
<DIV>
<DIV><FONT face=3DArial size=3D2>192.168.0.0, this is sql users,=20
<STRONG>joseph</STRONG>, testmachine5</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</DIV>
<DIV>Here is the sample code:</DIV>
<DIV>Const ForReading =3D 1</DIV>
<DIV>&nbsp;</DIV>
<DIV>Set objFSO =3D CreateObject("Scripting.FileSystemObject")<BR>Set =
objFile1 =3D=20
objFSO.OpenTextFile("C:\Audit\User_Info.txt", ForReading)</DIV>
<DIV>&nbsp;</DIV>
<DIV>strAddresses =3D objFile1.ReadAll<BR>objFile1.Close</DIV>
<DIV>&nbsp;</DIV>
<DIV>Set objFile2 =3D =
objFSO.OpenTextFile("C:\Audit\User_First_Name.txt",=20
ForReading)</DIV>
<DIV>&nbsp;</DIV>
<DIV>Do Until objFile2.AtEndOfStream<BR>&nbsp;&nbsp;&nbsp; strCurrent =
=3D=20
objFile2.ReadLine<BR>&nbsp;&nbsp;&nbsp; If InStr(strAddresses, =
strCurrent)=20
&lt;&gt; 0 Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
strNoAddress =3D=20
strNoAddress &amp; strCurrent &amp; vbCrLf<BR>&nbsp;&nbsp;&nbsp; End=20
If<BR>Loop</DIV>
<DIV>&nbsp;</DIV>
<DIV>objFile2.Close</DIV>
<DIV>&nbsp;</DIV>
<DIV>Wscript.Echo "Devices with no address: " &amp; vbCrLf &amp;=20
strNoAddress</DIV>
<DIV>&nbsp;</DIV>
<DIV>Set objFile3 =3D =
objFSO.CreateTextFile("C:\Audit\User_New_Info.txt")</DIV>
<DIV>&nbsp;</DIV>
<DIV>objFile3.WriteLine=20
strNoAddress<BR>objFile3.Close</DIV></DIV></FONT></DIV></DIV></DIV></DIV>=
</BODY></HTML>

------=_NextPart_000_0008_01C8A9F3.D5EC7650--

Re: Need a script to compare two files by Tom

Tom
Tue Apr 29 15:16:09 CDT 2008

On Apr 29, 3:23 pm, "DKS" <dksa...@hotmail.com> 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<-------------------
Const ForReading = 1, ForWriting = 2

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 arrAddresses
for each fname in arrNames
if InStr(data, trim(fname)) > 0 Then
strOut = strOut & data & vbCrLf
End If
next
next

.CreateTextFile("C:\Audit\User_New_Info.txt", forwriting)_
.Write strOut

end with ' FSO

wsh.echo strOut
'---------------------8<-------------------

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/

Re: Need a script to compare two files by Tom

Tom
Tue Apr 29 15:41:49 CDT 2008

On Apr 29, 4:16 pm, Tom Lavedas <tglba...@cox.net> wrote:
> On Apr 29, 3:23 pm, "DKS" <dksa...@hotmail.com> 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/

Re: Need a script to compare two files by ekkehard

ekkehard
Tue Apr 29 16:24:01 CDT 2008

DKS schrieb:
> 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:
> Const ForReading = 1
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFile1 = objFSO.OpenTextFile("C:\Audit\User_Info.txt", ForReading)
>
> strAddresses = objFile1.ReadAll
> objFile1.Close
>
> Set objFile2 = objFSO.OpenTextFile("C:\Audit\User_First_Name.txt", ForReading)
>
> Do Until objFile2.AtEndOfStream
> strCurrent = objFile2.ReadLine
> If InStr(strAddresses, strCurrent) <> 0 Then
> strNoAddress = strNoAddress & strCurrent & vbCrLf
> End If
> Loop
>
> objFile2.Close
>
> Wscript.Echo "Devices with no address: " & vbCrLf & strNoAddress
>
> Set objFile3 = objFSO.CreateTextFile("C:\Audit\User_New_Info.txt")
>
> objFile3.WriteLine strNoAddress
> objFile3.Close

Another approach:

Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sNamFSpec : sNamFSpec = ".\dksnames.txt"
Dim sUsrFSpec : sUsrFSpec = ".\dksusers.txt"
Dim sRptFSpec : sRptFSpec = ".\dksreport.txt"
Dim dicFound : Set dicFound = CreateObject( "Scripting.Dictionary" )
Dim dicMiss : Set dicMiss = CreateObject( "Scripting.Dictionary" )
Dim sNames : sNames = oFS.OpenTextFile( sNamFSpec ).ReadAll
Dim oRE : Set oRE = New RegExp

oRE.Pattern = "[\r\n]+$"
sNames = oRE.Replace( sNames, "" ) ' get rid of trailing vbCrLfs
WScript.Echo sNames + vbCrLf + "---------------------"

oRE.Pattern = "(\b" & Replace( sNames, vbCrLf, "\b)|(\b" ) & "\b)"
WScript.Echo oRE.Pattern + vbCrLf + "---------------------"

Dim oTS : Set oTS = oFS.OpenTextFile( sUsrFSpec )
Dim nLine : nLine = 0
Do Until oTS.AtEndOfStream
Dim sLine : sLine = Trim( oTS.ReadLine )
nLine = nLine + 1
If "" <> sLine Then
Dim oMTS : Set oMTS = oRE.Execute( sLine )
sLine = nLine & ": " & sLine
If 0 = oMTS.Count Then
dicMiss( sLine ) = 0
Else
Dim sName : sName = oMTS( 0 ).Value
If Not dicFound.Exists( sName ) Then
Set dicFound( sName ) = CreateObject( "Scripting.Dictionary" )
End If
dicFound( sName )( dicFound( sName ).Count ) = sLine
End If
End If
Loop
oTS.Close

Set oTS = oFS.CreateTextFile( sRptFSpec, True )
oTS.WriteLine String( 20, "-" ) & " Names Missing " & String( 20, "-" )
Dim sKey
For Each sKey In dicMiss.Keys
oTS.WriteLine sKey
Next
dicMiss.RemoveAll
For Each sKey In Split( sNames, vbCrLf )
dicMiss( sKey ) = 0
Next
oTS.WriteLine String( 20, "-" ) & " Names Found " & String( 20, "-" )
For Each sKey In dicFound.Keys
oTS.WriteLine sKey
Dim nIdx
For nIdx = 0 To dicFound( sKey ).Count - 1
oTS.WriteLine " " & dicFound( sKey )( nIdx )
Next
dicMiss.Remove( sKey )
Next
oTS.WriteLine String( 20, "-" ) & " Lines Missing " & String( 20, "-" )
For Each sKey In dicMiss.Keys
oTS.WriteLine sKey
Next
oTS.Close
WScript.Echo oFS.OpenTextFile( sRptFSpec ).ReadAll

Output:

=== compDKS: look for (missing) names in file =====================
tom
peter
joseph
ekkehard
---------------------
(\btom\b)|(\bpeter\b)|(\bjoseph\b)|(\bekkehard\b)
---------------------
-------------------- Names Missing --------------------
2: 192.168.1.2, this is mysql users, john, testmachine2
4: 192.168.0.0, this is sql users, maria, testmachine6
5: 192.168.0.0, this is sql users, donald, testmachine7
-------------------- Names Found --------------------
tom
1: 192.168.0.0, this is sql users, tom, testmachine
6: 192.168.0.0, this is sql users, tom, (again)
joseph
3: 192.168.0.0, this is sql users, joseph, testmachine5
-------------------- Lines Missing --------------------
peter
ekkehard

=== compDKS: 0 done (00:00:00) ====================================

for the slightly modified input files:

tom
peter
joseph
ekkehard

and

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
192.168.0.0, this is sql users, tom, (again)

Feel free to ask for explanations.

Re: Need a script to compare two files by ekkehard

ekkehard
Tue Apr 29 16:48:23 CDT 2008

Tom Lavedas schrieb:
> On Apr 29, 4:16 pm, Tom Lavedas <tglba...@cox.net> wrote:
>> On Apr 29, 3:23 pm, "DKS" <dksa...@hotmail.com> 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.

> 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

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

> 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
[...]

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


Re: Need a script to compare two files by DKS

DKS
Tue Apr 29 16:51:14 CDT 2008

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...@cox.net> wrote:
> > On Apr 29, 3:23 pm, "DKS" <dksa...@hotmail.com> 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/
>

Re: Need a script to compare two files by DKS

DKS
Tue Apr 29 16:57:00 CDT 2008

Hello Ekkehard,
Thanks for the reply. Is it possible to add some
comments in the script something like "Scripting guy". Thanks in advance.

DKS



"ekkehard.horner" wrote:

> DKS schrieb:
> > 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:
> > Const ForReading = 1
> >
> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> > Set objFile1 = objFSO.OpenTextFile("C:\Audit\User_Info.txt", ForReading)
> >
> > strAddresses = objFile1.ReadAll
> > objFile1.Close
> >
> > Set objFile2 = objFSO.OpenTextFile("C:\Audit\User_First_Name.txt", ForReading)
> >
> > Do Until objFile2.AtEndOfStream
> > strCurrent = objFile2.ReadLine
> > If InStr(strAddresses, strCurrent) <> 0 Then
> > strNoAddress = strNoAddress & strCurrent & vbCrLf
> > End If
> > Loop
> >
> > objFile2.Close
> >
> > Wscript.Echo "Devices with no address: " & vbCrLf & strNoAddress
> >
> > Set objFile3 = objFSO.CreateTextFile("C:\Audit\User_New_Info.txt")
> >
> > objFile3.WriteLine strNoAddress
> > objFile3.Close
>
> Another approach:
>
> Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
> Dim sNamFSpec : sNamFSpec = ".\dksnames.txt"
> Dim sUsrFSpec : sUsrFSpec = ".\dksusers.txt"
> Dim sRptFSpec : sRptFSpec = ".\dksreport.txt"
> Dim dicFound : Set dicFound = CreateObject( "Scripting.Dictionary" )
> Dim dicMiss : Set dicMiss = CreateObject( "Scripting.Dictionary" )
> Dim sNames : sNames = oFS.OpenTextFile( sNamFSpec ).ReadAll
> Dim oRE : Set oRE = New RegExp
>
> oRE.Pattern = "[\r\n]+$"
> sNames = oRE.Replace( sNames, "" ) ' get rid of trailing vbCrLfs
> WScript.Echo sNames + vbCrLf + "---------------------"
>
> oRE.Pattern = "(\b" & Replace( sNames, vbCrLf, "\b)|(\b" ) & "\b)"
> WScript.Echo oRE.Pattern + vbCrLf + "---------------------"
>
> Dim oTS : Set oTS = oFS.OpenTextFile( sUsrFSpec )
> Dim nLine : nLine = 0
> Do Until oTS.AtEndOfStream
> Dim sLine : sLine = Trim( oTS.ReadLine )
> nLine = nLine + 1
> If "" <> sLine Then
> Dim oMTS : Set oMTS = oRE.Execute( sLine )
> sLine = nLine & ": " & sLine
> If 0 = oMTS.Count Then
> dicMiss( sLine ) = 0
> Else
> Dim sName : sName = oMTS( 0 ).Value
> If Not dicFound.Exists( sName ) Then
> Set dicFound( sName ) = CreateObject( "Scripting.Dictionary" )
> End If
> dicFound( sName )( dicFound( sName ).Count ) = sLine
> End If
> End If
> Loop
> oTS.Close
>
> Set oTS = oFS.CreateTextFile( sRptFSpec, True )
> oTS.WriteLine String( 20, "-" ) & " Names Missing " & String( 20, "-" )
> Dim sKey
> For Each sKey In dicMiss.Keys
> oTS.WriteLine sKey
> Next
> dicMiss.RemoveAll
> For Each sKey In Split( sNames, vbCrLf )
> dicMiss( sKey ) = 0
> Next
> oTS.WriteLine String( 20, "-" ) & " Names Found " & String( 20, "-" )
> For Each sKey In dicFound.Keys
> oTS.WriteLine sKey
> Dim nIdx
> For nIdx = 0 To dicFound( sKey ).Count - 1
> oTS.WriteLine " " & dicFound( sKey )( nIdx )
> Next
> dicMiss.Remove( sKey )
> Next
> oTS.WriteLine String( 20, "-" ) & " Lines Missing " & String( 20, "-" )
> For Each sKey In dicMiss.Keys
> oTS.WriteLine sKey
> Next
> oTS.Close
> WScript.Echo oFS.OpenTextFile( sRptFSpec ).ReadAll
>
> Output:
>
> === compDKS: look for (missing) names in file =====================
> tom
> peter
> joseph
> ekkehard
> ---------------------
> (\btom\b)|(\bpeter\b)|(\bjoseph\b)|(\bekkehard\b)
> ---------------------
> -------------------- Names Missing --------------------
> 2: 192.168.1.2, this is mysql users, john, testmachine2
> 4: 192.168.0.0, this is sql users, maria, testmachine6
> 5: 192.168.0.0, this is sql users, donald, testmachine7
> -------------------- Names Found --------------------
> tom
> 1: 192.168.0.0, this is sql users, tom, testmachine
> 6: 192.168.0.0, this is sql users, tom, (again)
> joseph
> 3: 192.168.0.0, this is sql users, joseph, testmachine5
> -------------------- Lines Missing --------------------
> peter
> ekkehard
>
> === compDKS: 0 done (00:00:00) ====================================
>
> for the slightly modified input files:
>
> tom
> peter
> joseph
> ekkehard
>
> and
>
> 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
> 192.168.0.0, this is sql users, tom, (again)
>
> Feel free to ask for explanations.
>

Re: Need a script to compare two files by ekkehard

ekkehard
Tue Apr 29 17:54:57 CDT 2008

DKS schrieb:
> Hello Ekkehard,
> Thanks for the reply. Is it possible to add some
> comments in the script something like "Scripting guy". Thanks in advance.
As I wasn't sure about your specs - "compare" vs. desired output vs. the
negation in "strNoAddress" and "Devices with no address" - I decided to
do a script that collects
(a) the lines with names not in the list of names
(b) the lines for each name in the list
(c) the names without lines

Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )

No IO without a FSO, so let's have one.

Dim sNamFSpec : sNamFSpec = ".\dksnames.txt"
Dim sUsrFSpec : sUsrFSpec = ".\dksusers.txt"
Dim sRptFSpec : sRptFSpec = ".\dksreport.txt"

Two input files and one output file. Near the start of the program to ease
configuration. As I have no c:\audit (and wouldn't be allowed to access
it), I did it 'my way'.

Dim dicFound : Set dicFound = CreateObject( "Scripting.Dictionary" )
Dim dicMiss : Set dicMiss = CreateObject( "Scripting.Dictionary" )

To find something in an Array, you have to loop over its elements; see
Tom's script. Dictionaries are better suited to lookup - and as VBScript's
Arrays are especially cumbersome/non-dynamic, I generally prefer Dictionaries

Dim sNames : sNames = oFS.OpenTextFile( sNamFSpec ).ReadAll

Get the content of the file containing the names; no need for "ForReading",
because that's the dafault. But to avoid later grief caused by trailing
vbCrLf(s) - and Trim() is to stupid to remove them - we need a RegExp

Dim oRE : Set oRE = New RegExp

looking for a sequence of vbCr/vbLf(s) ([\r\n]+) before the end of the
string ($).

oRE.Pattern = "[\r\n]+$"
sNames = oRE.Replace( sNames, "" ) ' get rid of trailing vbCrLfs

Zap those vbCrLf(s) [if any] and show the reult

WScript.Echo sNames + vbCrLf + "---------------------"

To find the names in the lines we just reuse the RegExp oRE

oRE.Pattern = "(\b" & Replace( sNames, vbCrLf, "\b)|(\b" ) & "\b)"
WScript.Echo oRE.Pattern + vbCrLf + "---------------------"

with a different pattern: looking for the names framed/embraced by word
boundaries (\b). As the names are *separated* by vbCrLf, this pattern can
be generated by replacing the vbCrLfs with the boundaries and Or (|). But
don't forget the leading and trailing parts!

Dim oTS : Set oTS = oFS.OpenTextFile( sUsrFSpec )
Dim nLine : nLine = 0

Open the names list and initialize a line counter

Do Until oTS.AtEndOfStream

work thru the file

Dim sLine : sLine = Trim( oTS.ReadLine )
nLine = nLine + 1

get, trim, and count the line

If "" <> sLine Then

Apply the RegExp to non empty lines

Dim oMTS : Set oMTS = oRE.Execute( sLine )
sLine = nLine & ": " & sLine

Add the counter to the line (make it easier to check; avoid problems
with identical lines - we want to store them as keys in dicMiss)

If 0 = oMTS.Count Then

If none of the names is found in this line, collect it
dicMiss( sLine ) = 0
Else

Otherwise get the name found

Dim sName : sName = oMTS( 0 ).Value
If Not dicFound.Exists( sName ) Then

If there is no entry for this name, create one (an empty dictionary)

Set dicFound( sName ) = CreateObject( "Scripting.Dictionary" )
End If

Having an entry for this name, use the .Count of this sub dictionary as
a Key to store the line (indicating it was found for this name)

dicFound( sName )( dicFound( sName ).Count ) = sLine
End If
End If
Loop
oTS.Close

Set oTS = oFS.CreateTextFile( sRptFSpec, True )

Create the report file and list the lines for which there are no names in
the list

oTS.WriteLine String( 20, "-" ) & " Names Missing " & String( 20, "-" )
Dim sKey
For Each sKey In dicMiss.Keys
oTS.WriteLine sKey
Next

dicMiss.RemoveAll

Make dicMiss empty and add all names

For Each sKey In Split( sNames, vbCrLf )
dicMiss( sKey ) = 0
Next

Report the lines for each name

oTS.WriteLine String( 20, "-" ) & " Names Found " & String( 20, "-" )
For Each sKey In dicFound.Keys
oTS.WriteLine sKey

the name

Dim nIdx
For nIdx = 0 To dicFound( sKey ).Count - 1
oTS.WriteLine " " & dicFound( sKey )( nIdx )

the lines

Next
dicMiss.Remove( sKey )

remove the name from dicMiss to indicate it had lines
Next

report the names rmaining in dicMiss - the names not mentioned in the
second file

oTS.WriteLine String( 20, "-" ) & " Lines Missing " & String( 20, "-" )
For Each sKey In dicMiss.Keys
oTS.WriteLine sKey
Next

Done

oTS.Close

Now show the reult

WScript.Echo oFS.OpenTextFile( sRptFSpec ).ReadAll

Done 2


Re: Need a script to compare two files by DKS

DKS
Tue Apr 29 18:17:12 CDT 2008

Thanks for excellent script and explanation. I appreciate your help!!

"ekkehard.horner" wrote:

> DKS schrieb:
> > Hello Ekkehard,
> > Thanks for the reply. Is it possible to add some
> > comments in the script something like "Scripting guy". Thanks in advance.
> As I wasn't sure about your specs - "compare" vs. desired output vs. the
> negation in "strNoAddress" and "Devices with no address" - I decided to
> do a script that collects
> (a) the lines with names not in the list of names
> (b) the lines for each name in the list
> (c) the names without lines
>
> Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
>
> No IO without a FSO, so let's have one.
>
> Dim sNamFSpec : sNamFSpec = ".\dksnames.txt"
> Dim sUsrFSpec : sUsrFSpec = ".\dksusers.txt"
> Dim sRptFSpec : sRptFSpec = ".\dksreport.txt"
>
> Two input files and one output file. Near the start of the program to ease
> configuration. As I have no c:\audit (and wouldn't be allowed to access
> it), I did it 'my way'.
>
> Dim dicFound : Set dicFound = CreateObject( "Scripting.Dictionary" )
> Dim dicMiss : Set dicMiss = CreateObject( "Scripting.Dictionary" )
>
> To find something in an Array, you have to loop over its elements; see
> Tom's script. Dictionaries are better suited to lookup - and as VBScript's
> Arrays are especially cumbersome/non-dynamic, I generally prefer Dictionaries
>
> Dim sNames : sNames = oFS.OpenTextFile( sNamFSpec ).ReadAll
>
> Get the content of the file containing the names; no need for "ForReading",
> because that's the dafault. But to avoid later grief caused by trailing
> vbCrLf(s) - and Trim() is to stupid to remove them - we need a RegExp
>
> Dim oRE : Set oRE = New RegExp
>
> looking for a sequence of vbCr/vbLf(s) ([\r\n]+) before the end of the
> string ($).
>
> oRE.Pattern = "[\r\n]+$"
> sNames = oRE.Replace( sNames, "" ) ' get rid of trailing vbCrLfs
>
> Zap those vbCrLf(s) [if any] and show the reult
>
> WScript.Echo sNames + vbCrLf + "---------------------"
>
> To find the names in the lines we just reuse the RegExp oRE
>
> oRE.Pattern = "(\b" & Replace( sNames, vbCrLf, "\b)|(\b" ) & "\b)"
> WScript.Echo oRE.Pattern + vbCrLf + "---------------------"
>
> with a different pattern: looking for the names framed/embraced by word
> boundaries (\b). As the names are *separated* by vbCrLf, this pattern can
> be generated by replacing the vbCrLfs with the boundaries and Or (|). But
> don't forget the leading and trailing parts!
>
> Dim oTS : Set oTS = oFS.OpenTextFile( sUsrFSpec )
> Dim nLine : nLine = 0
>
> Open the names list and initialize a line counter
>
> Do Until oTS.AtEndOfStream
>
> work thru the file
>
> Dim sLine : sLine = Trim( oTS.ReadLine )
> nLine = nLine + 1
>
> get, trim, and count the line
>
> If "" <> sLine Then
>
> Apply the RegExp to non empty lines
>
> Dim oMTS : Set oMTS = oRE.Execute( sLine )
> sLine = nLine & ": " & sLine
>
> Add the counter to the line (make it easier to check; avoid problems
> with identical lines - we want to store them as keys in dicMiss)
>
> If 0 = oMTS.Count Then
>
> If none of the names is found in this line, collect it
> dicMiss( sLine ) = 0
> Else
>
> Otherwise get the name found
>
> Dim sName : sName = oMTS( 0 ).Value
> If Not dicFound.Exists( sName ) Then
>
> If there is no entry for this name, create one (an empty dictionary)
>
> Set dicFound( sName ) = CreateObject( "Scripting.Dictionary" )
> End If
>
> Having an entry for this name, use the .Count of this sub dictionary as
> a Key to store the line (indicating it was found for this name)
>
> dicFound( sName )( dicFound( sName ).Count ) = sLine
> End If
> End If
> Loop
> oTS.Close
>
> Set oTS = oFS.CreateTextFile( sRptFSpec, True )
>
> Create the report file and list the lines for which there are no names in
> the list
>
> oTS.WriteLine String( 20, "-" ) & " Names Missing " & String( 20, "-" )
> Dim sKey
> For Each sKey In dicMiss.Keys
> oTS.WriteLine sKey
> Next
>
> dicMiss.RemoveAll
>
> Make dicMiss empty and add all names
>
> For Each sKey In Split( sNames, vbCrLf )
> dicMiss( sKey ) = 0
> Next
>
> Report the lines for each name
>
> oTS.WriteLine String( 20, "-" ) & " Names Found " & String( 20, "-" )
> For Each sKey In dicFound.Keys
> oTS.WriteLine sKey
>
> the name
>
> Dim nIdx
> For nIdx = 0 To dicFound( sKey ).Count - 1
> oTS.WriteLine " " & dicFound( sKey )( nIdx )
>
> the lines
>
> Next
> dicMiss.Remove( sKey )
>
> remove the name from dicMiss to indicate it had lines
> Next
>
> report the names rmaining in dicMiss - the names not mentioned in the
> second file
>
> oTS.WriteLine String( 20, "-" ) & " Lines Missing " & String( 20, "-" )
> For Each sKey In dicMiss.Keys
> oTS.WriteLine sKey
> Next
>
> Done
>
> oTS.Close
>
> Now show the reult
>
> WScript.Echo oFS.OpenTextFile( sRptFSpec ).ReadAll
>
> Done 2
>
>

Re: Need a script to compare two files by Steve

Steve
Wed Apr 30 05:59:03 CDT 2008

DKS wrote:
>
> 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
>

Instead of a script, how about using a DOS utility?

findstr /g:file1 file2 > output

FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P]
[/F:file] [/C:string] [/G:file] [/D:dir list]
[/A:color attributes] [/OFF[LINE]]
strings [[drive:][path]filename[ ...]]

/B Matches pattern if at the beginning of a line.
/E Matches pattern if at the end of a line.
/L Uses search strings literally.
/R Uses search strings as regular expressions.
/S Searches for matching files in the current directory and
all subdirectories.
/I Specifies that the search is not to be case-sensitive.
/X Prints lines that match exactly.
/V Prints only lines that do not contain a match.
/N Prints the line number before each line that matches.
/M Prints only the filename if a file contains a match.
/O Prints character offset before each matching line.
/P Skip files with non-printable characters.
/OFF[LINE] Do not skip files with offline attribute set.
/A:attr Specifies color attribute with two hex digits. See
"color /?"
/F:file Reads file list from the specified file(/ stands for
console).
/C:string Uses specified string as a literal search string.
/G:file Gets search strings from the specified file(/ stands for
console).
/D:dir Search a semicolon delimited list of directories
strings Text to be searched for.
[drive:][path]filename
Specifies a file or files to search.

Use spaces to separate multiple search strings unless the argument is
prefixed with /C. For example, 'FINDSTR "hello there" x.y' searches
for "hello" or "there" in file x.y. 'FINDSTR /C:"hello there" x.y'
searches for "hello there" in file x.y.

Regular expression quick reference:
. Wildcard: any character
* Repeat: zero or more occurances of previous character or
class
^ Line position: beginning of line
$ Line position: end of line
[class] Character class: any one character in set
[^class] Inverse class: any one character not in set
[x-y] Range: any characters within the specified range
\x Escape: literal use of metacharacter x
\<xyz Word position: beginning of word
xyz\> Word position: end of word

For full information on FINDSTR regular expressions refer to the online
Command Reference.

--
Steve

The foolish and the dead al