Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Delete all files *except*

249 views
Skip to first unread message

JJ

unread,
Mar 28, 2012, 3:09:28 PM3/28/12
to
Is there a simple way to delete all files of a given extension, except
for some file(s) designated by name? Something like

delete *.log /e:error.log

tonysathre

unread,
Mar 28, 2012, 5:48:31 PM3/28/12
to
I'm sure there is a better way but...

@echo off & setlocal enabledelayedexpansion
for /f "tokens=*" %%i in (
'dir /b *.log'
) do (
set _=%%i
if not "!_!"=="error.log" (
echo del !_!
)
)

tonysathre

unread,
Mar 28, 2012, 5:57:23 PM3/28/12
to
This is probably better, you can add other exclusions in the exclude
variable, separated by a space.

@echo off
set "exclude=error.log other.log"
for /f "tokens=*" %%i in (
'dir /b *.log ^| findstr /l /v "%exclude%"'
) do (
del %%i
)

Tony

Robert Roland

unread,
Apr 2, 2012, 5:36:30 PM4/2/12
to
On Wed, 28 Mar 2012 12:09:28 -0700 (PDT), JJ <jim.m...@gmail.com>
wrote:

>Is there a simple way to delete all files of a given extension, except
>for some file(s) designated by name? Something like
>
>delete *.log /e:error.log

attrib +h error.log
del *.*
attrib -h error.log

It's not the neatest solution, but it might be acceptable in your
application.

The old 4Dos/Ndos command interpreters have an "except" command. It
works just like this.
--
RoRo

Todd Vargo

unread,
Apr 2, 2012, 10:20:28 PM4/2/12
to
ehh, del *.log

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

foxidrive

unread,
Apr 2, 2012, 10:23:24 PM4/2/12
to
On 3/04/2012 12:20, Todd Vargo wrote:
> On 4/2/2012 5:36 PM, Robert Roland wrote:
>> On Wed, 28 Mar 2012 12:09:28 -0700 (PDT), JJ<jim.m...@gmail.com>
>> wrote:
>>
>>> Is there a simple way to delete all files of a given extension, except
>>> for some file(s) designated by name? Something like
>>>
>>> delete *.log /e:error.log
>>
>> attrib +h error.log
>> del *.*
>> attrib -h error.log
>>
>> It's not the neatest solution, but it might be acceptable in your
>> application.
>>
>> The old 4Dos/Ndos command interpreters have an "except" command. It
>> works just like this.
>
> ehh, del *.log
>

c:\>ehh
bad command or filename


;)


--
Mic
0 new messages