sox trim with batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
m_vicki81@yahoo.in
Posts: 1
Joined: 26 Nov 2016 04:52

sox trim with batch

#1 Post by m_vicki81@yahoo.in » 26 Nov 2016 05:00

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.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: sox trim with batch

#2 Post by dbenham » 26 Nov 2016 09:58

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.

So

Code: Select all

sox "%%A" "trimmed\%%~nxA" trim 15
becomes

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: sox trim with batch

#3 Post by Squashman » 26 Nov 2016 10:56


dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: sox trim with batch

#4 Post by dbenham » 26 Nov 2016 15:43

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

Post Reply