|
-
February 14th, 2003, 09:06 PM
#1
Selective deletion
Here's another seemingly simple question that's eluding me.
I know that if I want to delete all the text files in a directory
I can just type:
del *.txt
But what if I want to delete everything except the text
files? Or to make matters more complicated, I want to delete
everything except files that end in .txt or .bat?
Thanks all, Dexahol
P.S. I should add, this is on a pure DOS 6.22 machine, so there's no multiple selection capabilty of File Manager.
Last edited by Dexahol; February 14th, 2003 at 09:09 PM.
-
February 15th, 2003, 04:52 AM
#2
Hey,
there are no switches in the 'del' command to do what you want.
One option though would be to move your *.txt and *.bat files to another location.
Then del *.* the contents of your directory.
Then move back the *.txt and *.bat files from where you had put them.
Also the 'move' command would be better than the 'copy' command here.
Although it's been over a year since I even looked at it, but with QBasic a tiny program could be written that set into the 'Path' statement could run in any directory, and delete or omit only those file types you want.
There are some good tutorials out there, and it's more than enough to learn how to write a little proggy for yourself. But you got to spend a bit of time on it, and keep using it or you lose it, like I did.
You could check the Simtel Network for DOS apps for this also.
Others will have more ideas.
Dave
*** Help others less fortunate.
JESUS IS LORD !
-
February 15th, 2003, 10:14 AM
#3
Hi David, you actually got me pointed in the right direction.
I'm trying this batch file:
::test.bat
@echo off
MD deltemp
for %%x in (%1 %2 %3 %4 %5 %6 %7 %8 %9) do move *.%%x deltemp
del *.*
copy deltemp\*.*
deltree /Y deltemp
If I want to delete everything except .txt and .bat files, I type:
test txt bat
This almost works, but not quite. The del *.* line prompts for confirmation and the copy deltemp\*.* works from the command line, but not in a batch!
I've obviously got the syntax for moving a file back one step in the directory wrong, but I can't seem to get this right.
(Thanks again to Vernon for showing me how to use the for . . . in command!)
Dex
EDIT: Ok, this is a bit awkward, but it solves the "confirmation on delete" problem:
::test.bat
@echo off
md deltemp1
md deltemp2
for %%x in (%1 %2 %3 %4 %5 %6 %7 %8 %9) do move *.%%x deltemp1
move *.* deltemp2
copy deltemp1\*.*
deltree /y deltemp1
deltree /y deltemp2
But I still have the problem of the copy deltemp1\*.* line, works from the command line but not in a batch!!!
EDIT 2:
Ok, this works perfectly:
::test.bat
@echo off
md c:\temp\deltemp1
md c:\temp\deltemp2
for %%x in (%1 %2 %3 %4 %5 %6 %7 %8 %9) do move *.%%x c:\temp\deltemp1
move *.* c:\temp\deltemp2
copy c:\temp\deltemp1\*.*
deltree /y c:\temp\deltemp1
deltree /y c:\temp\deltemp2
I just hate having to use explicit paths, and it just seems cumbersome and awkward. It does, however, do the job.
If I want to delete everything from the current directory except .txt and .bat files, I can just type:
test txt bat
Dex
LAST EDIT: One little bauble: the move command protests the existence of sub-directories in the directory being worked on, but it doesn't actually prevent operation. Just doesn't have a "clean" look to it.
Last edited by Dexahol; February 15th, 2003 at 11:31 AM.
-
February 15th, 2003, 12:19 PM
#4
Ok...here's the final version using Laurence Soucy's recommendation of adding built-in help to your batch files:
:: SDEL.BAT
@echo off
if "%1"=="" goto Help
if "%1"=="/k" goto Keep
if "%1"=="/K" goto Keep
goto Sdel
:Help
cls
echo.
echo.
echo ***** SDEL.BAT (Selective Delete) - written by Dexahol, Feb. 15, 2003 *****
echo.
echo Usage: sdel [/k] ext1 ext2 ... ext8
echo.
echo Example 1: Delete all files with extension of .txt and .bat
echo from current directory.
echo.
echo sdel txt bat
echo.
echo Example 2: Keep everything with extension of .txt and .bat,
echo deleting everything else.
echo.
echo sdel /k txt bat
echo.
echo.
echo help: sdel (with no parameters)
echo.
echo.
goto End
:Sdel
for %%x in (%1 %2 %3 %4 %5 %6 %7 %8 %9) do del *.%%x
goto End
:Keep
md c:\temp\deltemp1
md c:\temp\deltemp2
for %%x in (%2 %3 %4 %5 %6 %7 %8 %9) do move *.%%x c:\temp\deltemp2>nul
move *.* c:\temp\deltemp1>nul
copy c:\temp\deltemp2\*.*>nul
deltree /y c:\temp\deltemp1>nul
deltree /y c:\temp\deltemp2>nul
:End
Thanks again Dave. You actually guided me towards finding this solution.
Dexahol
Last edited by Dexahol; February 15th, 2003 at 12:31 PM.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|