1

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).

Dharma Leonardi
  • 161
  • 2
  • 6

1 Answers1

1

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
Stephane
  • 6,432
  • 3
  • 26
  • 47
  • I understand this point. However, the problem does not seem to be from folder paths. All files are in the same directory. I've also tried hard coding the paths into the batch file but no dice. The diskpart command is run correctly as the output is dumped to the console, but just not into the VHD.INFO file. The VHD.INFO file however is created, but with 0 byte length. I'm suspecting it is something to do with cmd opening a new instance interfering with the pipe? – Dharma Leonardi May 28 '14 at 12:48
  • Did you try to set the current path at the start of your command-line ? – Stephane May 28 '14 at 13:13
  • Yes, and it didn't make any difference. FYI, the dragged and dropped file is in the same directory of the batch file that is to be run. The difference I noticed is that when executed on the command line, everything runs in the same window. While using drag & drop the diskpart command runs in a new window and the output is dumped into that console then closes. – Dharma Leonardi May 28 '14 at 13:58
  • Ok, so I have finally found the problem. It is because the batch file requires Administrator Privilege to run. Right clicking and running as administrator works. So, have found the problem but not a fix. How would one run the batch via drag and drop with admin privilege? – Dharma Leonardi May 28 '14 at 14:58
  • 1
    Two solution: resart the batch file with elevated privs (search the stak for detail) if your user is admin or change it to a scheduled task if he's not (ask a new question on Friday if you want ore detail) – Stephane May 28 '14 at 18:31
  • Not sure if it works anymore in Windows 7; but I know for a fact that in Win8.1 you can create a shortcut for the batch file and have it run as administrator as well. – Get-HomeByFiveOClock May 29 '14 at 19:45