Jump to content



Photo

Finding duplicates lines in text files - Vbs script to Autioit


  • Please log in to reply
1 reply to this topic

#1 kwyking

kwyking

    Seeker

  • Normal Members
  • 2 posts

Posted 13 March 2012 - 06:39 AM

Hi forum,

Can someone help me convert this vbs script so that i can include it in my autoit script. I don't want to run it as a seperate script if i can help it.


[code=''text'']'# "Search for duplicate lines"
'# www.petri.co.il/forums/showthread.php?t=32793
'# author: Remco Simons [NL] 2009
Const ForReading = 1
Const dictKey = 1
Const dictItem = 2
' Search for duplicate lines in:
TXTFile = "c:test.txt"
Set objDictionary1 = CreateObject("Scripting.Dictionary")
objDictionary1.CompareMode = vbTextCompare
Set objDictionary2 = CreateObject("Scripting.Dictionary")
objDictionary2.CompareMode = vbTextCompare
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(TXTFile, ForReading)
iCnt = 0
Do Until objFile.AtEndOfStream
strName = objFile.ReadLine
If Not strName = "" Then
iCnt = iCnt + 1
If (objDictionary1.Exists(strName) = False) Then
objDictionary1.Add strName, iCnt
Else
foundfirst = objDictionary1.Item(strName)
foundfirst = Right("0000"&CStr(foundfirst),4) _
& " - - - - - - - - - - - - - - - - - - -"
If (objDictionary2.Exists(foundfirst) = False) Then
objDictionary2.Add foundfirst, UCase(strName)
End If
objDictionary2.Add Right("0000"&CStr(iCnt),4), strName
End If
End If
Loop
objFile.Close
objDictionary1.RemoveAll
SortDictionary objDictionary2, dictItem, dictKey
Dim arrOut()
Z = objDictionary2.Count: If Z > 0 Then
' create an 2D-array to store dictionary information
ReDim arrOut(Z,2)
X = 0
' populate the string array
For Each objKey In objDictionary2
arrOut(X,dictKey) = CStr(objKey)
arrOut(X,dictItem) = CStr(objDictionary2(objKey))
X = X + 1
Next
wscript.echo Join2D(arrOut, vbNewLine)
Else
wscript.echo "No duplicate entries found"
End If
objDictionary2.RemoveAll
wscript.quit
Function SortDictionary(objDict, intSortA, intSortB)
' http://support.microsoft.com/kb/246067
' declare our variables
Dim strDict()
Dim objKey
Dim strKey,strItem
Dim X,Y,Z
' get the dictionary count
Z = objDict.Count
' we need more than one item to warrant sorting
If Z > 1 Then
' create an array to store dictionary information
ReDim strDict(Z,2)
X = 0
' populate the string array
For Each objKey In objDict
strDict(X,dictKey) = CStr(objKey)
strDict(X,dictItem) = CStr(objDict(objKey))
X = X + 1
Next
' perform a a shell sort of the string array
For X = 0 to (Z - 2)
For Y = X to (Z - 1)
If StrComp((strDict(X,intSortA) & strDict(X,intSortB)), _
(strDict(Y,intSortA) & strDict(Y,intSortB)), _
vbTextCompare) > 0 Then
strKey = strDict(X,dictKey)
strItem = strDict(X,dictItem)
strDict(X,dictKey) = strDict(Y,dictKey)
strDict(X,dictItem) = strDict(Y,dictItem)
strDict(Y,dictKey) = strKey
strDict(Y,dictItem) = strItem
End If
Next
Next
' erase the contents of the dictionary object
objDict.RemoveAll
' repopulate the dictionary with the sorted information
For X = 0 to (Z - 1)
objDict.Add strDict(X,dictKey), strDict(X,dictItem)
Next
End If
End Function
Function Join2D(varData, strDelim)
Dim strOutpu
For i = LBound(varData, 1) to UBound(varData, 1)
For j = LBound(varData, 2) to UBound(varData, 2)
If Len(strOutput) > 0 _
Then strOutput = stroutput & strDelim
strOutput = stroutput & varData(i, j)
Next
Next
Join2D = strOutput
End Function[/code]

Attached Files


Edited by kwyking, 13 March 2012 - 06:41 AM.








#2 Spiff59

Spiff59

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 1,312 posts

Posted 13 March 2012 - 06:51 AM

There are some recent modified _ArrayUnique functions using Scripting.Dictionary in the Example Script forum that would get you on the right path. Try a search on that function name...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users