在指定的文件夹中,如何递归删除除“.mkv”和“.nfo”文件之外的所有内容?

在指定的文件夹中,如何递归删除除“.mkv”和“.nfo”文件之外的所有内容?

我正在尝试找出批处理文件(Windows)的代码在指定的文件夹中以递归方式删除除具有特定扩展名的文件(即.mkv.nfo)之外的所有内容?

类似这样,但是针对上述情况。

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|music.video|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) } }

答案1

以下是众多快速实现此目的的方法之一:

@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
:End
popd

首先在测试目录中运行一些要保留的文件和要删除的文件,然后再在主目录中运行。如果您希望批处理文件请求每个文件的删除确认,请更改del. /s /qdel. /s /p

答案2

这是无需更改属性的另一种方法,查看输出并删除echo,如果可以的话:

@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!"
    endlocal
)

相关内容