[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3078
  • Last Modified:

Batch script to delete certain files in tree

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
sneeuw
Asked:
sneeuw
  • 12
  • 7
  • 6
1 Solution
 
SteveGTRCommented:
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
 
sneeuwAuthor Commented:
Thanks,

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

Peter
0
 
sneeuwAuthor Commented:
echo seems to display each situation twice ?
That's not a problem of course ... I was just wondering why that is ?
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
SteveGTRCommented:
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
 
sneeuwAuthor Commented:
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
 
SteveGTRCommented:
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
 
sneeuwAuthor Commented:
Should

goto :EOF

be

goto :DONE

?
0
 
sneeuwAuthor Commented:
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
 
SteveGTRCommented:
: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
 
sneeuwAuthor Commented:
But is it possible to use for with directories ?

Like

for %%aa ( directory ) .....
0
 
SteveGTRCommented:
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
 
sneeuwAuthor Commented:
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
 
SteveGTRCommented:
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
 
sneeuwAuthor Commented:
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
 
SteveGTRCommented:
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
 
pauldohertyCommented:
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
 
sneeuwAuthor Commented:
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
 
pauldohertyCommented:
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
 
sneeuwAuthor Commented:
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
 
pauldohertyCommented:
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
 
sneeuwAuthor Commented:
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
 
pauldohertyCommented:
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
 
sneeuwAuthor Commented:
> 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
 
pauldohertyCommented:
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
 
pauldohertyCommented:
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

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

  • 12
  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now