Fred's Deep Blush (Deleting TMP Files) Pt2
(Continued from above item; use all the following as EXAMPLES that may have to be tweaked to work properly on your specific system.)
Fred, Rather than having to write an EXE file, you could simply use the FOR command built into XP. Two ways you could do this:
1. Use the /D flag, which tells FOR that the wildcards refer to directories:
FOR /D %d IN (*.tmp) DO RD /Q /S %d
2. Using the file created by DIR /B, you could use the /F option:
FOR /F %d IN (DELME.TXT) DO RD /Q /S %d
(This would work, as when the FOR command tokenizes each line, there is only one token in the file per line.) --- Steven FoustHi Fred, For the user who wanted to delete all *.tmp directories -- how about this. No need for any external tools:
C:
cd \
for /F "delims=" %%i in ('dir /A:D /B *.tmp') do @rd /S /Q "%%i"
All the best, RobinFred: Just got the 2006-4-13 Plus Edition with your lead article "Free Tool & Demo From Fred" regarding removing .tmp folders from the root of a C: drive. I think there is a simple one line DOS command which will do the trick. Try this:
FOR /d %a IN (C:\*.tmp) DO rd %a
The FOR command loops thru all the values inside the parentheses one at a time and executes the command following the DO. Because the rd command is executed for a single folder at a time it gets around the limitation on wildcards. The /d option after the FOR tells the FOR command to process directories. DOS Command Extensions need to be enabled for this to run, but I think they are enabled by default in Win XP. Typing FOR /? at a command line will display all the options that FOR supports. As a veteran programmer (30+ years) I understand the urge to write your own solution to a problem when the simple one-line answer is elusive. I've done it many times. You'll probably have 100 versions of this solution from all the old DOS hackers out there. Thanks for the great newsletter. ---Frank TanzilloFred: Thanks as ever for the great newsletter! The Plus is definitely worth springing for! In regards to "1) Free Tool & Demo From Fred", here's a quick way to accomplish the folder deletions from the command line without having to use external programs in Windows XP:
for /d %i in (*.tmp) do rd /s /q "%i"
This searches only for directories named *.tmp and for each one executes the "rd" command. So your sample batch file would become:
c:
cd \
for /d %%i in (*.tmp) do rd /s /q "%%i"
Note that the single percent sign has to be doubled when used in a batch
file. The FOR command has become extremely useful under XP, even to the extent
that it can do simple text file parsing and variable substitution. I use it at work to scan program listings and extract certain data structure values for use in automatically constructing INF files. Anyway, just thought this might be interesting. Keep up the excellent work! *joe*Fred: There's a much cleaner way to delete directories ending in .tmp using a batch file - and it's only one line!
FOR /D %%F IN (*.TMP) DO RD /S /Q %%F
Fire that batch file off in the directory that has the problem directories and watch them go away.... If you try this from the command line, replace the two %%'s with just single %'s - ie
FOR /D %F IN (*.TMP) DO RD /S /Q %F
I find that the FOR command is one of the most powerful yet underused commands in the MS world. Love your newsletter, I've been a long time subscriber and still learn something new in every letter. ---Jim Ruby
Hi Fred, I know you are going to get a lot of comments and suggestions about this but I could not resist suggesting a BAT only solution because not everyone can make EXE programs.
1. Make one TXT file called delme.txt which includes only "RD /S /Q ".
2. Modify your delme.bat to be recursive and modify the DIR command:
c:
cd \
:start
if exist delme.bat del delme.bat
if not exist *.tmp goto :notmp
copy delme.txt delme.bat
dir /b *.tmp >> delme.bat
call delme.bat
goto :start
:notmp
cls
@echo No tmp folders/files found; aborting.
@pause
:end
