Link to home
Create AccountLog in
Avatar of PaulRKrueger
PaulRKrueger

asked on

VBscript combine lists of files, remove duplicates

I'm using a vbscript to examine and combine a set of text files in two separate folders. I need to take the file list from each folder, combine them, and remove any duplicates (note, I'm talking about the file list, not file manipulation at this point).

For example...

Files in source folder 1:
file1.txt
file2.txt
file3.txt

Files in source folder 2:
file1.txt
file3.txt
file5.txt

The resulting list should read:
file1.txt
file2.txt
file3.txt
file5.txt

I don't need to know anything specific about the files (date modified, etc.), I just need a list that I can then diagrammatically move through. I'm using the following code to pull the folder lists, however, I'm perfectly open to using a different method.

option explicit
Dim objFSO, A, B, FLD, strFolder, item
set objFSO = CreateObject("Scripting.FileSystemObject")

strFolder = "\\<network path>\LogonErrors"
	set FLD = objFSO.GetFolder(strFolder)
	set A = FLD.Files
	
strFolder = "\\<network path>\LogonSuccess"
	set FLD = objFSO.GetFolder(strFolder)
	set B = FLD.Files

Open in new window

Avatar of Bill Prew
Bill Prew

Here's a nice simple way to do it in a BAT script.  Adjust the SET statements near the top as needed.

@echo off
setlocal EnableDelayedExpansion

set d1=c:\ee\ee28036758\d1
set d2=c:\ee\ee28036758\d2
set list=report.txt
set last=

(
  for /f "tokens=*" %%A in ('dir /b /a-d "%d1%" "%d2%" ^| sort') do (
    if /i "%%~A" NEQ "!last!" (
      echo %%~A
      set last=%%~A
    )
  )
)>"%list%"

Open in new window

~bp
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of PaulRKrueger

ASKER

This is absolutely perfect. Thank you. Works like a charm and fits my need for a vbscript.
That's cool you preferred VBS, I knew that could be the case going in.

Just so you know, there is one downside to that approach.  ALL of the entries that were new from the second file will follow ALL of the entries from the first file.  They won't be in a sorted order in the output file, but rather the order they were added to the dictionary object.  The batch approach addressed that, but it may not matter to you.

Glad you got a useful solution.

~bp