Batch file to do a look up

September 29, 2009 at 19:53:38
Specs: Windows XP

I would like to write a batch file which will pick a string from file A check if that exists in file B, if not there in file B write it to new file.

my file A value is like this-

my file B value is like this-

After comparision I will have
in a new file.

I have tried my level best to search in google but in vain. Any help is greatly appreciated.

See More: Batch file to do a look up

Report •

September 29, 2009 at 20:10:45
These sort of questions belong in the programming forum.

Anyway this may do it:


Batch Variable how to

Report •

September 30, 2009 at 16:54:26
Hi Judago,

Thanks for your immediate response. This worked fantastic.

Also, I'm just thinking of somemore future scenarios we might get. Will the same script work for the scenario below? my B file name will be like this,

XXXX_XXX22XX.txt and so on....

All X will be alphabetical and 11 & 22 will be numeric. XX part will be same for all files but the numeric will change each time. The value will be any but will be in numbers. Also the number of files might also vary for each execution. I might have 2 file now 10 files tomorrow etc. But the file naming will be like the one above and the file content will be as like the one in my first post.

In this case after the comparision I want my file name to be
missingXXXX_XXX11XX.txt for XXXX_XXX11XX.txt
missingXXXX_XXX22XX.txt for XXXX_XXX22XX.txt etc.

This is my last question. If you can explain the code about what each command does, I can learn by myself so that I may not need to disturb you in future. Thanks in advance.

Report •

September 30, 2009 at 21:45:21
Provided your "file A" will contain the numbers then it should work:


FOR /F ....

Loop over the text output of a file command or string exchanging the current token(chunk of text).


These are options for the for /f loop I used, the are however others. "USEBACKQ" tells the for /f loop that the meaning of the quote characters should use the "new" sematics, where double quotes enclose file names(important for file names with spaces or ampersands).

"delims=" tells the for loop to split up the text on the delimiters right of the equals sign and before the double quote, leaving it blank means the text won't be split up.

... %%A ...

This is the for loop variable it can be any letter(among other characters), is case sensitive and is only valid within the for loop. It will contain the lines/tokens on at a time.


This simply tells the for loop we will be iterating over the file.

DO (

The code block(inside parentheses)will be executed once for every line/token of text.


A couple of things are being done here, the ">nul" basically says "discard all output" and the find command is looking for the text held in the variable "%%A" in the text file. Find will leave an errorlevel(return code) of zero if it finds the string and one if it doesen't.


The double pipe means "if the command to the left returned an errorlevel greater than zero execute the command to the right"


This command only gets executed if the find returned an errorlevel greater than zero. the ">>" means append the out put of the command to and "newfile" is a text file, echo simply outputs the text in %%a.

Recommended reading;


for /?
find /?
if /?

Batch Variable how to

Report •

Related Solutions

Ask Question