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 gkittelson 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 argument using VBS

Status
Not open for further replies.

GeorgeAdhaero

IS-IT--Management
Nov 25, 2010
3
GB
Hi,

This is a script I use to update a shortcut argument and it works well apart from if the short cut has allready been updated the find finds the oldserver name in the new string and replaces it.

I kinda know that what I want to do is a regex match ^Oldserver so it only finds it in that format but how would I integrate this into my script ?

cheers

Dim objShell, objFSO, SearcherPath, fileFolder
Dim userFolder, desktopFolder, custFolder, extension
Dim fullname, shortcut, shortTarget

SearcherPath = "C:\Program Files\Searcher\Searcher.exe"

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set fileFolder = objFSO.GetFolder("C:\Documents and Settings")
Set subFileFolder = fileFolder.Subfolders

For Each userFolder in subFileFolder
If objFSO.FolderExists(userFolder & "\Desktop") Then
Set desktopFolder = objFSO.GetFolder(userFolder & "\Desktop")
Set custFolder = desktopFolder.Files

For Each custFile in custFolder
extension = objFSO.GetExtensionName(LCase(custFile.name))
If extension = "lnk" then
'Find full path of shortcut
fullname = objFSO.GetAbsolutePathName(custFile)

'Find full path of target within shortcut
Set shortcut = objShell.CreateShortcut(fullname)
shortTarget = shortcut.TargetPath

If InStr(shortTarget, SearcherPath) then
Dim seachershortcutfile
Set seachershortcutfile = objFSO.GetFile(fullname)
Set oWS = WScript.CreateObject("WScript.Shell")


sLinkFile = seachershortcutfile
Set oLink = oWS.CreateShortcut(sLinkFile)
sFind = "Oldserver"
sReplace = "Newserver\Oldserver"
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

End If
End If
Next
End If
 
[0] This is a measure to stop the replacement to occur upon repeating call of the vbs.
[tt]
ix = instr(oLink.Arguments, sFind)
[red]ix2 = instr(oLink.Arguments, sReplace)[/red]
if ix > 0 [red]and ix2 = 0[/red] then
oLink.Arguments = left(oLink.Arguments,ix-1) & sReplace & mid(oLink.Arguments,ix+len(sFind))
oLink.Save
end if
[/tt]
[1] There are some _very_ casual use default property of file/folder object which are particularly bad taste and easily misleading. In particular, this is very bad taste.

>sLinkFile = seachershortcutfile

Though it works, but I would strongly incline to recommend it should be put in this form.

[tt]sLinkFile = seachershortcutfile[blue].path[/blue][/tt]

[1.1] Other places of the similar use of default property is to a less degree misleading, but I would not write like that.

[2] Since file and folder path or name can easily be case-blind during creation or rename, when testing name or path. Hence, I would suggest replace all instr() to case insensitive version, such as these and others.
[tt]
ix = instr(1, oLink.Arguments, sFind, 1)
ix2 = instr(1, oLink.Arguments, sReplace, 1)
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top