batch file to compare and delete unidentical files

April 10, 2012 at 03:40:21
Specs: Windows XP
Working on a batch file from past few days but no luck yet.

I am in need of a script that will compare an XML file with a folder and shall delete nonidentical files from the folder whose file names are not mentioned in the XML file.
i.e, I want to keep the files which are there in the build list(XML) and delete the rest from the folder.

For Eg: I have a folder named D:\123 which has around 15 files in it. I have a build list (XML file) which has 10 filenames which are there in D:\123. Now I want a batch file script which will delete the rest 5 files from D:\123 which is not there in the xml file.

XML file looks like (BuildList.XML):

The batch file must be able to compare BuildList.XML with D:\123 and its subfolders, for instance - D:\123\subfolder1 and D:\123\subfolder2. Keep the identical files and DELETE nonidentical files from the folder D:\123\subfolder1 or 2.

Any help will be highly appreciated!

See More: batch file to compare and delete unidentical files

Report •

April 10, 2012 at 04:20:55
Like this?

@echo off
for /f "tokens=*" %%i in (
'dir /a-d /b /s *.txt'
) do (
findstr /i /l "%%i" buildlist.xml >nul || echo del "%%i"

If the output looks correct, remove the echo.


Report •

April 10, 2012 at 22:13:45
@tonysathre - Thanks for your reply.

How will I set the source directory (where the files have to be deleted) in your script? Things I tried till now:

This script actually deletes all the files from the folder, irrespective of the file being present or not in the xml list file. (any tweak with this?)
@echo off
rem Create a list of existent file names in D:\123 *at any level*
for /R "D:\123" %%a in (*.*) do set "fileName[%%a]=1"
ECHO List of existent files:
SET fileName[
rem Process the build list and remove found names from existent files list
ECHO Keep these files:
for /F "tokens=2-4 delims=<>" %%a in (C:\ReadXML\buildList.xml) do (
if "%%a" == "Project" if "%%c" == "/Project" (
ECHO fileName[%%b]
set "fileName[%%b]="
ECHO Remove these files:
rem Remove the remaining files
for /F "tokens=2 delims=[]" %%a in ('set fileName[') do (
echo del "%%a"

Report •

April 11, 2012 at 00:27:59
Run it from D:\


Report •

Related Solutions

April 11, 2012 at 00:41:11
@tonysathre - Great.. Perfect!!! :) Have been working on this script for some time now.. :) This works like a charm if I keep my buildList.xml and the batch file in the D:\.

But this script actually deletes all the text files in D:\. Whereas I need to put a check only on dir D:\123 and its subsequent subfolders.

one more thing - Can I check files in the folder/subfolders with different file extensions, other than .txt (.vbproj, .xml etc) too??. i.e, I want to delete other files (with different extn) too if they are not there in the buildlist.xml. Can this be achieved using *.* instead of *.txt.? I can't check as I fear it will flush off all the files from my D:\ :)

Also, can I delete subfolders too if they are not mentioned in my buildlist?

Thanks for your help! Greatly appreciate it :)

Report •

April 11, 2012 at 12:31:31
This should work:

@echo off
set build=c:\test\buildlist.xml
pushd 123
for /f "tokens=*" %%i in (
'dir /a-d /b /s'
) do (
findstr /i /l "%%i" %build% >nul || del "%%i
for /f "tokens=*" %%z in (
'dir /ad /b /s'
) do (
findstr /i /c:"%%z" %build% >nul || rd /q "%%z"


Report •

April 12, 2012 at 01:41:49
@tonysathre - This script deleted all of .txt / .xml (and I don't know what all) from my C:\ :(.

Do I need to make any changes before running it? coz you've set only the xml file's path as in c:\. pushd 123 shall be compared with the xml right?

Am I missing any change in the script here.. ??

Report •

April 12, 2012 at 14:46:01
You need to run it from the d: drive. Add an echo before the rd Nd del commands do it only shows you what it will do. Just change the paths accordingly.


Report •

Ask Question