Link to home
Create AccountLog in
Avatar of neelyjer
neelyjerFlag for United States of America

asked on

VBScript to change shortcut properties

Hello Experts,

     I have 120 shorcuts that all include the same ip address in the target field under shorcut properties.  I need to change the ip address all shorcuts to HomePC.  I'm sure there's a way to do it programtically with VBScript rather than by hand, I'm just not sure how to do that.  I do have some experience with VBScript, just not enough to risk screwing up all of the shortcuts.  Below is an example of what I am looking for:

Original Target Value --
C:\Sim\Sim.exe C:\Sim\SimTrack\MyTrack.sim -sa 192.168.0.10:15700 -rs y -sn C:\Sim\SimTrack\MyTrack.ssf

Updated Value --
C:\Sim\Sim.exe C:\Sim\SimTrack\MyTrack.sim -sa HomePC:15700 -rs y -sn C:\Sim\SimTrack\MyTrack.ssf

All shorcuts are slightly different except for the IP address.  The ip addess is the only thing I'd like to change.

Can anyone suggest how to do this?

Thanks Experts!!
Avatar of Robberbaron (robr)
Robberbaron (robr)
Flag of Australia image

no exactly your question but theres a program 'shortcut doctor'...http://www.funduc.com/shortcut_doctor.htm.   but i was using it to change to path to executable,rarther than the parameters
Avatar of Mikkk
Mikkk

Make some kind of file iteration and for each file use this functions:

Set oWS = WScript.CreateObject("WScript.Shell")
sLinkFile = "C:\program.lnk"
Set oLink = oWS.CreateShortcut(sLinkFile)
oldpath=oLink.TargetPath
newpath="C:\windows\notepad.exe"
oLink.TargetPath=newpath
oLink.Save
https://www.experts-exchange.com/questions/21814142/VBScript-Modify-a-shortcut-NOT-CREATE-ANOTHER-ONE.html shows the way.  with http://www.ss64.com/nt/shortcut.html

CreateShortCut is used for both create & modify...


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

   'search for all link files
   ' for each one found .. modify
        ModifyLink lnkfilename
   'next link
   Wscript.quit

Sub ModifyLink (sLinkFile)
   
'   sLinkFile = "C:\MyShortcut.LNK"

   Set oLink = oWS.CreateShortcut(sLinkFile)
   oLink.TargetPath = "C:\Program Files\MyApp\MyProgram.EXE"
   '      oLink.Arguments = ""
   '      oLink.Description = "MyProgram"
   '      oLink.HotKey = "ALT+CTRL+F"
   '      oLink.IconLocation = "C:\Program Files\MyApp\MyProgram.EXE, 2"
   '      oLink.WindowStyle = "1"
   '      oLink.WorkingDirectory = "C:\Program Files\MyApp"
   oLink.Save
Exit Sub
ASKER CERTIFIED SOLUTION
Avatar of Robberbaron (robr)
Robberbaron (robr)
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of neelyjer

ASKER

@ robberbaron

     I haven't tried your code yet but, can you breakdown what the code is doing for my future reference?

Thanks

Jeremy
it's hardly 'my code' as it is copied from the links.

1. Set link = Osw.createshortcut(sLinkFile)
this is creating the shortcut object and opening the EXISTING shortcut defined by sLinkfile.  It could also create a new shortcut if sLinkFile does not exist.

2. The shortcut object has properties. Such as target path , arguments, description, working directory. Just the same as those you see when viewing properties of an existing shortcut from the right-click menu.

3. the script allows editting of these.
for the arguments, we are searching for the string defined by sFind (=192.168.0.10) using InStr.
we then replace the found substring with a new one by adding the string before the found location , the new string and then the bit of original from after the found substring

hope that helps.
Using the follwoing...

Set oWS = WScript.CreateObject("WScript.Shell")
ModifyLink lnkfilename
Wscript.quit
Sub ModifyLink (sLinkFile)
'sLinkFile = "C:\Documents and Settings\jneel00\Desktop\Sim++ Stuff\SIM SPs\TEst\"
Set oLink = oWS.CreateShortcut(sLinkFile)
   sFind = "192.175.0.10"
   sReplace = "HomePC"
   ix = instr(oLink.Arguments, sFind)
   if ix > 0 then
          oLink.Arguments = left(oLink.Arguments,ix-1) & sReplace & mid(oLink.Arguments,ix+len(sFind))
       oLink.Save
   end if
Exit Sub
End Sub

I am receiving an error on the following:
Set oLink = oWS.CreateShortcut(sLinkFile)

The error says that the pathname must end in .lnk or .url.  I am trying to use this code to update the 120 links I have in this directory.  Am I missing something?
BTW...

The code works great when I specify a specific link to be modified.