user198989
user198989

Reputation: 4665

vbscript - modify shortcut if it exists

I'm trying to modify shortcut (by deleting old one and creating new one) to desktop, the script first checks if file exists, if it exists, it should modify it. Tried this without any luck. It works when I remove the file exist check, so the problem should be on the file exists check. Here is my code:

Set wsc = CreateObject("Scripting.FileSystemObject")

If (wsc.FileExists(wsc.SpecialFolders("desktop") & "\Google Chrome.LNK")) Then

Set fso = WScript.CreateObject("WScript.Shell")

Set lnk = fso.CreateShortcut(fso.SpecialFolders("desktop") & "\Google Chrome.LNK")
lnk.targetpath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
lnk.Arguments = "http://www.google.com"
lnk.save

End If

What is the correct way to do that ?

Upvotes: 1

Views: 2435

Answers (1)

triggeradeadcat
triggeradeadcat

Reputation: 121

Your objects are mixed up. You are calling FSO wsc and wscript.shell fso. You are using wscript.shell before creating it.

Set wsc = CreateObject("WScript.Shell")

If (wsc.FileExists(wsc.SpecialFolders("desktop") & "\Google Chrome.LNK")) Then
  Set fso = WScript.CreateObject("Scripting.FileSystemObject")

I've swapped the objects to what you intended rather that what you typed.

Here's all 5 errors fixed, plus the one logic error (you only created shortcut if it already existed):

Set wsc = CreateObject("WScript.Shell")
Set fso = WScript.CreateObject("Scripting.FileSystemObject")

If (fso.FileExists(wsc.SpecialFolders("desktop") & "\Google Chrome.LNK")) = false Then

    Set lnk = wsc.CreateShortcut(wsc.SpecialFolders("desktop") & "\Google Chrome.LNK")
    lnk.targetpath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
    lnk.Arguments = "http://www.google.com"
    lnk.save

End If

Upvotes: 3

Related Questions