<--- Turn the page
   
(contents page)
   
Turn the page ---> Batch Files/DOSDelete all files with a specified extention |
<--- Turn the page
   
(contents page)
   
Turn the page ---> Page 8
I need to delete all files with a certain extension off my hard disk; including those located in the subdirectories.
- a DOS user
Well, let us use the recursive part of the DIR command and the FIND command.
rem delete all files with the given specs
rem in this and all dir's under it
rem By Forever Young Software
rem http://www.zekes.com/~blunt
rem RECDEL.BAT
rem to use: RECDEL *.TMP
@echo off
dir %1 /s /-p | find "Directory of " > del.$$$
echo 0 ON ERROR GOTO 1>d$$$.bas
echo file$ = "\%1">>d$$$.bas
echo CLS>>d$$$.bas
echo OPEN "del.$$$" FOR INPUT AS #1>>d$$$.bas
echo WHILE NOT EOF(1)>>d$$$.bas
echo LINE INPUT #1, A$>>d$$$.bas
echo A$ = RIGHT$(A$, LEN(A$) - LEN("Directory of "))>>d$$$.bas
echo A$ = A$ + file$>>d$$$.bas
echo PRINT "Deleting: "; A$>>d$$$.bas
echo KILL A$>>d$$$.bas
echo WEND>>d$$$.bas
echo CLOSE>>d$$$.bas
echo SYSTEM>>d$$$.bas
echo 1 RESUME NEXT>>d$$$.bas
call qbasic /run d$$$.bas
del del.$$$
del d$$$.bas
We first pipe the recursive directory listing to FIND and then redirect it to a temporary file.
dir %1 /s /-p | find "Directory of " > del.$$$
Now we can create a small .bas program and then call Qbasic to run it.
Notice the ON ERROR GOTO line in the basic source. The del.$$$ file we create contains all directories under the current directory, even if that directory doesn't contain a file
we specify.
This way, when the KILL command returns an error of File not Found, it won't stop, but continue with the deletion of the files we specify.
Once we create the basic source and send it to another temp file, we can call it.
call qbasic /run d$$$.bas
Depending on the speed of your harddrive and CPU, you should see a short flash of blue screen. This
is just the QBasic editor. Once it hits the SYSTEM command, QBasic returns to DOS.
A few notes: Notice that we had to put a line number (0) on the ON ERROR... line in the
basic source. If we don't, DOS thinks we are saying ECHO ON. Also, be careful with this
batch file, you could delete all files if you are not careful.
Now let us delete are temp files.
To use the batch file, name it something like recdel.bat and then at the DOS prompt:
RECDEL *.TMP
That's it.
¥