Confirm each item in one file is found in another

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dlminehart
Posts: 1
Joined: 21 Jul 2011 14:57

Confirm each item in one file is found in another

#1 Post by dlminehart » 21 Jul 2011 15:15

I have two files. First (goodlist.txt) is just a list of words, one per line, against which I wish to validate. Second (checkme.csv) is a CSV text file, with one "column" (e.g., the word between the 2nd and 3rd commas) containing a word I wish to ensure is found within the other file, the definitive list of allowable words.

If the csv file is OK, do one thing (nothing, or a message, or move the csv to a GoodFiles folder). If any line in the csv contains a word between the 2nd and 3rd commas that is not found in the reference file, do something else (a message, error file, beep, move csv to BadFiles folder...) that indicates the line number on which the erroneous code word was found, and the erroneous code word itself.

I'm hoping I don't have to pull out the big guns and move up to VB or such. Thanks for any help!

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Confirm each item in one file is found in another

#2 Post by dbenham » 21 Jul 2011 16:06

Untested code:

Code: Select all

@echo off
setlocal

set goodFile="goodlist.txt"
set checkFile="checkme.csv"
set badFile="badWords.txt"

if exist %badFile% del %badFile%
for /f "usebackq eol=, tokens=3 delims=," %%a in (%checkFile%) do (
  echo %%a|findstr /vx /g:%goodFile% >>%badFile%
)
if exist %badfile% (
    echo %checkFile% is BAD
    echo The following words in column 3 failed:
    type %badFile%
) else (
    echo %checkFile% is GOOD
)

The code assumes that columns 1 and 2 are never empty - if they are then the code requires a major rewrite.

Adjustments will need to be made if either file contains special characters, quotes or leading or trailing spaces.

The search can be made case insensitive by adding the /I option to the FINDSTR command.

Dave Benham

Post Reply