VBS – Read txt file and delete duplicated lines

24 January, 2011 at 11:54 am Leave a comment

‘READ TXT FILE
‘CREATE A NEW TXT FILE WITH NOON-DUPLICATED ENTRIES
‘EXTRACT SERVER NAMES

strFile = InputBox( “Enter file path, eg D:\all_unix” )

If strFile = “” Then
WScript.Echo “Script quitting.”
WScript.Quit
End If

Dim objFSO: Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Dim dicSort: Set dicSort = CreateObject(“Scripting.Dictionary”)

If objFSO.FileExists(strFile & “.txt”) Then

Dim objFile: Set objFile = objFSO.OpenTextFile(strFile & “.txt”)

‘check and delete all duplicated entries============
Do While Not objFile.AtEndOfStream
On Error Resume Next
strData = objFile.ReadLine
dicSort.Add strData, dicSort.Count
Loop
‘===================================================

objFile.Close

‘create output file
Set objFile = objFSO.CreateTextFile(strFile & “_ServerNameOnly.txt”)

‘go through non-duplicated file, get server name only =================
For Each Item In dicSort

find_Primary = InStr(Item, “Primary:”)
find_NT = InStr(Item, “:NT”)
find_KUX = InStr(Item, “:KUX”)

‘if line has Primary:
If (find_Primary <> 0) Then
objFile.WriteLine (Mid (Item, 9, (find_NT – 9) ) )
End If

‘if line has :KUX
If (find_KUX <> 0) Then
objFile.WriteLine (Left (Item, (find_KUX – 1) ) )
End If

Next
‘=======================================================================

objFile.Close

Else
WScript.Echo “Cannot find file. Script quitting.”
WScript.Quit
End If

Set objFile = Nothing
Set objFSO=Nothing

WScript.Echo “Finish!”

Entry filed under: IT/Soft.

boss talk in a word

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


January 2011
M T W T F S S
 12
3456789
10111213141516
17181920212223
24252627282930
31  

%d bloggers like this: