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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find and replace shortcut

Status
Not open for further replies.

redlair2000

Technical User
Sep 15, 2003
17
US
Hello all,

I am trying to create a script that will find and replace a shortcut. We have recently moved a network application to another location. I need a way to find there desktop shortcut (which may be named anything) and change the target path. I put together a script with tid bits from the net. The script does not error out but does not find any shortcuts. Can you look it over and tell me if I am on the right track?

-----------------------------------------------------------
Set oNetwork = CreateObject("WScript.Network")

sTargetStrOld = "\\onsite\EPODist" & oNetwork.UserName

sTargetStrNew = "\\onsite\Images" & oNetwork.UserName

Set oShell = CreateObject("WScript.Shell")
set oFso = CreateObject("Scripting.FilesystemObject")

sPath = oShell.SpecialFolders("Desktop")
ChkLnk(sPath)

sPath = oShell.SpecialFolders("AllUsersDesktop")
ChkLnk(sPath)

Sub ChkLnk (sFolder)
Set oFolder = oFso.GetFolder(sFolder)
Set oFiles = oFolder.Files

For Each oFile In oFiles
If LCase(oFso.GetExtensionName(oFile)) = "lnk" Then
Set oLnk = oShell.CreateShortcut(oFile)
If LCase(oLnk.TargetPath) = LCase(sTargetStrOld) Then
oLnk.TargetPath = sTargetStrNew
oLnk.Save
End If
End If
Next
End Sub

msgbox "Done
 
Your logic is flawed. By not knowing the the actual name of the link you will be changing ANY shortcut to point to your new location.

Your best bet is to simply ask your users to delete their icon and have it get recreated via script.

I hope you find this post helpful.

Regards,

Mark
 
By the look of the targets, you probably meant these?
[tt]sTargetStrOld = "\\onsite\EPODist[red]\[/red]" & oNetwork.UserName
sTargetStrNew = "\\onsite\Images[red]\[/red]" & oNetwork.UserName[/tt]
They are shortcuts to directory?
 

My example was shortcuts to directories, but the actual shortcuts are to a application.
 
If the real shortcuts are something else, the only thing I would say is the script as such is sound. It is run under the credential of the login user (onetwork.username) and for shortcuts on his/her desktop. The only you have to make sure is your sTargetStrOld is actually something you can find on some shortcuts. (Echo it out to see if you are specifying something correct.)
 
Thanks, After Echo it, I removed the (onetwork.username)from the search string and this resolved it. I will be honest, I do not understand the need for onetwork.username.
The script seems to function properly now. What is the best way to have it search all the users desktops?
 
Again I want to point out that your script is enumerating ALL shortcuts and changing them based on extension with no qualifier to determine if it is the actual shortcut you are looking for. You will end up replacing all shortcuts with this new target.

I hope you find this post helpful.

Regards,

Mark
 
If the domain reshuffle is systematic on the name and share, you can do it systematically like this.
[tt]
sTargetStrOld = "\\onsite\EPODist"
sTargetStrNew = "\\onsite\Images"
[/tt]
With the algor inside the looping changed to this.
[tt]
If LCase(oFso.GetExtensionName(oFile[blue].name[/blue])) = "lnk" Then
Set oLnk = oShell.CreateShortcut(oFile[blue].path[/blue])
If instr(1,oLnk.TargetPath,sTargetStrOld,1)<>0 Then
[blue]oLnk.TargetPath = replace(oLnk.TargetPath,sTargetStrOld,sTargetStrNew)[/blue]
oLnk.Save
End If
End If
[/tt]
 
I have restructured the script. What they want is to have a common script were from the Novell login script or a batch file they can call the script and pass on the variable of old and new target paths. Can this be done?

'
' VBScript - Replace target path
'
' - 04/11/06
'
Set oShell = CreateObject("WScript.Shell")

sPath = oShell.SpecialFolders("Desktop")
ReplaceShortcut
'Replace this section with varible from .bat
(sPath, "\\strnorth\SASIxp\SASIxp.exe", "\\strdistrict\Sasixp\SASIxp.exe")
ReplaceShortcut(sPath, "\\strnorth\SASIxp\ClassXP.exe", "\\strdistrict\Sasixp\ClassXP.exe")

sPath = oShell.SpecialFolders("AllUsersDesktop")
ReplaceShortcut(sPath, "\\strnorth\SASIxp\SASIxp.exe", "\\strdistrict\Sasixp\SASIxp.exe")
ReplaceShortcut(sPath, "\\strnorth\SASIxp\ClassXP.exe", "\\strdistrict\Sasixp\ClassXP.exe")

Sub ReplaceShortcut (localFolderToSearch, targetToReplace, replacementTarget)
set oFso = CreateObject("Scripting.FilesystemObject")
Set oFolder = oFso.GetFolder(localFolderToSearch)
Set oFiles = oFolder.Files

For Each oFile In oFiles

If LCase(oFso.GetExtensionName(oFile.name)) = "lnk" Then
Set oLnk = oShell.CreateShortcut(oFile.path)
If instr(1, oLnk.TargetPath, targetToReplace, 1)<>0 Then
oLnk.TargetPath = replace(oLnk.TargetPath, targetToReplace, replacementTarget)
oLnk.Save
End If
End If
Next
End Sub

msgbox "Completed"
WScript.quit

 
This is how you can properly (relatively) address the problem.

[1] Let's say replacetargetpath.vbs.
[tt]
dim sarg1,sarg2,sarg3
with wscript.arguments.named
sarg1=.item("localFolderToSearch")
sarg2=.item("targetToReplace")
sarg3=.item("replacementTarget")
end with
if sarg1="" or sarg2="" or sarg3="" then
wscript.echo "Critical argument(s) not passed. Operation aborted"
else
ReplaceShortcut sarg1,sarg2,sarg3
end if
wscript.echo "Completed"

Sub ReplaceShortcut (localFolderToSearch, targetToReplace, replacementTarget)
dim oFso,oFolder,oFiles,oFile,oLnk
set oFso = CreateObject("Scripting.FilesystemObject")
if oFso.folderExists(localFolderToSearch) then
Set oFolder = oFso.GetFolder(localFolderToSearch)
Set oFiles = oFolder.Files
For Each oFile In oFiles
If LCase(oFso.GetExtensionName(oFile.name)) = "lnk" Then
Set oLnk = oShell.CreateShortcut(oFile.path)
If instr(1, oLnk.TargetPath, targetToReplace, 1)<>0 Then
oLnk.TargetPath = replace(oLnk.TargetPath, targetToReplace, replacementTarget[blue],1,-1,1[/blue])
oLnk.Save
End If
set oLnk=nothing
End If
Next
set oFiles=nothing
set oFolder=nothing
else
'folder does not even exist---do nothing?
end if
set oFso=nothing
End Sub
[/tt]
[2] You call it from a bat file with a line like this, adding full path if necessary.
[tt]
cscript.exe //nologo replacetargetpath.vbs /localFolderToSearch:"c:\xyz\pqr" /targetToReplace:"\\a0b1c2\" /replacementTarget:"\\d7e8f9\"
[/tt]
 
What if I had two diff tartget paths (shortcuts) I need to find and replace?
 
Add another If/Then inside the For/Next

I hope you find this post helpful.

Regards,

Mark
 
What if I have two sets of junk meal burger to eat? Eat one at a time. The same, two lines in the bat file.
 
LOL, thanks for the laugh tsuji!

I hope you find this post helpful.

Regards,

Mark
 
Yes I would, but as stated above I would hard code this and not pass such long arguments. I find that requiring multiple (and long) arguments is the easiest way to introduce errors, plus it sort of defeats the whole automation idea.

I hope you find this post helpful.

Regards,

Mark
 
I should add an emoticon to my question---but I am not a fans of using it, meaning I would like to hear your opinion, Mark, on the really "serious" matter, ie, the eating business! As you indulge in sharing your opinion on the "minor" scripting buisness, it would not be nice on my part to contradite you. Thanks, Mark!
 
I'm sorry tsuji, I am not following what you are saying regarding "serious" and "minor". What "serious" matter are you saying you would like my opinion on?

If it is respect to my opinion on the passing of arguments, I think there is a time and place for them however it is my opinion that we write scripts to help us automate what would otherwise be repeditive tasks. this script in particular has its value in being able to be set as a login script to quickly update a users shortcuts. By requiring an argument, it no longer can server that function, so I have to wonder why not manually update the shortcut if I have to type in the path at a command line. I don't see anything in the above code to execute this on remote systems, so again it seems to me that you would need to execute this script (each time with arguments) on a seperate PC to update the icon. I don't see this as saving the admin or user any time or agrevation.

The script you originally provided before this latest alteration request was great and gave the ability to set it as a login script to ensure each user had the updated link location.

The other thing I don't like about arguments is if the path is very long it is not only tedious to type, but that leaves lots of room for error. Using myself as an example, I type fairly quickly, however I have a problem with transposing letters and with one hand being a little faster than the other so I get spaces in the wrong spot with certain combinations. I am notorious for typing int he instead of in the. Having a long argument only increases my chances of messing that up. Furthermore I'd rather have the script do the work and not me.

While engaged on a project for Microsoft last year another consultant was asked to write some scripts that I later had to test and make change suggestions. The person doing the script writing was really heavy into arguments. As an example in one script you needed to type the domain name as an argument. To me this was unacceptable. The script can automatically determine the RootDSE info and return the domain name without the user having to type it. When possible, I like my scripts to just require a double click.

Hope this explanation helps to explain my point of view.

Regards,

Mark
 
It is I who feel sorry, Mark! I meant be a bit humourous but end up having to bother you to spend more time on this.

I think typing here is not a problem. The op probably want to do somehow at login too. Only that heterogenous system might have them to use batch file instead of vbs file. That is why they ask and that is what I understand their question. It is login batch calling vbs file to do that specific task. So there would be no user typing at the keyboard.

As to the other observation, I wouldn't disagree.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top