Solved

Batch script to delete certain files in tree

Posted on 2004-03-31
25
3,049 Views
Last Modified: 2007-12-19
Hi,

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
:LOOPSTART
    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    
:DONE


del __del.lst
del __dell.lst
del __dell.bat
0
Comment
Question by:sneeuw
  • 12
  • 7
  • 6
25 Comments
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
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,
Steve
0
 

Author Comment

by:sneeuw
Comment Utility
Thanks,

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

Peter
0
 

Author Comment

by:sneeuw
Comment Utility
echo seems to display each situation twice ?
That's not a problem of course ... I was just wondering why that is ?
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
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:

temp.~aa
temp.~a

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...
0
 

Author Comment

by:sneeuw
Comment Utility
Does
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 ... ?

Thanks,
P
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 250 total points
Comment Utility
You are correct in both cases.

1) for /R c:\ does scan the C: drive starting at the root.

2) Win98 does not understand the /R option for the for statement.

Here is this complete batch file:

@echo off

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

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

goto :EOF

:DO98CODE

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
:LOOPSTART
    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    
:DONE

del __del.lst
del __dell.lst
del __dell.bat
0
 

Author Comment

by:sneeuw
Comment Utility
Should

goto :EOF

be

goto :DONE

?
0
 

Author Comment

by:sneeuw
Comment Utility
Thanks for your help.

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

<clean.bat>


REM http://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
:DO98CODE

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

:EOF


</clean.bat>

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 ?

0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
: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.
0
 

Author Comment

by:sneeuw
Comment Utility
But is it possible to use for with directories ?

Like

for %%aa ( directory ) .....
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
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
0
 

Author Comment

by:sneeuw
Comment Utility
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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
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.
0
 

Author Comment

by:sneeuw
Comment Utility
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 ?
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
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
0
 
LVL 1

Expert Comment

by:pauldoherty
Comment Utility
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:

http://powerusers.info/modules.php?op=modload&name=Downloads&file=index&req=viewdownload&cid=1&min=10&orderby=titleA&show=10

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

http://powerusers.info
0
 

Author Comment

by:sneeuw
Comment Utility
cool
would I need to call the util for every file-type or does the tool accept multiple file-types on the command prompt ?
0
 
LVL 1

Expert Comment

by:pauldoherty
Comment Utility
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??
etc...
0
 

Author Comment

by:sneeuw
Comment Utility
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 :
file.~dsk
which I expected to be deleted.
0
 
LVL 1

Expert Comment

by:pauldoherty
Comment Utility
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?
0
 

Author Comment

by:sneeuw
Comment Utility
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 !
0
 
LVL 1

Expert Comment

by:pauldoherty
Comment Utility
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?
0
 

Author Comment

by:sneeuw
Comment Utility
> 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.
0
 
LVL 1

Expert Comment

by:pauldoherty
Comment Utility
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.
0
 
LVL 1

Expert Comment

by:pauldoherty
Comment Utility
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:

http://powerusers.info/downloads/RDeleteXP.zip

It works using the same syntax as before but should work fine.  Let me know if it doesn't work perfectly now.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now