I have a windows batch script that runs a SOX command on various files, but I don't know how to get the batch to run on files in a relative path to that of the SOX executable. Currently all the files are in the root and it outputs to /combined.

The Batch Script:

cd %~dp0
mkdir combined
FOR %%A IN (*.mp3) DO sox static.mp3 %%A "combined/%%~nxA"

I want the script to run the sox command on files in the directory "audiotracks" and output it to the directory "combined". To give you an idea, this would be the desired folder structure:


Is this possible, or is there a better method of doing this? Any help would be greatly appreciated. Thanks a lot!


Try switching first to the diretory. The alter the save path. (Two consecutive dots mean the parent folder.)

cd %~dp0
mkdir combined
cd audiotracks
FOR %%A IN (*.mp3) DO sox static.mp3 %%A "../combined/%%~nxA"
cd ..
  • Didn't work. It's looking for the sox.exe in the audiotracks folder. Is there a way to point it to the sox.exe in the root folder? – Matteo Cuellar Vega Oct 17 '12 at 17:14
  • Just moved the sox.exe and libraries into the audiotracks folder and it works, but how can I make it work with sox in the root? – Matteo Cuellar Vega Oct 17 '12 at 17:22
  • I guess I can have the batch script move all mp3s contained in a new folder (E.g. "/input") to "/audiotracks" then output them to "combined" and delete the mp3s from "/audiotracks". I'm just trying to make an easier and cleaner "one-click" solution for the user. – Matteo Cuellar Vega Oct 17 '12 at 17:28
  • Just tried with the move command and it works well. Thanks a lot for your help. Much appreciated! – Matteo Cuellar Vega Oct 17 '12 at 17:39

As with most programming tasks there's more than one way to do what you want. Your current implementation isn't too far off from a working solution. The real problem is that you are thinking about the problem from the wrong angle. Instead of trying to process the files in the relative audiotracks directory you should think about outputting the results to the relative combined directory. Here's how your script could be adjusted:

cd %~dp0
mkdir combined
cd audiotracks
FOR %%A IN (*.mp3) DO sox ..\static.mp3 "%%A" "..\combined\%%~nxA"

An alternate, more flexible implementation might be to use the /f argument of the for command. (Run for /? for more details.) Doing so would allow you to use relative paths for both the input and output.

cd %~dp0
mkdir combined
FOR /f "tokens=*" %%A IN ('dir /b .\audiotracks\*.mp3') DO sox static.mp3 "%%A" ".\combined\%%~nxA"

Let's break it down:

  • /f means process the output of the command in single-quotes (inside the parens) one token at a time.
  • "tokens=*" means parse an entire line of output as a single token. (Basically, it means whitespace is not considered a delimiter.)
  • 'dir /b .\audiotracks\*.mp3' means list all *.mp3 files in the audiotracks directory without date, size or other metadata.
  • Unfortunately that didn't work. It returns: sox FAIL formats: can't open input file `Audio1.mp3': No such file or directory. I guess it tries to get the file from the root. – Matteo Cuellar Vega Oct 17 '12 at 17:11
  • The other solution worked for me, but thanks for your help. I keep learning with every help I get! – Matteo Cuellar Vega Oct 17 '12 at 17:41
  • What is the output of dir /b .\audiotracks\*.mp3? If the output only gives you the file names and not the full path names then adjust the script like this: FOR /f "tokens=*" %%A IN ('dir /b .\audiotracks\*.mp3') DO sox static.mp3 ".\audiotracks\%%A" ".\combined\%%~nxA" – HairOfTheDog Oct 18 '12 at 0:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.