We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


Batch script to delete certain files in tree

sneeuw asked
Medium Priority
Last Modified: 2007-12-19

On my Win98 machine I always ran this batch script to delete a bunch of temporary files in the directory from where the bacth was run and its sub-directories.

It doesn't seem to work using NT (2K, XP)

Question, can somebody suggest a solution ?
Preferably a solution that works well on Win98 ranging to WinXP but if that is not possible then no problem.
I have to ditch 98 sooner or later anyway.

This is the batch I have been using all these years :

echo off

rem del __del.lst
rem del __dell.lst
rem del __dell.bat

del deleted.lst

dir *.~?? /s/b >> __dell.lst
dir *.~? /s/b >> __dell.lst
dir *.il? /s/b >> __dell.lst
dir *.obj /s/b >> __dell.lst

copy __dell.lst deleted.lst

:: http://www.ericphelps.com/batch/samples/vxe2exe.bat.txt
:: Make a line fragment
:: See http://www.ericphelps.com/batch/lines/frag-dbg.htm

echo e 100 "del " > script
echo e 104 22 >> script
echo rcx>> script
echo 5>> script
echo n __del.lst>> script
echo w>> script
echo q>>script
debug < script > nul
del script

:: Now process each line
    rem *** Check to see if __dell.lst is zero bytes
    dir __dell.lst | find "__dell.lst" | find " 0 " > nul
    if not errorlevel 1 goto DONE
    copy /b __del.lst + __dell.lst __dell.bat  > nul
    rem *** Create a new __dell.lst minus the first line
    type __dell.bat | find /v "del " > __dell.lst
    rem *** Trim the bat file so it only has the first line
    type __dell.bat | find "del " > __dell.bat
    rem *** Display :
    type __dell.bat
    rem *** Execute :
    call __dell.bat
goto LOOPSTART    

del __del.lst
del __dell.lst
del __dell.bat
Watch Question

Top Expert 2007

That seems really complicated to delete some files. How about this:

@echo off

for %%a in (*.~??, *.~?, *.il?, *.obj) do echo del "%%a"

Remove the echo command to actually delete the files.

Good Luck,



Seems to work but only in the dir itself and not in subdirectories ?
Is that possible ?



echo seems to display each situation twice ?
That's not a problem of course ... I was just wondering why that is ?
Top Expert 2007

In 2000 you can do the following:

@echo off

for /R c:\ %%a in (*.~??, *.~?, *.il?, *.obj) do echo del "%%a"

The *.~?? appears to be matching some of the same files as *.~?. For example:

dir *.~??

matches the following files on my machine:


If you need the processing to work in both 98 and other operating systems you could test the OS variable and do specific code for 98 and then for NT:

if "%OS%"=="" goto DO98CODE

REM ** Do this processing...


for /R c:\ ....
mean that this process will be done starting from the C: root ?

I only need it done, starting from the dir it is executed in.
Which btw. will not be the c drive.

I have little or no knowledge on batch files so please be as complete as possible.
Post a fully working batch, I can't assemble one myself from pieces.

Are you saying that Win98 cannot cope with the /R or ... ?

Top Expert 2007
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview



goto :EOF


goto :DONE



Thanks for your help.

Inspired by the possibilities I was thinking of the following, don't know if this is possible :


REM https://www.experts-exchange.com/Operating_Systems/MSDOS/Q_20938678.html#10724235

@echo off

del deleted.txt

if "%OS%"=="" goto DO98CODE

REM // Following only works under 2K / XP

echo Executed under Win2K/XP >> deleted.txt

for /R . %%a in (*.~?*, *.il?, *.obj) do echo del "%%a" >> deleted.txt
for /R . %%a in (*.~?*, *.il?, *.obj) do del "%%a"

goto :EOF

REM // Following does not include Sub-directories

echo Executed under Win9x >> deleted.txt

for %%a in (*.~?*, *.il?, *.obj) do echo del "%%a" >> deleted.txt
for %%a in (*.~?*, *.il?, *.obj) do del "%%a"

for %%a in (/DA) do echo call clean.bat %input



With this line I wanted to try to execute the batch per directory :
for %%a in (/DA) do echo call clean.bat %input

But  it doesn't look like it that this can work ?
Is it possible to use the for command with directories ?

Top Expert 2007

:EOF is a predefined label in NT/XP so you don't need to define it.

To answer your question yes.

for %%a in (%1\*.~?*, ... etc

%1 is the 1st parameter passed to the code.


But is it possible to use for with directories ?


for %%aa ( directory ) .....
Top Expert 2007

That's what the code I posted does. If you named your batch file test.bat you could do this:

test c:\temp

It would process the file types you have annotated:

c:\temp\*.~?*, c:\temp\*.il?, c:\temp\*.obj

You'd code the for statement like this:

for %%a in (%1\*.~?*, %1\*.il?, %1\*.obj) do echo del "%%a" >> deleted.txt


I'm not sure I understand or maybe you misunderstood my question.

because I am still not iterating through the directories ... right ?

the for command processes the files that meet the criteria between brakets () but the question is can I put something in the brakets that causes a directory name to be stored in %%a ?
so that the for command would store all directories in %%a and then the command that for executes could be something like call test.bat %%a
Top Expert 2007

Okay, I see now... This will do the trick on XP/NT part of the code:

for /R %1 %%a in (*.~??, *.~?, *.il?, *.obj) do echo del "%%a"

So when you execute test.bat like so:

test c:\temp

it would process all the files found in temp and all of it's sub-directories.


I understand but I was asking for Win9x ;-)
I gather it can't be done then ... using the for command to iterate through directories matching the criteria instead of files ?
Top Expert 2007

For the 9x part you could code:

dir %1\*.~?? /s/b >> __dell.lst
dir %1\*.~? /s/b >> __dell.lst
dir %1\*.il? /s/b >> __dell.lst
dir %1\*.obj /s/b >> __dell.lst
I know this one is already answered and accepted but I thought I would mention my free utility RDelete.  It will run under Windows 9x and up, and will allow you to delete wildcarded files from particular branches of your directory tree.  For instance you could remove *.obj files from the C:\MYFILES directory branch (and down) by using this command:

rdelete c:\myfiles *.obj

RDelete is available here:


Or just go to the main page here and then to Downloads in the left pane:



would I need to call the util for every file-type or does the tool accept multiple file-types on the command prompt ?
You'd simply run your BAT file with one line per wildcarded file type:

rdelete c:\myfiles *.obj
rdelete c:\ *.tmp
rdelete c:\windows\temp\*.x??


I expected a DOS util actually, but it's a windows app.
I got a bunch of windows including an error and so on ?

I ran this :

rdelete P:\Projects *.obj
rdelete P:\Projects *.~*
rdelete P:\Projects *.il?

When I did a quick check afterwards I noticed this file :
which I expected to be deleted.
The tilde may be what is throwing RDelete off - I think th tilde is not being passed through since it has special meaning in this language.  Did the rest of the wildcarded commands get rid of the files you expected?  I may take a look at the source code for RDelete and try to fix that tilde issue.  Perhaps you could use a different wildcard without a ~ to match those files?


Several of the other files were still intact as well, I didn't inspect closer.
E.g. *.obj files were left behind
The tilde is used on all backup files (all extensions), so they come in many flavours and the tilde is the only comonality in the filename.
I'm going to stay away from rdelete for now as I was a bit chocked by the errors and so on.  I used it immediately on my "good" project files and folders.  Don't want to risc losing anything !
Yes I'm sorry it's not working perfectly for you - but the problem certainly isn't it deleting too much.  If anything it's leaving something behind you intended to get rid of.  Is there any pattern to the ones it's missing?  Like a protection bit being set, or the file is set as hidden or system attribute?


> Like a protection bit being set, or the file is set as hidden or system attribute?

None of those I think.
In the mean time I cleaned everyting up via my Win2K system so I can't verify at the moment.
At a later date I will try again.
OK - I think part of the problem is the tilde (~).  I know it's a special meaning character in this language, and I never tested RDelete on tilde-named files when I was writing it.  I'll see if I can clean that behavior up, test it, and get back to you.
sneeuw - Sorry again for my utility being so out of date.  It had been so long since I had written it, I don't think I'd ever tested that code under 2K or XP.  I have reworked that code and tested with files with ~'s in the names and it's working fine now.  This is the new version for NT/2K/XP:


It works using the same syntax as before but should work fine.  Let me know if it doesn't work perfectly now.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.