Dear Members,
I wish to trim first 15 seconds from multiple audio files, kept in folder 'multiplefiles' .
for test purposes, i have taken up 3 audio files, x.wav, y.wav, and z.wav
below is the code
@echo off
cd E:\trim\multiplefiles
mkdir trimmed
FOR /F %%A IN ("*.wav") DO sox "%%A" "trimmed\%%~nxA" trim 15
The above code, however, merges the 3 files into folder 'trimmed' , names it as 'x.wav' and only trims 14 seconds from the sum of the merged audio. i do not want this. I want individual files trimmed 15 seconds and sent to 'trimmed' folder.
Please suggest where I am going wrong.
sox trim with batch
Moderator: DosItHelp
Re: sox trim with batch
The FOR command is perhaps the most complicated command in batch, with many forms that do different things. You are using the wrong FOR form for your task.
You are using FOR /F with quotes around your IN clause, which simply parses the string value "*.wav". FOR /F will break the string into tokens that are delimited by space or tab. Your string does not contain any delimiters, so %%A simply contains the original string "*.wav".
When %%~nxA is applied against "*.wav", Windows searches the current folder for matching files and returns the first matching file found - there is no iteration.
Sobecomes
You should be able to see why you are getting your result.
You want to iterate all files that match "*.wav", so you want a simple FOR loop instead, without the /F option:
This form will iterate all matching files, so you will get 3 iterations with %%A values of x.wav, y.wav, and z.wav.
Your %%A value will never contain path information, so %%~nxA isn't actually needed in this case. But it doesn't hurt.
Note how my post has easy to read code blocks. When you post code on this site, you should place the code within code blocks. The simplest option for a few lines of code is to write the code as you did, highlight the code, and press the "Code" button at the top of the editing area. That action places a [code] tag before your code, and [/code] afterward. The other option is to manually type those tags yourself.
Dave Benham
You are using FOR /F with quotes around your IN clause, which simply parses the string value "*.wav". FOR /F will break the string into tokens that are delimited by space or tab. Your string does not contain any delimiters, so %%A simply contains the original string "*.wav".
When %%~nxA is applied against "*.wav", Windows searches the current folder for matching files and returns the first matching file found - there is no iteration.
So
Code: Select all
sox "%%A" "trimmed\%%~nxA" trim 15
Code: Select all
sox "*.wav" "trimmed\x.wav" trim 15
You should be able to see why you are getting your result.
You want to iterate all files that match "*.wav", so you want a simple FOR loop instead, without the /F option:
Code: Select all
@echo off
cd E:\trim\multiplefiles
mkdir trimmed
FOR %%A IN ("*.wav") DO sox "%%A" "trimmed\%%~nxA" trim 15
This form will iterate all matching files, so you will get 3 iterations with %%A values of x.wav, y.wav, and z.wav.
Your %%A value will never contain path information, so %%~nxA isn't actually needed in this case. But it doesn't hurt.
Note how my post has easy to read code blocks. When you post code on this site, you should place the code within code blocks. The simplest option for a few lines of code is to write the code as you did, highlight the code, and press the "Code" button at the top of the editing area. That action places a [code] tag before your code, and [/code] afterward. The other option is to manually type those tags yourself.
Dave Benham
Re: sox trim with batch
That SO post is a different question, obviously asked after she figured out the problem she had with this question. I can't tell if she figured out the answer to this question on her own, or used my answer.
Dave Benham
Dave Benham