?
Solved

VBScript to change shortcut properties

Posted on 2007-10-02
8
Medium Priority
?
14,924 Views
Last Modified: 2008-08-30
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!!
0
Comment
Question by:neelyjer
  • 4
  • 3
8 Comments
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 20002921
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
0
 
LVL 8

Expert Comment

by:Mikkk
ID: 20002965
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
0
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 20002986
http://www.experts-exchange.com/Programming/Misc/Q_21814142.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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 2000 total points
ID: 20003017
  Set oLink = oWS.CreateShortcut(sLinkFile)
   sFind = "192.168.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.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
   end if
0
 

Author Comment

by:neelyjer
ID: 20010451
@ robberbaron

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

Thanks

Jeremy
0
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 20020520
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.
0
 

Author Comment

by:neelyjer
ID: 20156398
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?
0
 

Author Comment

by:neelyjer
ID: 20156403
BTW...

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Make the most of your online learning experience.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question