Link to home
Start Free TrialLog in
Avatar of highwaterhead
highwaterhead

asked on

Delete files based on NOT meeting criteria

Is it possible to delete a set of files if they do not match a file type criteria?

I have a folder on the network which is meant to hold word docs.
Someone on the network has been dumping other random rubbish in there.

If the rubbish was always MP3 files, then I could do a "del h:\*.mp3"

What I really want is something along the lines of "del NOT h:\*.doc *.*"

Is this possible? I would probably put it into a batch file to run at regular intervals.

Thanks in advance,

Richard.
Avatar of Computron
Computron

DEL doesn't have exceptions available, however, why don't you create a quick backup folder and copy *.doc to that folder, then delete the original, then copy the backup folder back to the original.
Avatar of sirbounty
Sure, it's possible with a for loop...
remove the echo to 'initiate' the erase...


for %%a in (*.*) do (
set strFile=%%a
set strExt=%%~xa
if not %strExt%==.doc echo erase %strFile%
)
Avatar of highwaterhead

ASKER

Computron - Thought of that, but would prefer a more direct method.

sirbounty - Like the look of that, but could you explain how/where I put that into a batch file?

Thanks.
OK,

I have come up with this:

attrib -a *.doc /s       (removes archive attribute)
erase *.* /a:a /s /q   (deletes all files that HAVE the archive attribute set)
attrib +a *.doc /s      (replaces archive attribute - for NT backup)

This works as erase allows you to specify by attribute.

Seems a bit fiddly.  Anyone have a better idea?

Thanks in advance.

Richard.
This looks more strange :-), but should be slower because of the repeated calls to del:
(if not in batch mode, replace %% by %)

dir /s/b | for /F "delims=" %%f in ('findstr /v .doc') do @del %%f
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial