0

I have the following batch-file code to convert some video files with HandBrakeCLI:

for /R .\test %%F in (*.mp4,*.avi,*.flv,*.mov,*.3gp,*.wmv,*.mkv,*.ts) 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"
    )
)

To apply this code on videos, I should copy the batch-file and Handbrake.exe (and also its relative folders) and paste them beside the folder named test (in the code above) then change the name "test" in the batch file to the name of that folder, then run the batch-file.

Could you write the batch-file in a way we run it in an arbitrary folder, so it prompt for a folder containing videos and we write the path (or just simply drag and drop the folder to the command line and press enter) without moving the files and renaming the "test"?

living being
  • 213
  • 4
  • 10

2 Answers2

1

You should create a variable to be read from user input

set /p path=Videos path :

for /R %path% %%F in (*.mp4,*.avi,*.flv,*.mov,*.3gp,*.wmv,*.mkv,*.ts) 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"
    )
)

set /p will work fine either writing down the path or with folder drag-and-drop

Jhon
  • 582
  • 7
  • 28
1

Use "%~1" to read a drag-and-drop argument.

for /R "%~1" %%F in (etc...)

If the batch script is launched without drag and drop (if "%~1"==""), you could use a folder chooser to let the user browse for a folder.

@if (@CodeSection == @Batch) @then

:: based on fchooser2.bat
:: https://stackoverflow.com/a/15906994/1683264

@echo off
setlocal

if "%~1"=="" (
    call :chooser dir || goto usage
) else if exist "%~1" (
    set "dir=%~1"
) else goto usage

for /R "%dir%" %%F in (*.mp4,*.avi,*.flv,*.mov,*.3gp,*.wmv,*.mkv,*.ts) 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"
    )
)

:: end main runtime.  Pause if dragged-and-dropped or double-clicked.
if /i "%cmdcmdline:~0,6%"=="cmd /c" pause
goto :EOF

:chooser <var_to_set>
setlocal
for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0"') do (
    if not "%%~I"=="" if exist "%%~I" (
        endlocal & set "%~1=%%~I"
        exit /b 0
    )
)
exit /b 1

:usage
echo Usage: %~nx0 [directory]
goto :EOF

@end
// JScript portion

var shl = new ActiveXObject("Shell.Application");
var folder = shl.BrowseForFolder(0, "Please choose a folder.", 0, 0x00);
WSH.Echo(folder ? folder.self.path : '');
Community
  • 1
  • 1
rojo
  • 24,000
  • 5
  • 55
  • 101
  • Also worth noting, if you drag and drop more than one folder, the others will be passed in as %~2, %~3, etc. If you have multiple items selected, they will be processed for the one you dragged on, and then loop back up to the top - if you highlight file1, file2, file3, and file4 and then drag file3 to the script, they will be processed in the order file3, file4, file1, file2. – SomethingDark Feb 04 '15 at 22:09