Batch feed file selection as script arguments

In this article, we’ll see how to run scripts on a selection of files, either through Drag-and-drop or through send-to.

Script Example

For a simple example, the following script is in batch language (Windows platform).
But we could just as well use a Python script with sys.argv or argparse

Example: Resize all video files passed as arguments to 720p using ffmpeg.

1
2
3
4
5
:loop
if "%~1"=="" goto :eof
ffmpeg -i %1 -vf "scale=1280:-1" "%~dp1%~n1_720.mp4"
shift
goto loop

The code must be saved in a to_720.bat file

Command explanation:

The command will be executed on the first argument, then the next one (thanks to shift), and so on until all arguments are exhausted.

%~1: The argument as-is, the complete file path

%~dp1: The path of the directory containing passed file

%~n1: “stem” of the passed file (name without extension). The original extension can be placed with %~x1

Note: Using the combination %~dp1%~n1 allows putting the output file in the same directory as the input file. If we only used the name “%~n1_720.mp4” for the output name, the destination would correspond to the folder containing the “bat” script (working directory).

Usage

Drag-and-drop

As is, you can directly select your files and then drag and drop them onto the script file.

But you need to have the script quickly acessible. If it’s in the same directory it’s easy, but that’s not often the case and not really practical.

Hence the use of the context menu

Context menu: Send To:

Put the bat file (or a shortcut to the bat file) in the Windows SendTo folder.

To open the folder, open Run (type “run” in the Windows search to launch the window), then enter and validate the following command: shell:sendto.

Once the file or shortcut is placed in SendTo, you can send an entire selection of files directly for processing via right-click > send to > custom_script.bat :thumbsup: