0

I have a python script that I want to be able to drag .exe files onto and have that python script process the files. I don't think Python easily supports this so I want to make a .BAT file which, for a file dragged onto it, gets the path of that file and then runs something like:

python C:\files\myPy.py [full path to dragged .exe here]

Basically, it just runs my script with the dragged file as an argument.

The part I am struggling with is simply getting the path of a dragged executable. Would this be %%f or %1?

the_endian
  • 2,259
  • 1
  • 24
  • 49
  • Python's default `Python.File` association for .py scripts does implement a drop handler. Recent versions of Python 3 even install a shell extension (pyshellext.amd64.dll) to better support Unicode paths in the handler. – Eryk Sun Jun 03 '17 at 06:59

1 Answers1

0

The 0-th argument of a batch-file is the path to the batch-file itself. The 1-st argument is... Well the first thing you put after which (when dragging the file onto the command-line) that should be the path to the executable.
To make sure everything is properly enclosed in double-quotes, you now want to first remove all surrounding quotes and then add new ones:

python C:\files\myPy.py "%~1"

The ~ removed the (potentially) existing quotes. If there are none, it does not remove anything. This makes it possible to make safe that the quotes are correct (at least for further use in batch; not sure about python).

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54