1

Can a Windows batch file determine its invoked filename when invoked through a shortcut?

For example, I create real.bat, and create its shortcut named phony.bat (.lnk?)

And invoke phony by double-click on it.

Can this batch file detect the name phony.bat instead of real.bat?

Of course I can just copy it to another name, but when I edit one of them, I have to manually sync the content to all files.

The question is related to Can a Windows batch file determine its own file name?, but different.

Cychih
  • 352
  • 2
  • 14

2 Answers2

1

As in your you mentioned that you've created the shortcut I assume you can create the with any properties you want.

So right click on your lnk file and change the the target line to:

C:\Windows\System32\cmd.exe /c "set "lnk_call=1"&"C:\PATH\TO\your.bat" "

This will change the icon of the link so to set back to batch file cog click on change icon and find the bat file icon in :

%SystemRoot%\System32\SHELL32.dll

Finally in your bat put this line:

if defined lnk_call echo triggered from lnk file

the lnk_call now can be used to determine if your file is called from double clicking on a .lnk file. I don't think it is possible to detect this from a shortcut that anyone else created.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Wow it's brilliant, save the original filename in property. Just a little bit troublesome that I have to edit every `lnk` files – Cychih Mar 16 '18 at 13:16
  • @Cychih To do this automatically(through command line) you can check this: https://github.com/npocmaka/batch.scripts/blob/master/hybrids/jscript/shortcutJS.bat – npocmaka Mar 16 '18 at 13:21
0

Oh yeah, I found hardlink useful in this case:

mklink /h <link-name> <source-file>

I can create many hardlinks with different name, and they all points to the same file, so I can freely edit any one of them without manually sync their content.

Cychih
  • 352
  • 2
  • 14