Using drag and drop from another directory to a batch file does not work

I have a folder structure that looks like this:

project bin my_program.exe misc_stuff.exe DROP_OVER_ME.bat input_file.txt 

Basically, I want to be able to drag the input file over the batch file DROP_OVER_ME.bat and transfer it along the path of the input file to exe.

This is what my batch file looks like:

 @echo off start bin/my_program.exe %1 exit 

When I drag input_file.txt over the batch file, everything works fine - my_program.exe successfully gets the path to the input file and starts.

However, when input_file.txt is outside the project folder, dragging it causes the batch file to pop up with a message

Windows cannot find "bin / my_program.exe". Make sure you type the name correctly, and then try again.

How can I fix my batch file so that I can drag and drop files from any arbitrary location inside my file system?

+7
source share
1 answer

It appears that the script package bases the current working directory as the directory from which you drag and not the directory containing the script. (You can verify this by adding echo %cd% && pause to your script if you want.) Try changing your script as follows to eliminate any ambiguity regarding the file paths:

 @echo off cd /d "%~dp0" start "" "bin\my_program.exe" "%~f1" exit /b 
+7
source

All Articles