0

I'm trying to run a for loop in cmd that looks like this:

for %f in (*.mp4) do (
    set outfile=%f:mp4=mp3%
    ffmpeg -i %f -vn -ar 44100 -ac 1 -b:a 32k -f mp3 %outfile%
)

The script is supposed to grab all *.mp4 files, run the ffmpeg command on them, then name the resultant file identical as the mp4 with only the extension changed to *.mp3.

It doesn't work. The output is literally named %outfile%. If I had to guess, there's something wrong with the initialization or calling of the outfile variable. I also thought that perhaps it is a data type issue. %f is perhaps a pointer, not a string, so some kind of string extraction or type conversion is needed.

How can I set and use a filename as a string variable in cmd?

user1934286
  • 1,732
  • 3
  • 23
  • 42
  • You need to enable and use [delayed expansion](http://ss64.com/nt/delayedexpansion.html) for `outfile` as you are writing *and* reading this variable in a single block of code; otherwise the variable is expanded to the value it holds when the entire block is parsed; or, even easier, follow [MCND's advice](http://stackoverflow.com/a/41813600) below... – aschipfl Jan 23 '17 at 18:55

1 Answers1

2

The best way to do it is not to do it. Don't store inside a variable something that can be directly retrieved from the for replaceable parameter.

for %f in (*.mp4) do ffmpeg -i "%f" -vn -ar 44100 -ac 1 -b:a 32k -f mp3 "%~nf.mp3"

The for command replaceable parameter (%f in your case) allow the use of some modifiers (see for /?). %~nf is the name, without extension, of the file being referenced by %f

But, if your only option is to do it, here you can find some ways to deal with the problem.

Community
  • 1
  • 1
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Yes, seems to be working. So it was a data type issue? If I changed the last part to `"%f.mp3"` it would be named exactly `filename.mp4.mp3`. Shouldn't `%f:mp4=mp3%` also work if it were the last line? – user1934286 Jan 23 '17 at 18:58
  • @fredsbend for variables are only valid inside the scope of the for (block) statement. –  Jan 23 '17 at 19:00
  • @fredsbend, and the sub-string replacement syntax cannot be used with `for` variable references, it is only valid for "normal" environment variables... – aschipfl Jan 23 '17 at 19:03
  • @fredsbend, answer updated. The provided link should clarify the source of your problem. – MC ND Jan 23 '17 at 19:05
  • @MCND Yes, starting to understand now. I'm close to novice with DOS and cmd. Did a UNIX course in college five years ago ... don't remember it. Thank you. – user1934286 Jan 23 '17 at 19:13
  • I don't think potential other solutions can get better than this, so I'll give the selection now, but don't let that stop anybody from answering with a different solution. I'll at least up vote if it works. – user1934286 Jan 23 '17 at 19:14
  • @fredsbend, just to avoid confusion: [The Windows command prompt is *NOT* a DOS prompt!](https://scalibq.wordpress.com/2012/05/23/the-windows-command-prompt-is-not-a-dos-prompt/) – aschipfl Jan 24 '17 at 11:11