0

My question is pertaining to coding the BAT or PowerShell file, not ffmpeg commands. Also I know how to edit Windows context menu, so I'm good with that also.

I want to create a BAT or PowerShell file (whichever is easier), add a shortcut to the BAT/PS1 file via Windows's "Send To" right-click context menu, and be able to send a folder with FLAC files, or an individual FLAC file, to convert to MP3. The converted file(s) can be placed in the same directory as the FLAC files.

This is the basic BAT code I have, which doesn't work:

for /f "usebackq delims=" %%a in ("%~1") do (

    [ffmpeg conversion code goes here]

)

I have no idea how to approach this. Looking for assistance. Thanks.

howdoicode
  • 779
  • 8
  • 16
  • If your input argument is an appropriately spaced and quoted, _where necessary_, set of files, then perhaps, `@For %%A In (%*)Do @ffmpeg ...` will get you further! – Compo Jul 15 '19 at 13:45
  • @Compo I tried what you suggested, but ffmpeg tries to reference the folder name instead, not the contents. Here is my code: `For %%A In (%*) Do ffmpeg -i "%%~nA.flac" -c:v copy -b:a 320k "%%~nA.mp3"` – howdoicode Jul 15 '19 at 15:18
  • 1
    Well that's because I hadn't noticed that you were sending either a directory or a file. I provided advice based upon one or more files. You cannot expect the code to act the same for both types of object, so you may be better advised to use code to determine whether the input exists and what type of object it is, then run an appropriate loop depending upon that result. – Compo Jul 15 '19 at 15:23
  • @Compo So what do I need to do to make it work for a folder? – howdoicode Jul 15 '19 at 15:32
  • @Compo I did say in my initial post **I have no idea how to approach this**. But since you want my searches and attempts, this is what got and none have worked: `For %%A In (%*) Do ffmpeg -i "%%~nA.flac" -c:v copy -b:a 320k "%%~nA.mp3"` `For /D %%A In (%*) Do ffmpeg -i "%%~nA.flac" -c:v copy -b:a 320k "%%~nA.mp3"` `For %%A In (.\*) Do ffmpeg -i "%%~nA.flac" -c:v copy -b:a 320k "%%~nA.mp3"` `For /D %%A In (.\*) Do ffmpeg -i "%%~nA.flac" -c:v copy -b:a 320k "%%~nA.mp3"` `For %%A In (%*\) Do ffmpeg -i "%%~nA.flac" -c:v copy -b:a 320k "%%~nA.mp3"` ... – howdoicode Jul 15 '19 at 15:46
  • @Compo ...Continued `For %%A In (*) Do ffmpeg -i "%%~nA.flac" -c:v copy -b:a 320k "%%~nA.mp3"` `For /D %%A In (*) Do ffmpeg -i "%%~nA.flac" -c:v copy -b:a 320k "%%~nA.mp3"` – howdoicode Jul 15 '19 at 15:47
  • @Compo Sites I went to: [Get list of passed arguments in Windows batch script (.bat)](https://stackoverflow.com/questions/357315/get-list-of-passed-arguments-in-windows-batch-script-bat/382312) [How to get filename only without path in windows command line?](https://superuser.com/questions/489240/how-to-get-filename-only-without-path-in-windows-command-line) [Variable for getting absolute path in windows batch script](https://superuser.com/questions/1014835/variable-for-getting-absolute-path-in-windows-batch-script) – howdoicode Jul 15 '19 at 15:52
  • @Compo ...Sites continued... [Resolve absolute path from relative path and/or file name](https://stackoverflow.com/questions/1645843/resolve-absolute-path-from-relative-path-and-or-file-name) [Iterate all files in a directory using a 'for' loop](https://stackoverflow.com/questions/138497/iterate-all-files-in-a-directory-using-a-for-loop) [Rob van der Woude's FOR loop]https://www.robvanderwoude.com/for.php) – howdoicode Jul 15 '19 at 15:52
  • Can you please use the [edit facility](https://stackoverflow.com/posts/57040712/edit), to include the pertinent code and information within your question. The comment area isn't always read, so after you've edited your question, please remove the then unnecessary comments. – Compo Jul 15 '19 at 16:03
  • @Comp I can't believe you can't just tell me if I am missing something from my code after **YOUR** suggestion. All I wanted to know if I am missing a "%", ".", "\", or whatever arcane character needed for BAT to use the files inside the directory for ffmpeg to work on. – howdoicode Jul 15 '19 at 16:10

1 Answers1

0

After much searching, and trial and error, I discovered a solution.

For  %%A In ("%~1\*.flac") Do (

    ffmpeg -i "%%~dpnA.flac" -c:v copy  -b:a 320k  "%%~dpnA.mp3"

)

I found the idea for my solution from here https://stackoverflow.com/a/12269576/11653053.

By adding "%~1\*.flac" for the set in the FOR loop, this allowed me to also ignore anything else inside the folder, such as image files.

howdoicode
  • 779
  • 8
  • 16
  • 1
    Whilst I applaud your efforts, the solution you've provided works for a directory as input but your question wants to be able to accept a single file as input too. As I said before you really need to determine whether the input exists and what type of object it is, then run an appropriate loop depending upon that result. Have you abandoned that idea now, or do you still require help? – Compo Jul 15 '19 at 18:40