Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the directory of shortcut to a batch file

Tags:

batch-file

cmd

I have a batch script file C:\scripts\MyScript.bat, I create shortcuts for this file in many other directories:

  • folderA\shortcut1.lnk

  • folderB\shortcut2.lnk

  • ....

I want to be able to get the folder of the shortcut inside the script, meaning if the script called using shortcut1 I get folderA, if using shortcut2 I get folderB, ...

Please note that I can do this without shortcuts, by creating another script shortcut.bat and inside this I pass %~dp0 as a parameter to MyScript.bat, but I want to do it with shortcuts because it's easier to manage.

Any ideas will be appreciated.

PS: this is different from the Extract Path from shortcut link - windows batch question.

as a user commented below, I want a batch file which can programmatically determine the location of the shortcut which invoked it.

like image 423
Ould Abba Avatar asked Nov 03 '25 22:11

Ould Abba


1 Answers

I have just tested the method I commented on and it appears to work as I had assumed.

Create the following batch file:

@Echo Off
Set "InvokedFrom=%__CD__:~,-1%"
CD /D "%~dp0"
Echo Your current directory is %__CD__:~,-1%
Echo=
Echo This file was invoked from %InvokedFrom%
Echo=
Pause

Now create a shortcut to that batch file.

Right Click on the new shortcut and change the Start in: field to %V.

Now copy the shortcut to a new location and double click it!

The batch file should open and report the path from where the shortcut was located.

like image 88
Compo Avatar answered Nov 07 '25 14:11

Compo