4

How can I create a bat or vbs file to create a Windows 7 compatible desktop shortcut?

I need the bat or vbs file to create the desktop shortcut using the following target and start-in locations (below). I basically created a desktop app that uses Google Chrome Portable to render my webapp as if it is Windows native and the shortcut will launch Chrome so it is very lightweight and looks like a genuine Windows application kinda like what Prism used to do. I've tried manually creating the shortcut.lnk but when my user installs my app it wont extract my shortcut.lnk via this path C:\Users\Public\Desktop so thats why I am now trying to create a bat or vbs file I can run on install. Thanks for your help.

Target:

C:\MyProgram\App\Chrome-bin\chrome.exe --user-data-dir="C:\MyProgram\Data\profile" --app=http://my-web-site-url.com/

Start in:

C:\MyProgram\App\Chrome-bin

Jay
  • 73
  • 1
  • 2
  • 8

2 Answers2

7

Your installer should be able to do this ... here is how in VBS:

Set wsc = WScript.CreateObject("WScript.Shell")
Set lnk = wsc.CreateShortcut(wsc.SpecialFolders("desktop") & "\XXXX.LNK")

lnk.targetpath = "C:\MyProgram\App\Chrome-bin\chrome.exe"
lnk.arguments = "--user-data-dir=""C:\MyProgram\Data\profile"" --app=http://my-web-site-url.com/"
lnk.description = "Bla bla"
lnk.workingdirectory = "C:\MyProgram\App\Chrome-bin" 
lnk.save
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • Dude, that is awesome, it worked! How do I specify a icon using your script. Thank you so very much – Jay Oct 26 '12 at 11:21
  • 1
    lnk.IconLocation ref; http://msdn.microsoft.com/en-us/library/xsy6k3ys(v=vs.84).aspx – Alex K. Oct 26 '12 at 11:23
  • One last question, your awesome. Could Do some such as this? I tried like this and get a error lnk.IconLocation ref = "%SystemDrive%\MyProgram\Data\profile\Default\Web Applications\my-web-site-url.com\http_80\logo.ico" – Jay Oct 26 '12 at 11:36
  • To expand the environment vars use ExpandEnvironmentStrings: `lnk.IconLocation = wsc.ExpandEnvironmentStrings("%SystemDrive%\MyProgram\Data\profile\Default\Web Applications\my-web-site-url.com\http_80\logo.ico")` – Alex K. Oct 26 '12 at 11:43
  • worked like a charm. You have been such a great help. Thank you so much again, I guess we can now say my question is officially resolved. TY. Now I just need to figure out how to rep you for the help – Jay Oct 26 '12 at 11:47
  • Your welcome, just tick the tick next to the answer if you would like to accept it. – Alex K. Oct 26 '12 at 12:27
  • Ah, ok. I just click the check. Any idea how I can convert this vbs to a bat now? – Jay Oct 26 '12 at 14:11
  • You should be able to just run the vbs file or run `cscript xxx.vbs` from a bat – Alex K. Oct 26 '12 at 14:12
  • cscript xxx.vbs from a bat did it. Thanks again :) – Jay Oct 26 '12 at 14:30
  • Hi bud, do you know how to set the shortcut.lnk to be Maximized by default within the vbs script? I try several combinations and failed. lnk.run = wsc.ExpandEnvironmentStrings("Maximized") and also tried lnk.run = "Maximized" – Jay Oct 27 '12 at 08:06
1

You can use the INTERNAL command MKLINK to make a SYMBOLIC link (ie: It acts just like the file it's linked to).

You need to have an elevated command prompt, or have the Administrator account activated (with a password set, as RUNAS will not accept a blank password).

From an elevated command prompt:

mklnk.bat

@echo off
mklink %~n1.lnk %~dpnx1

With an active Administrator account:

mklnk.bat

@echo off
runas /user:administrator "cmd /c mklink %~dpn1.lnk %~dpnx1"

Because mklink is an internal command, you can't use RUNAS to directly access it, but you can run CMD.EXE as the Administrator, and then call mklink from there.


Both of the above batch files will accept the same options and create the same files in the same place. So if you call the batch file mklnk.bat :

c:>mklnk welcome.msg
symbolic link created for welcome.lnk <<===>> welcome.msg

Another CMD.EXE window will flash on the screen, but that is normal.

James K
  • 4,005
  • 4
  • 20
  • 28
  • The above will not put the link on the desktop, but rather in the same directory as the indicated file. In the example I gave above, it's in the current directory. – James K Oct 28 '12 at 01:47