Delete in ALL Dirs

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Delete in ALL Dirs

#1 Post by drgt » 13 Jul 2012 03:40

How do I make a batch to delete all files in every possible directory (hidden included) that have this certain extension (.ext for example)?

Thanks!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Delete in ALL Dirs

#2 Post by foxidrive » 13 Jul 2012 03:48

The DEL command can help you there.

Type this for help:

DEL /?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Delete in ALL Dirs

#3 Post by Ed Dyreen » 13 Jul 2012 03:51

'

Code: Select all

@echo off &setlocal enableDelayedExpansion

for /r %%? in (

       "*.ext"

) do (
       echo(
       echo.attrib -r -a -s -h "%%~?" &&echo.del /f /q "%%~?" ||echo.[error:!errorLevel!] attrib -r -a -s -h '%%~?'
)

pause
exit
or

Code: Select all

attrib -r -a -s -h /S "*.ext"
del /f /s /q "*.ext"

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Delete in ALL Dirs

#4 Post by drgt » 13 Jul 2012 03:59

Thanks.

What about delete all files that DO NOT match a list of extensions?

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Delete in ALL Dirs

#5 Post by Fawers » 13 Jul 2012 15:23

drgt wrote:What about delete all files that DO NOT match a list of extensions?

Code: Select all

for /f "delims=" %%a in ('dir /b /s /a') do (
  if /i "%%~xa" NEQ ".ext" del /a /q /f "%%a"
)


Edit
Sorry, I misread your last line. You want a list of extensions.

Code: Select all

@echo off
::set your extensions.
::xtension examples: ext, xtn
set "extensions=.ext .xtn"
for /f "delims=" %%a in ('dir /b /s /a') do (
  set "xdel="
  for %%e in (%extensions%) do (
    if /i "%%~xa" == "%%e" set "xdel=0"
  )
  if not defined xdel del /a /q /f "%%a"
)

Post Reply