Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding an icon to a shortcut 2

Status
Not open for further replies.

JPJeffery

Technical User
May 26, 2006
600
GB
Hi

I've seen code that will create a .lnk shortcut but I need to do the equivalent for a .url shortcut. I seem to remember finding the code somewhere on t'internet but then abandoning the idea in favour of a .BAT solution (because I know batch far better) only then to be told that I must use vbscript.

So, I've written working vbscript code to copy the .URL shortcuts from a central repository to the All Users Startup folder but I reckon it'll be quicker, and have less of an impact on the network, if I can produce code to create new shortcuts instead. However, what I can't find anything on is how to specify which icon from a (Windows standard and present on the client PC) .DLL to use for the shortcut instead of the rather dull default IE icon.

This must be possible, right? But how?

Thanks for all responses in advance (as well as afterwards!)

Jeff
 
Yes it is possible and easy to do.

The property you want to set is iconlocation

Defines the location of the shortcut’s icon. Typically, its value is the complete path and filename to
the file containing the icon followed by a comma and the zero- based position of the icon within
the file. If the default icon is used, the value of IconLocation is ” ,0”.
TargetPath Sets or returns the path and filename to the shortcut’s executable file.

Here is an example of how to do it.

Code:
SET fso = Wscript.CreateObject("Scripting.FileSystemObject")
SET WshShell = WScript.CreateObject("WScript.Shell")
WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")
strDsk = WshShell.SpecialFolders("Desktop")
strshortcut = strDsk & "\Calculator.lnk"
If Not fso.FileExists(strshortcut) Then
	SET oUrlLink = WshShell.CreateShortcut(strshortcut)
	oUrlLink.TargetPath = Windir & "\System32\Calc.EXE"
	oUrlLink.IconLocation = Windir & "\System32\moricons.dll,6"
	oUrlLink.Save
End If



I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
That's marvellous. One query though, will this be significantly different bearing in mind I'm creating a url shortcut ( rather than to a shortcut to an .exe?

And by the by, how do you add an event to an event log (local or to a server)?

Thanks for this.

JJ
 
There is no difference between a LNK or URL when creating the shortcut as far as this property is concerned.

Sorry I can not assist with writing to the event log. I suggest posting that question in a new thread so you get new sets of eyes looking to respond.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
OK Curiosity got the best of me and it turns out it is quite simple.

Code:
Const EVENT_SUCCESS = 0

Set objShell = Wscript.CreateObject("Wscript.Shell")

objShell.LogEvent EVENT_SUCCESS, _
    "Payroll application successfully installed."

I like this so much I can see myself adding this feature to most of my scripts. Thanks for getting my curiosity up. :)

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Glad I could help...

;-)

And thanks for yours!

JJ
 
Just for the record, the IconLocation property doesn't seem to work for .URL icons, only .LNK icons.

As a .LNK icon works just as well as a .URL it's hardly a show-stopper but still...

JJ
 
Sorry I neglected to mention how I cheat on my method. If you give the icon an extension of LNK but point it to a URL all of the above code works without having to do the extra steps that tsuji points to. Tsuji's solution is really nice however in that it supports pointing to an ICO file where the Shortcut method only supports pointing to an EXE or DLL. I usually just go with the DLL method and use a program to add my ICO files to the DLL.

Here is an example that create a link to CNN.Com with a world map icon.

Code:
SET fso = Wscript.CreateObject("Scripting.FileSystemObject")
SET WshShell = WScript.CreateObject("WScript.Shell")
WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")
strDsk = WshShell.SpecialFolders("Desktop")
strshortcut = strDsk & "\CNN.lnk"
If Not fso.FileExists(strshortcut) Then
    SET oUrlLink = WshShell.CreateShortcut(strshortcut)
    oUrlLink.TargetPath = "[URL unfurl="true"]http://www.cnn.com"[/URL]
    oUrlLink.IconLocation = Windir & "\System32\moricons.dll,61"
    oUrlLink.Save
End If

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top