9

I want to drag and drop a file onto a batch file in order to run the below command on it. How do I go about running the command on the dropped file?

PotreeConverter.exe <dropped file> -o C:/output -p index
Mark
  • 565
  • 1
  • 8
  • 19

2 Answers2

15

The path of the file, when you drop it on the BATfile, will be returned as a normal %1 argument.

so :

@echo off
PotreeConverter.exe "%~1" -o C:/output -p index

You can use %* if you drop more then 1 file

Example :

@echo off

for %%a in (%*) do echo [%%a] was dropped on me 
pause
SachaDee
  • 9,245
  • 3
  • 23
  • 33
  • that won't work for file names with spaces or special characters – phuclv Aug 16 '18 at 02:07
  • what will not work ? That's the only known solution to get the file name of a dropped file in bat!!!! – SachaDee Aug 16 '18 at 10:41
  • last time I tested and it doesn't work for strings with spaces but I forgot how I run the command. Maybe I made some mistake. But it won't work for files like `somename(a,b)` or `me&you.txt` due to the [bad file name handling in Windows Explorer](https://stackoverflow.com/q/1243240/995714) – phuclv Aug 16 '18 at 10:52
  • I don't know how you made your test ! I made a file `"fich&ier(1) test.txt"` and it work perfectly ! And again that's the only solution in BAT!!! – SachaDee Aug 16 '18 at 10:59
  • copy exactly the names I pasted above. Names with both spaces and comma, semicolon or ampersand won't work – phuclv Aug 16 '18 at 11:01
  • Did you read the name i tested `fich&ier(1) test.txt` it have a space and a & and () and it work !!! – SachaDee Aug 16 '18 at 11:04
  • yes. As I said, you must use names like `a;b.txt`, `a,b.txt`, `a&b.txt` **without spaces in their names** – phuclv Aug 16 '18 at 11:05
  • Is there a variable returning the folder where the .bat file is? If I use "." it will refer to the folder of the dragged file.... – Redoman Jun 18 '22 at 21:09
  • Yes you can use %~p0 – SachaDee Jun 18 '22 at 22:50
  • I do have an issue when my filename is like Last^First.zip and using move "%1" USB\ in a .bat script. The "%1" strips out the "^" sign from the filename. Is there a way to preserve the "^". This is when I drag and drop onto the .bat script. – SScotti Oct 18 '22 at 18:41
2

Following this easy guide.

Create a batch file test.bat with the contents

@echo off
echo The full path of the file is: %1
pause

Drag any file onto it, you will see that %1 is replaced with the full path for that file in quotes.

Now you know how to execute some command that takes a path to a file as an argument:

@echo off
some_command_that_takes_a_path_to_a_file %1
JoseOrtiz3
  • 1,785
  • 17
  • 28
  • 1
    `%1` already contains double quotes, so no need to add them especially if the path contains spaces ! – Youssef Jun 02 '23 at 13:50