0

I wrote this code for HandBrakeCLI as a batch file to manipulate my videos. This code creates output files with input file name plus a "_conv" suffix.

for /R .\test %%F in (*.mov) do HandBrakeCLI -e x264 --x264-preset medium -q 35 --crop 0:0:0:0 --aencoder copy -i "%%~fF" -o "%%~pF%%~nF_conv.mp4

Then I want to delete the original file and then remove _conv part of the output file. What should be added to the code above?

I want to delete each file just after converting it, or at least when going from its containing folder to another folder, not wholly after converting all of the file (because lots of files must be converted and I may run out of space)

By the way, how can I add other formats in addition of *.mov in the code?

living being
  • 213
  • 4
  • 10

1 Answers1

2
for /R .\test %%F in (*.mov) do (
    HandBrakeCLI -e x264 --x264-preset medium -q 35 --crop 0:0:0:0 --aencoder copy -i "%%~fF" -o "%%~dpF%%~nF_conv.mp4"
    if exist "%%~dpF%%~nF_conv.mp4" (
        del "%%~fF"
        ren "%%~dpF%%~nF_conv.mp4" "%%~nxF"
    )
)

All the information is inside your original code. All that is needed is to wrap the set of commands in parenthesis so the three commands are executed for each of the input files. Also, an aditional if has been included to only delete the source file if the converted file exists.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • I'd test `if !errorlevel! equ 0 if exist "%%~dpF%%~nF_conv.mp4" (` with assumption that `HandBrakeCLI` returns error codes properly... Might it be of advantage? – JosefZ Jan 29 '15 at 18:50
  • 1
    @Josefz, if handbrake raises errorlevel on failure (no, i don't have the software at hand to test), it is better to use `if not errorlevel 1` (that does not need the delayed expansion in your code) or use conditional execution of the rest of the commands, and in both cases, remove the test for file existence (or if you are as paranoid as i, better keep it). – MC ND Jan 29 '15 at 20:07
  • file converted. Original file deleted. But output file has not been renamed. – living being Jan 29 '15 at 20:18
  • 1
    `ren "%%~dpF%%~nF_conv.mp4" "%%~nxF"` as `rename` target may not include path but filename (with extension, of course) only – JosefZ Jan 29 '15 at 21:01
  • @livingbeing, the usual copy/paste problem. I forget to remove the path for the final name from the rename command. My fault. Answer updated – MC ND Jan 29 '15 at 21:11
  • @livingbeing 21:13 nope as whole `do ( bracketted code )` is parsed and evaluated for every instance of `%%F`. Try with `echo on` – JosefZ Jan 29 '15 at 21:19