1

I’m running cmd.exe code directly from within a windows shortcut .lnk file rather than in .bat text file; example:

%COMSPEC% /Q /V:ON /C “SET QUERY=StackOverflow && ECHO Hello !QUERY!”

Noting, the reason I’m running code like this is due to group policy restrictions. So I’ve been learning the joys of single-line coding with only 260 available characters! :)

And I’m looking for an equivalent to %0 in batch files that can return me the source file of the shortcut itself? Or at least the folder it started in?

Noting I can’t just simply just set the shortcut’s ‘Start In’ directory, because I want to retain the original %CD% that called the shortcut.

I don’t think it’s even possible. But if anyone at least has any good general tips for single-line coding then please share!

Skytunnel
  • 1,063
  • 2
  • 10
  • 21
  • 3
    This is one of the most strangest things I have seen! Good luck with that! **`;)`** You have _no facilities_ at all in a `.lnk` file started this way. I suggest you to use the trick described at [this answer](https://stackoverflow.com/questions/13320578/how-to-run-batch-script-without-using-bat-extension/13337597#13337597)... – Aacini Aug 02 '19 at 17:33

1 Answers1

0

This is only a partial answer. But I was able to get the equivalent to %~n0 (i.e the name of the shortcut)

This works because when you call cmd.exe from a shortcut, the window title is equal to the shortcut’s filename

Create a new shortcut file called ‘getWinTitle’ with the following command

%COMSPEC% /Q /C FOR /F "delims=: tokens=2" %G IN ('TASKLIST /FI "IMAGENAME eq cmd.exe" /FO "LIST" /V^|FIND "Window Title"') DO IF NOT "%G"==" N/A" IF NOT "%G"==" getWinTitle" ECHO %G

Then in your main shortcut code, you call the ‘getWinTitle.lnk’ file and store it’s output as follows:

%COMSPEC% /Q /V:ON /C “(FOR /F "tokens=*" %G IN ('getWinTitle.lnk') DO SET _WT=%G)&ECHO !_WT!”

You could do this all in one shortcut, but it eats up a lot of the 260 character limit. I found this article which describes how to increase the 260 character limit to 1096. But I was unable to use this method, as it was also blocked under the group policy of my machine.

“The maximum target length can be extended though by creating the shortcut via a jscript making use of the 'WScript Shell' object. The GUI properties window will still clip the target field at two hundred sixty characters, but the 'WScript Shell' object will only clip the target parameter at one thousand ninety-six (1096) characters.”

https://www.uperesia.com/booby-trapped-shortcut-generator

Skytunnel
  • 1,063
  • 2
  • 10
  • 21