Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to figure out the code for a batch-file (Windows) that In a specified folder recursively deletes everything except files with a certain extension (i.e. .mkv and .nfo)?

Something like this, but for the above.

def deleteRootFolder = tryQuietly{ root.toBoolean() }

 * Delete orphaned "clutter" files like nfo, jpg, etc and sample files
def isClutter(f) {
    def exts    = tryQuietly{ exts }            ?: /jpg|jpeg|png|gif|nfo|xml|htm|html|log|srt|sub|idx|md5|sfv|txt|rtf|url|db|dna|log/
    def terms   = tryQuietly{ terms }           ?: /sample|trailer|extras|deleted.scenes||scrapbook/
    def maxsize = tryQuietly{ maxsize as Long } ?: 100 * 1024 * 1024

    // path contains blacklisted terms or extension is blacklisted
    return (f.extension ==~ "(?i)($exts)" || f.path =~ "(?i)\\b($terms)\\b") && f.length() < maxsize

def clean(f) {
    println "Delete $f"

    // do a dry run via --action test
    if (_args.action == 'test') {
        return false

    return f.isDirectory() ? f.deleteDir() : f.delete()

// delete clutter files in orphaned media folders
args.getFiles{ isClutter(it) && !it.dir.hasFile{ (it.isVideo() || it.isAudio()) && !isClutter(it) }}.each { clean(it) }

// delete empty folders but exclude given args
args.getFolders().sort().reverse().each { if (it.listFiles().length == 0) { if (deleteRootFolder || !args.contains(it)) clean(it) } }
share|improve this question
What OS are you using? – Sekhemty May 29 '13 at 22:19
I am using Windows. – David Custer May 30 '13 at 0:10
This is not batch, what are you looking for? – Endoro May 30 '13 at 8:03
We'll I use SABnzbd that has a blacklist function for extensions(otherwise know as a cleanup list), but no whitelist function. Unfortunately the list of what you don't want(blacklist) is unlimited, and the list of what you do want(whitelist) is ofted times only two(or so) depending on the category. So I'm looking for some batch-code that will "In a specified folder recursively delete everything except for a list of extensions ie .mkv .nfo" – David Custer May 30 '13 at 12:20
@DavidCuster I could suggest you a simple Bash script for Linux, but I'm not that expert on CLI commands for Windows. – Sekhemty May 30 '13 at 17:26
up vote 3 down vote accepted

Here's one quick way of doing this among many:

@echo off
set extlist=mkv nfo
set rootfolder="C:\Video Folder"
pushd %rootfolder%
if not ["%cd%"]==[%rootfolder%] echo Aborting as unable to change to %rootfolder% && goto End
attrib /s /d -r
for %%a in (%extlist%) do attrib /s *.%%a +r
del. /s /q

Run on a test dir first with a few files that you want to retain and others you want to delete, before unleashing on your main dir. If you want the batch file to request delete confirmation for every file, change del. /s /q to del. /s /p.

share|improve this answer
If you want to get rid of empty dirs as well, then before the :End line insert the following (rd doesn't normally delete non-empty dirs): for /r %%a in (.) do rd "%%~a". – Karan May 30 '13 at 17:55
YOU ARE AMAZING! Karan is my hero, he has saved me in other posts as well. – David Custer May 30 '13 at 22:23
I added the deletion of non-empty dirs per your tutorial. PERFECT! – David Custer May 31 '13 at 16:54

this is an other way without the need to change attributes, look at the output & remove echo, if OK:

@echo off &setlocal
set "extlist=.mkv|.nfo|"
for /r "c:\video" %%i in (*) do (
    set "fname=%%~i"
    setlocal enabledelayedexpansion
    if "!extlist:%%~xi|=!"=="%extlist%" echo del "!fname!"
share|improve this answer
;) Your the best! Are you saying this is a better way? In your opinion? – David Custer May 31 '13 at 16:53
If you change the attributes to read only you must set it back after deleting the other files (otherwise all MKV/NFO will stay read only). And you can't use the attributes for other things because the previous state is not restored. – Endoro May 31 '13 at 17:04
It doesn't actually delete anything. Hmmmm. – David Custer May 31 '13 at 21:10
Yes, you must remove the word echo before....... – Endoro May 31 '13 at 22:18
LOLOLOL OOps -.- – David Custer Jun 1 '13 at 3:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .