Page 1 of 1

Delete in ALL Dirs

Posted: 13 Jul 2012 03:40
by drgt
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!

Re: Delete in ALL Dirs

Posted: 13 Jul 2012 03:48
by foxidrive
The DEL command can help you there.

Type this for help:

DEL /?

Re: Delete in ALL Dirs

Posted: 13 Jul 2012 03:51
by Ed Dyreen
'

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"

Delete in ALL Dirs

Posted: 13 Jul 2012 03:59
by drgt
Thanks.

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

Re: Delete in ALL Dirs

Posted: 13 Jul 2012 15:23
by Fawers
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"
)