|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Find lines duplicate in a text file
Hi at all..
i don't understand where i wrong. I would like fin a line duplicates in a text file. My code i wrong. Where? Thank's a lot. Bye from Italy. ------------------------------ CODE START ------------------------------------- Const ForReading = 1, ForWriting = 2, ForAppending = 3 Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 Dim fs, f, ts, s, filename, oldline, max filename= "pippo.TXT" Set fs = CreateObject("Scripting.FileSystemObject") Set f = fs.GetFile(filename) Set tsmax = f.OpenAsTextStream(ForReading, Tristatefalse) while not tsmax.AtEndOfStream max=max+1 text=tsmax.ReadLine wend msgbox (max&"="&text) tsmax.Close Set tsorig = f.OpenAsTextStream(ForReading, Tristatefalse) for i = 1 to max orig=tsorig.ReadLine start = tsorig.line wscript.echo orig&start Set tscomp = f.OpenAsTextStream(ForReading, TristateFalse) for a = start to max comp=tscomp.ReadLine if orig=comp then wscript.echo "Record :"& orig & " is duplicated " end if next tscomp.Close next tsorig.Close ----------------------------------- CODE END ----------------------------- Last edited by Zinzi : September 12th, 2003 at 09:28 AM. |
|
#2
|
||||
|
||||
|
The code is OK, except for one thing. Lets say that there are 8 lines in the text file. It reads the first line, and compares it with lines 1 to 8. It finds a match (bcs line 1 is the same as line 1). Then it reads line 2, and compares it with line 2 to 7 (finds a match, bcs line 2 is the same as line 2) Then it reads line 3, and compare it with line 3 to 6. etc.
The best way to solve this problem will be to tell it to start in the middle of the text file (at line 2, 3, etc). I am not sure if you can do this (maybe someone else can help out) Otherwise (if you know that it's going to be a small file), you can read everything into an array when you get the number of lines e.g. (untested) Code:
dim oArr(100) as variant while not tsmax.AtEndOfStream max=max+1 text=tsmax.ReadLine oArr(max) = text wend msgbox (max&"="&text) tsmax.Close Set tsorig = f.OpenAsTextStream(ForReading, Tristatefalse) for i = 1 to max orig=tsorig.ReadLine start = tsorig.line wscript.echo orig&start for a = start + 1 to max comp=oArr(a) if orig=comp then wscript.echo "Record :"& orig & " is duplicated " end if next or if you have can use access, you can use a temporary table to read everything into, and just select everything that appears twice. Otherwise you can use basically the same code as you have, and just read the entire file, comparing only if a is larger than start e.g. (untested) Code:
for i = 1 to max orig=tsorig.ReadLine start = tsorig.line wscript.echo orig&start Set tscomp = f.OpenAsTextStream(ForReading, TristateFalse) for a = 1 to max comp=tscomp.ReadLine if orig=comp and a > start then wscript.echo "Record :"& orig & " is duplicated " end if next Hope this helps |
|
#3
|
|||
|
|||
|
If the problem is due to comparing the line with itself( like line 1 with line 1 etc.,) as suggested by Silian, you can just skip this comparison. Put a line such as "if a=i then " and skip the comparison. This should solve the problem of self comparison.
- Anant Navale |
|
#4
|
|||
|
|||
|
I know how to find duplicated files on your disks. This is the best prog for it: http://www.atory.com/Dupe_Checker_PRO/
IMHO! |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Find lines duplicate in a text file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|