7

Is there a way to set relative path to the icon in win shortcut? for the target location it works fine:

%windir%\system32\cmd.exe /c "cd %CD% && start fileToExecute.bat"

I read that win shortcuts can pick up icon from .exe files automatically but what to do if my target file is a batch file?

31415926
  • 3,811
  • 7
  • 47
  • 78
  • Batch files cannot have icons. Is that what you are asking? Or are you asking how to give a .lnk (shortcut) an icon if it is pointing to batch file? – Gray Feb 04 '13 at 21:07
  • I'm asking how to give a .lnk (shortcut) an icon if it is pointing to batch file – 31415926 Feb 04 '13 at 21:19
  • Programmatically? Or is this a one-off deal? – Gray Feb 04 '13 at 21:20
  • I just noticed your command. What are you trying to do here? That makes no sense. cmd is already in your environment path, so you don't need to specify that. you are changing directory to the current directory... just to execute a batch file. You can just make a shortcut to the batch file... the rest of that stuff looks pointless. – Gray Feb 04 '13 at 23:16
  • I'm not sure if relative paths can be used and if they were, which location they would be relative to. However, it probably wouldn't hurt if you elaborated on the end result you want to achieve, i.e. if you explained the *actual* issue you are having and trying to resolve (in your question, please). [What is the XY problem?](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Andriy M Feb 05 '13 at 00:30

1 Answers1

12

It's not entirely clear what you're asking, since your title says one thing but your question says another.

if the question is whether it is possible to set a relative path for the icon location in a LNK file, then the answer is no. The icon file format has two ways of specifying the icon location, either as an absolute path or as a path with environment variables expanded. There is no option for a relative path.

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
  • Interestingly I noticed .lnk files allow the environment variable %CD% in the Start In field but not in the Icon path. In the Icon path it does recognise other environment variables like %SystemDrive% – shufler Feb 21 '18 at 21:13
  • 1
    I'm surprised `%CD%` works anywhere at all, because it is not a real environment variable. It is a virtual environment variable understood only by cmd.exe. – Raymond Chen Feb 21 '18 at 23:56
  • Maybe the Windows 10 team missed the memo :) – shufler Feb 23 '18 at 02:00
  • @shufler I cannot get `%CD%` to be interpreted as anything special. It just looks for a directory called `%CD%`. – Raymond Chen Feb 23 '18 at 16:59
  • OK, so e.g. I create a shortcut to PowerShell.exe in C:\Temp and %CD% as the Start In. Run the shortcut and PowerShell starts in the C:\Temp directory. – shufler Feb 26 '18 at 23:44
  • 1
    @shuffler String the "Start In" as `%CD%` is meaningless anyway. It means "Start in the directory that you would normally start in." What happens is that defaults to `C:\Temp` and then tries to change to a subdirectory called `%CD%`, which fails, so you stay in `C:\Temp`. – Raymond Chen Feb 27 '18 at 01:05
  • OK, I see what you mean. If I put `C:\Temp` as the Start In folder in a shortcut and copy that to a machine that does not have `C:\Temp` it will start in the current folder. I guess this would only be an issue if `C:\Temp` exists but whatever the shortcut needs from the Start In folder does not. Effectively what you're saying is `%CD%` is the same as creating a shortcut with the Start In value `Z:\d3fcfce1-b3ce-42f3-af7a-888e407af544` (even though %CD% will always "work" and some smart aleck might create this folder). I can see how this might be a feature whose value is still in the negative. – shufler Feb 27 '18 at 23:01
  • The underlying issue is that `%CD%` - even if it worked (which it doesn't) - doesn't make any sense. What is the "current directory" of a shortcut? – Raymond Chen Feb 28 '18 at 00:24
  • 3
    I'm going to say the current directory of a shortcut is the directory that contains the .lnk file. – shufler Feb 28 '18 at 20:46