I'm using the SoX sound editing command line tool. I'm trying开发者_如何学Python 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!
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
)
)
)
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.
精彩评论