Search code examples
powershellcommand-line

Why does a for loop work in Windows Command Line, but not in PowerShell?


Windows 11 here.

Trying to convert all the files in a folder (they're all .mp4 files) into .mp3 files using FFMPEG

I got the code from https://ottverse.com/convert-all-files-inside-folder-ffmpeg-batch-convert/#Convert_all_the_audio_files_WAV_MP3_OGG_in_your_folder_to_MP3_format

I'm currently in the E:\Downloads2\ folder

The following code works when using Windows Command Line (CMD):

for %f in (*.*) do "C:\FFMPEG\ffmpeg" -i "%f" -vn -acodec libmp3lame -q:a 2 "E:\Downloads2\converted\%~nf.mp3"

However, when I try it in Power Shell (with ./ appended before it), I get the following error message:

*.* : The term '*.*' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Why is it not working in Power Shell?


Solution

  • cmd and PowerShell are two different languages. To call cmd from powershell:

    cmd /c 'for %f in (*.*) do "C:\FFMPEG\ffmpeg" -i "%f" -vn -acodec libmp3lame -q:a 2 "E:\Downloads2\converted\%~nf.mp3"'
    

    Powershell would be something like:

    get-childitem *.mp4 | foreach-object { 
    C:\FFMPEG\ffmpeg -i $_ -vn -acodec libmp3lame -q:a 2 ('E:\Downloads2\converted\' + $_.basename + '.mp3') }