'======================================================================================================== '======================================================================================================== ' ### Script: trim.vbs ' ### ScriptVersion: 0 ' ### Created: 04/04/04 ' ### Contact: Tim Chovanak, timc@blackpondfarm.com ' ### Description: Trims duplicate lines out of any file '======================================================================================================== '======================================================================================================== Option explicit On error resume next Dim wshNetwork,wshShell,wshFile Dim objDictionary,ItemsArray,KeysArray,ArrayItems,i Dim objArgs Dim strinputFile Dim stroutputFile Dim objFolder Dim strPath Set Wshnetwork = wscript.CreateObject("Wscript.Network") Set Wshshell = wscript.CreateObject("Wscript.shell") Set Wshfile = wscript.CreateObject("Scripting.FileSystemObject") 'Make sure the host is cscript, IF not THEN set default to cscript IF instr(1, wscript.fullname, "cscript.exe", 1) = 0 THEN VerifyCscript() 'Read our command line arguments, proceed if we have the correct input or print help if we don't. Set objArgs = WScript.Arguments If objArgs.Count <> 2 Then wscript.echo "Usage: trim.vbs " wscript.echo "" wscript.echo "Removes any duplicate lines from a file" wscript.echo "Input file is the file to filter, output file is a new file for the filtered results." Else strInputFile = objArgs(0) strOutputFile = objArgs(1) End If 'Set up a dictionary to hold a copy of each entry for the filter Set objDictionary = CreateObject("Scripting.Dictionary") ItemsArray = objDictionary.Items KeysArray = objDictionary.Keys 'Clear out any existing output file If (Wshfile.fileexists(strOutputFile)) Then Wshfile.deletefile(strOutputFile) 'Proceed with our search for duplicates FilterFile(strPath) '======================================================================================================== '======================================================================================================== Function FilterFile(strPath) On error resume next Dim wshtempfile Dim Line,FullLine,Position,Temp wscript.echo "" wscript.echo "Filtering duplicate lines from " & strInputFile & " and writing to " & strOutputFile & "." 'Open our input file Set wshtempfile = wshfile.OpenTextFile(strInputFile, 1) 'If we have an error, print the error and exit If Err.Number <> 0 Then screenout Err.Description wscript.quit ELSE 'Read through our input file Do While wshtempfile.AtEndOfStream <> true 'For each line, see if we already have a matching line within the dictionary line = wshtempFile.ReadLine IF objDictionary.exists(Line) THEN 'We already have this line, don't do anything with it wscript.echo "Discarding " & Line ELSE 'This is a unique new line, store a copy in the dictionary and print to screen and the output file objDictionary.Add Line,1 Screenout Line END IF loop END IF 'Close out our files wshtempfile.Close wscript.DisconnectObject wshtempfile Set wshtempfile=nothing wscript.echo "Finished!" END Function '======================================================================================================== '======================================================================================================== Function screenout(text) Dim wsherrorlogfile On Error Resume Next If (Wshfile.fileexists(strOutputFile)) Then 'If our output file exists, open it, write our data, and close Set wsherrorlogfile = Wshfile.OpenTextFile(strOutputFile, 8) wsherrorlogfile.writeline (text) wsherrorlogfile.Close wscript.DisconnectObject wsherrorlogfile Set wsherrorlogfile = Nothing If Err.number <> 0 Then Err.Clear Else 'File doesn't yet exist, create our output file and write our first line of text Set wsherrorlogfile = Wshfile.createtextfile(strOutputFile, 1) wsherrorlogfile.writeline (text) wsherrorlogfile.Close wscript.DisconnectObject wsherrorlogfile Set wsherrorlogfile = Nothing If Err.number <> 0 Then Err.Clear End If End Function '======================================================================================================== '======================================================================================================== Function VerifyCscript() Dim temp 'Sets up Vbscript to always run in command window temp = MsgBox ("The script is changing your default output of Windows Scripting Host to the command prompt." &_ vbCrLf & "This is pop up is normal, just re-run the script after the third pop-up.", 0, "WSH default changed to cscript.") temp = wshshell.Run("cmd /c ""wscript //h:cscript //nologo //s 1>nul 2>nul""", 0, true) wscript.quit END Function '======================================================================================================== '========================================================================================================