批处理文件:拖放文件夹和文件(某些文件类型和递归)

时间:2018-04-23 17:06:25

标签: batch-file drag-and-drop

我正在尝试将文件夹和文件拖到批处理文件中,并且如果目录/子目录包含这些文件类型,则希望仅处理文件类型.flac .mp3 .acc

递归处理也是我追求的目标。

Dir树看起来像:

Z:\Music\
|   03 - I Love You (Booka Shade Remix).mp3
|   02 - Towers.mp3
|
+---Woodkid - 2013 - I Love You [FLAC]
|       cover.jpg
|       02 - Towers.flac
|       01 - I Love You.flac
|       03 - I Love You (Booka Shade Remix).flac
|
+---No FLAC, MP3 or ACC folder
|       cover.jpg
|
\---Woodkid - 2013 - I Love You [MP3]
|       cover.jpg
|       01 - I Love You.mp3
|       02 - Towers.mp3
|       03 - I Love You (Booka Shade Remix).mp3
|
\---Woodkid - 2013 - I Love You [ACC]
|        cover.jpg
|        01 - I Love You.acc
|        02 - Towers.acc
|        03 - I Love You (Booka Shade Remix).acc

我想按照数字顺序处理上面的目录树。

Processing Directory: Z:\Music\
   Processing File: 1 - 02 - Towers.mp3
   Processing File: 1 - 03 - I Love You (Booka Shade Remix).mp3
Processing Directory: Woodkid - 2013 - I Love You [FLAC]
   Processing File: 1 - 01 - I Love You.flac
   Processing File: 1 - 02 - Towers.flac
   Processing File: 1 - 03 - I Love You (Booka Shade Remix).flac
Processing Directory: Woodkid - 2013 - I Love You [MP3]
   Processing File: 1 - 01 - I Love You.mp3
   Processing File: 1 - 02 - Towers.mp3
   Processing File: 1 - 03 - I Love You (Booka Shade Remix).mp3
Processing Directory: Woodkid - 2013 - I Love You [ACC]
   Processing File: 1 - 01 - I Love You.acc
   Processing File: 1 - 02 - Towers.acc
   Processing File: 1 - 03 - I Love You (Booka Shade Remix).acc

我试图在this post的帮助下使用它,但这仅适用于单个文件类型。

编辑:我的尝试。代码中的注释和问题:

@echo off
FOR %%I IN (%*) DO (
    ECHO.%%~aI | FIND "d" >NUL
    IF ERRORLEVEL 1 (
        :: Processing Dropped Files
        :: Only need folder (%%~dpI) to repeat once, this repeats for every dropped file.
        echo %%~dpI
        :: Would be nice if I could set a variable for file types at beginning of script
        for /f "tokens=*" %%I in ('DIR /B/OGDN "%%~nI.flac" "%%~nI.mp3" "%%~nI.acc" 2^>NUL') DO (
            echo   Processing File: %%I
            )
        ) ELSE (
        :: Processing Dropped Folders
        :: Need the directory echod here (just once).
        :: Don't know how to process flac mp3 or acc files within the dropped folder recursively.
        :: Be nice if I could set a variable for file types here too.
        )
        )
@pause

1 个答案:

答案 0 :(得分:0)

如果只是为了几个扩展,这应该做。如果您有许多扩展,那么构建列表并运行单个if语句会更容易。

@echo off
for /f "tokens=*" %%i in ('dir /b /a-d /s %*') do (
  if "%%~xi"==".flac" echo "%%i"
  if "%%~xi"==".mp3" echo "%%i"
  if "%%~xi"==".acc" echo "%%i"
 )
pause

显然你会用你的实际命令取代echo。

修改

这不会很快,因为它有3个循环,但应该可以工作。

@echo off
set "list=.flac .mp3 .aac"
for %%a in (%*) do (
if exist .\nul @echo Processing Directory: %%a
 for /f "tokens=*" %%i in ('dir /b /a-d /s %*') do (
   for %%j in (%list%) do (
    if "%%~xi"=="%%j" echo File Type is %%j - Processing file: "%%i"
   )
  )
 )
pause

如果您只想在Processing file:部分显示文件名,只需将%%i更改为%%~nxi