Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copyfile then compare both

Status
Not open for further replies.

ajtsystems

IS-IT--Management
Jan 15, 2009
80
GB
Hi,

I have a file which I search through and then if I find a string I am looking for the string gets copied to a results.txt file.

What i need to do is initially take a copy of the results.txt file to a results copy.txt file. Next time the script is run the results.txt file is updated with new strings, then results.txt and resultscopy.txt are compared. if they are different then echo to screen "something..."

does anyone know how this might be done? I am strugling with error handling and running the script for the first time. i.e. results.txt exists already and only needs to be copied to resultscopy.txt the first time then next time round results.txt and resultscopy.txt need to be compared...

 
Post what you have so far...

I'll use pseudocode to outline what your script should be doing...

Code:
parse sourcefile.txt for string
[tab]copy (append) interesting string to results.txt

if resultscopy.txt exists then
[tab]parse for differences between results.txt and resultscopy.txt
[tab][tab]output differences to screen
[tab]delete resultscopy.txt (old file is no longer needed)
[tab]copy results.txt to resultscopy.txt (so differences can be detected on next run)

PSC
[—] CCNP [•] CCSP [•] MCITP: Enterprise Admin [•] MCSE [—]

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
OK, Here is what I have so far. It needs ordering and sorting i think t=before it will work##

copied = false
Set objFSO = CreateObject("Scripting.FileSystemObject")
results = "C:\\Scripts\\results.txt"
test = "C:\Scripts\Test.txt"
resultsCopy = "C:\\Scripts\\resultsCopy.txt"
searchstring = "Somestring"
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "^25-09-2007 Somestring"

'Create Text Files

if objFSO.fileexists(results) then
'ResultsFile = true
else
objfso.createtextfile(results)
End if

if objFSO.fileexists(resultsCopy) then
'copy = true
else
objfso.createtextfile(resultsCopy)
End if

Dim arrFileLines()
i = 0
Set objFile = objFSO.OpenTextFile(test, 1)
Set objFile1 = objFSO.OpenTextFile(results, 2)
Set objFile2 = objFSO.GetFile(test)
Set objFile3 = objFSO.GetFile(results)
Set objFile4 = objFSO.GetFile(resultsCopy)
'Set objFile4 = objFSO.OpenTextFile(resultsCopy, 2)


'check through text file and create 1d array
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close


'Read through array from last line to 1st line
For l = Ubound(arrFileLines) to LBound(arrFileLines) Step -1
hello = arrFileLines(l)

'Check for regular expression and write to file
Set colMatches = objRegEx.Execute(hello)
If colMatches.Count > 0 Then
For Each strMatch in colMatches
Wscript.Echo (hello)
Next
objFile1.writeline (hello)
End If
next


objFile1.Close


'check file size

if objFile2.Size = objFile3.Size then
same = true
else
same = false
end if


retvalue = ShowFileSum(FileSize1, FileSize2)

MsgBox "The function returned: " & retValue


'call function, if file has changed then same is 0
showFileSum 0, same

Function ShowFileSum(FileSize1, FileSize2)
Dim sum

' Determine and display the sum.
sum = FileSize1 + FileSize2

' Set the function's return value.
ShowFileSum = sum

End Function






 
You're pretty close... Look at the logic of the pseudocode above... you can echo the differences using something like this...

Code:
EchoDiffs objFSO, results, resultscopy

[green]' Lines in Input1 that are not present in Input2 are echoed[/green]
Sub EchoDiffs(oFSO, sInput1, sInput2)
	[green]' TS = Text Stream, o = object, s=string[/green]
	Dim oTSIn1, oTSIn2, sLine1, sLine2
	Const ForReading = 1

	Set oTSIn1 = oFSO.OpenTextFile(sInput1, ForReading)
	Set oTSIn2 = oFSO.OpenTextFile(sInput2, ForReading)

	Do
		sLine1 = oTSIn1.ReadLine

		If oTSIn2.AtEndOfStream Then
			WScript.Echo sLine1
		Else
			sLine2 = oTSIn2.ReadLine

			If sLine1 <> sLine2 Then WScript.Echo sLine1
		End If
	Loop Until oTSIn1.AtEndOfStream

End Sub [green]'EchoDiffs[/green]


PSC
[&mdash;] CCNP [&bull;] CCSP [&bull;] MCITP: Enterprise Admin [&bull;] MCSE [&mdash;]

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top