Batch Processing of Multiple Files in Multiple Folders

那年仲夏 提交于 2019-12-13 21:23:18

问题


I'm using the SoX sound editing command line tool. I'm trying to create a batch file that will allow me to drag multiple folders onto it and have the file process all the mp3 files within the folders, then output them into the converted folder, arranged into folders named the same as the ones originally dragged on. I currently have:

cd %~dp0
mkdir converted
FOR %%A IN (%*) DO sox %%A -c 1 "converted/%%~nxA" mixer -1
pause

This allows me to drag files on, but not whole folders. All I really need to do is take it up a level. As you may have guessed, I have very little batch processing knowledge so an explanation would be greatly beneficial.

Thank you for your time!


回答1:


You need to check if the passed name is a file, a directory or nothing. You might try this code, and adapt it to your needs

FOR %%a IN (%*) DO (
  IF EXIST %%~sa\NUL  (
   echo %%~fa is a directory
 ) else (
   IF EXIST %%a (
     echo %%~fa is a file
   ) else (
     echo %%~a does not exist
   ) 
 )
)



回答2:


I managed to adapt this file to use nested for loops to accomplish what I wanted:

cd %~dp0
mkdir converted
FOR /d %%A IN (*) DO (
    IF EXIST %%A/*.mp3 (
        cd converted
        mkdir %%~nxA
        cd ..
    )   
    cd %%A
    For %%B IN (*.mp3) DO (
        cd ..
        sox "%%A\%%~nxB" -c 1 "converted/%%~nxA/%%~nxB" mixer -1
        cd %%A
    )
    cd ..
)
pause

The code now searches each directory in the folder with the batch file, then enters the directories and processes the files within.



来源:https://stackoverflow.com/questions/6012650/batch-processing-of-multiple-files-in-multiple-folders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!