Windows – Why is execution of batch files different between drag & drop and from command line

batch-filewindows

Ok, so I've been trying to figure this out for hours with no progress. I have created a batch file to get details of a VHD.

Everything runs fine and produces the expected results when run from the command line in a command prompt. However, when I use drag and drop from file explorer (dragging a vhd file and dropping onto the batch file) the batch file runs without errors but the output (VHD.INFO) is empty.

I'm stumped.

Edited to only include the behaviour:

@echo off
cls
setlocal enabledelayedexpansion
set "_PATH.THIS=%~dp0"

echo HELP | diskpart > %_PATH.THIS%OUTPUT.TMP

TYPE %_PATH.THIS%OUTPUT.TMP
PAUSE

To demonstrate the different behaviour, please run the batch file from the command line once (works) and also run the batch file by double clicking in file explorer (failure in all piping commands).

Best Answer

When you run your batch file from the command line, the current directory is the location of your batch file.

When you drag-and-drop a file onto the batch file, the current directory is the path of the drag-and-dropped file instead.

To demonstrate this, try it with the following batch file:

@echo off
cd
pause