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
 
No worries tsuji. :) Humor does not always translate well in typing. I think that is why I've come to like the use of the emoticons which I formerly dismissed as just being "cute."

So if I am following you here, then what you are suggesting is the user would use a batch file to start the script with arguments. I just want to toss out there that the user will then be calling up a VM in order to run the script which does represent a slight performance hit.

Regards,

Mark
 
I started with the serious junk meal thing and you drag me into a debate on scripting... so don't blame me!

I think the ultimate virtue of scripting is the speed of developing and getting the task behind us and move ahead. At the conceiving level, what are the most important: the freedom of creation, the rigour and discipline in the art of making out a good script and the understanding of the problem and its repercussion. There are banalities all over in those web wisdom(s) which good scripters should free themselves from. At the practical level, judicious compromise should be made on stiking the balance of performance and convenience and life-cycle of the scripts so produced. So in the art of scripting, it is like phoenix which always re-create itself. Thousands and thousands of scripts in the repositories and faq's won't help a bit or only a little. So those who only know copy and paste won't succeed or should I say miss the fun. That's all I want to say.
 

Hey guys, I appreciate your help and sorry about all the conflict. As it is the script is working. I will try to add another line in my login script. How can I change the /localFolderToSearch:"c:\xyz\pqr" to search the users desktop. I tried replacing the path to oShell.SpecialFolders("Desktop") but did not work

cscript.exe //nologo replacetargetpath.vbs /localFolderToSearch:"c:\xyz\pqr" /targetToReplace:"\\a0b1c2\" /replacementTarget:"\\d7e8f9\"

Script being used:

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,1,-1,1)
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
 
Didi you remember to define oShell?

I hope you find this post helpful.

Regards,

Mark
 
I tried this and the and the argument isn't being past.

/localFolderToSearch:"oShell.SpecialFolders("Desktop")"


dim sarg1,sarg2,sarg3
Set oShell = CreateObject("WScript.Shell")
with wscript.arguments.named
sarg1=.item("localFolderToSearch")
sarg2=.item("targetToReplace")
sarg3=.item("replacementTarget")
 
try this above the line ReplaceShortcut sarg1,sarg2,sarg3:

Set oShell = CreateObject("WScript.Shell")
uDesktop = oShell.SpecialFolders("Desktop")
If sarg1 ="desktop" Then
sarg1 = uDesktop
End If

Then when passing args just type the word desktop for the first argument.

I hope you find this post helpful.

Regards,

Mark
 
markdmac thank-you for your help. your last reply resolved the Desktop issue. I have one last problem. I also need to search these locations:

C:\Documents and Settings\<USERProfile>\Application Data\Microsoft\Internet Explorer\Quick Launch

C:\Documents and Settings\<USERProfile>\Start Menu\Programs\Accessories

Could I utilize SpecialFolders?
 
You can but it is more involved. Those sub folders don't have a "special folder" equivalent. SO you would have to do something like grab the desktop path as done above. Take the lenght of it minus 7 and append the extra text to make your paths.

Like this:
Code:
Set oShell = CreateObject("WScript.Shell")
uDesktop = oShell.SpecialFolders("Desktop") 
QLPath= Left(uDesktop,(Len(uDesktop)-7))& "Application Data\Microsoft\Internet Explorer\Quick Launch"
WScript.Echo QLPath

I hope you find this post helpful.

Regards,

Mark
 
You have a limiting case of hard coding localFolderToSearch, targetToReplace, replacementTarget explicitly shown. You have a limiting case of soft coding all of them explicitly shown. You cannot make a script out of them with localFolderToSearch being hard-coded, and targetToReplace and replacementTarget soft-coded? That's not the way how this forum work for the best benefit of its members.
 
Again, Thank-you guys. Here are the changes I made.

Set oShell = CreateObject("WScript.Shell")
uDesktop = oShell.SpecialFolders("Desktop")
uquicklaunch = Left(uDesktop,(Len(uDesktop)-7))& "Application Data\Microsoft\Internet Explorer\Quick Launch"
ustartmenu = Left(uDesktop,(Len(uDesktop)-7))& "Start Menu"


If sarg1 ="desktop" Then
sarg1 = uDesktop
End If

If sarg1 ="quicklaunch" Then
sarg1 = uquicklaunch
End If

If sarg1 ="startmenu" Then
sarg1 = ustartmenu
End If

ReplaceShortcut sarg1,sarg2,sarg3
end if
 
Everything is working great. I just had two more requirements added to the function of the script. 1)It needs to be able to search all the user folders. 2)It needs to drop some kind if tag on the machine to force the login script to run once. I have a idea of creating a reg key that the script will look for before running. I will run it by you. Do you have any ideas on searching all profiles? If this is going to be to time consuming I may suggest they reconsider that piece.
 
Any time you do a recursive search it will take more time. Furthermore, if your script is a logoff script then the logged on user is going to need to have rights to the other users folders.

here is an example of how you can do the recursive search on either files or folders:

Code:
'==========================================================================
'
' VBScript Source File -- 
'
' NAME: CleanBadMail.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL   : [URL unfurl="true"]http://www.thespidersparlor.com[/URL]	
' Copywrite (c) 2003 All rights reserved
' DATE  : 09/10/2003
'
' COMMENT: 
'
' This script will list all filtered and quarantined SPAM mail, check that 
' the files are more than 30 days old and then delete them.
' This file is to be scheduled to run each day.
'=====================================

Path1 = "E:\Program Files\Exchsrvr\Mailroot\vsi 1\BadMail"
Path2 = "E:\Program Files\Trend\SMCF\Quarantine"


Dim fso 
Dim oFolder
Dim oFile
Dim oSubFolder

  Set fso = createobject("Scripting.FileSystemObject")
  
   Set oFolder = fso.GetFolder(Path1)
  
  For Each oFile In oFolder.files
   	If DateDiff("d", oFile.DateCreated,Now) > 30 Then
    	oFile.Delete True
    End If
  Next


Set oFolder = fso.GetFolder(Path2)
Set colSubfolders = oFolder.Subfolders

For Each oSubfolder in colSubfolders
   	If DateDiff("d", oSubFolder.DateCreated,Now) > 30 Then
		fso.DeleteFolder(oSubFolder)
	End If
Next

Set oSubFolder = Nothing
Set oFolder = Nothing
Set fso = Nothing



I hope you find this post helpful.

Regards,

Mark
 


Hello again, I have a error that comes up on about 20% of the machines that receive the script via login script. I just started deployment today. The message is as follows:

F:\LOGIN\replacetargetpath.vb3(36, 3)Microsoft VBScript runtime error: ActiveX component can’t create object: ‘Scripting.FilesystemObject’

Any ideas?
I believe the line refrenced is this:
set oFso = CreateObject("Scripting.FilesystemObject")

Here is an example of the argument line from the batch file and the script being used:

cscript.exe //nologo F:\LOGIN\replacetargetpath.vbs /localFolderToSearch:"desktop" /targetToReplace:"\\strdistrict\Sasixp\SASIxp.exe" /replacementTarget:\\strnorth\Sasixp\SASIxp.exe



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
Set oShell = CreateObject("WScript.Shell")
uDesktop = oShell.SpecialFolders("Desktop")
uquicklaunch = Left(uDesktop,(Len(uDesktop)-7))& "Application Data\Microsoft\Internet Explorer\Quick Launch"
ustartmenu = Left(uDesktop,(Len(uDesktop)-7))& "Start Menu"


If sarg1 ="desktop" Then
sarg1 = uDesktop
End If

If sarg1 ="quicklaunch" Then
sarg1 = uquicklaunch
End If

If sarg1 ="startmenu" Then
sarg1 = ustartmenu
End If

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,1,-1,1)
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
 
Make sure these systems have WSH 5.6 installed. That could be the problem.

I hope you find this post helpful.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top