HEELPBOOK - How to Sort a File Content in to either Alphabetic or Numeric Order (VBScript) ####################### The easiest way to eliminate duplicate entries is going to utilizing a object that has the ability to see if an object exists (Scripting.Dictionary) or contains (System.Collections.ArrayList) an entry already. Let's see a sample text file containing the following rows... ######## Servers.txt (Before) SERVER1 SERVER2 SERVER3 SERVER4 SERVER2 SERVER5 SERVER1 SERVER6 SERVER3 SERVER4 SERVER5 ######## The Script Option Explicit Const FOR_READING = 1 Const FOR_WRITING = 2 Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject") Dim arrLines : Set arrLines = CreateObject("System.Collections.ArrayList") 'Open the file Dim objFile : Set objFile = objFSO.OpenTextFile("C:\servers.txt", FOR_READING, False) 'Loop thru and add each line to the array Do Until objFile.AtEndOfStream strLine = Trim(objFile.ReadLine) 'Verify that array doesn't already have the entry If Not arrLines.Contains(strLine) Then arrLines.Add(strLine) Loop objFile.Close 'Sort (ascending) for aesthetics arrLines.Sort() 'Join the array with vbCrLf (carriage return or enter) Dim strNewFile : strNewFile = Join(arrLines.ToArray, vbCrLf) 'Re-open the file for reading Set objFile = objFSO.OpenTextFile("C:\servers.txt", FOR_WRITING, False) 'Write the new text objFile.Write strNewFile objFile.Close ######## Servers.txt (After) SERVER1 SERVER2 SERVER3 SERVER4 SERVER5 SERVER6 ############ ARTICLE INFO ############# Article Month: February Article Date: 28/02/2013 Permalink: http://heelpbook.altervista.org/2013/how-to-sort-a-file-content-in-to-either-alphabetic-or-numeric-order-vbscript/ Source: http://www.visualbasicscript.com/VBScript-to-Sort-a-Files-Contents-in-to-Either-Alphabetic-or-Numeric-Order-m89972.aspx Language: English View more articles on: http://www.heelpbook.net/ Follow us on Facebook: http://it-it.facebook.com/pages/HeelpBook/100790870008832 Follow us on Twitter: https://twitter.com/#!/HeelpBook Follow us on RSS Feed: http://feeds.feedburner.com/Heelpbook Follow us on Delicious: http://delicious.com/heelpbook Linkedin: http://it.linkedin.com/pub/stefano-maggi/27/73a/b20 Google+ : https://plus.google.com/116990277568167008289/posts