Friday, February 25, 2011

Batch edit shortcut targets with a VB Script

On some file servers, users create multiple shotcuts pointing to different locations on the same server.
When migrating data from this server to another, these shorcuts are deprecated, so you need to update them and you can have thousands...
Here is a VB script that let you batch edit these shortcuts so that you can keep all of them updated.

Dim objFSO

Const srcServer = "\\formerServer\"
Const dstServer = "\\newServer\"

args = WScript.Arguments.Count

If args < 1 then
WScript.Echo "usage: cscript replaceLinkTarget.vbs [folder]"
WScript.Quit
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")

Call FindEditShortcut(WScript.Arguments.Item(0))


Sub FindEditShortcut(currentDrive)
Dim objFile
Dim objFolder

For Each objFolder In objFSO.GetFolder(currentDrive).SubFolders
For Each objFile In objFolder.Files
'Check extension
If LCase(objFSO.GetExtensionName(objFile)) = "lnk" Then
Set objShell = CreateObject("WScript.Shell")
Set shortcut = objShell.CreateShortcut(objFile.path)
target = LCase(shortcut.TargetPath)

If (InStr(target, srcServer) > 0) Then
target = replace(target,srcServer,dstServer)
Wscript.Echo "New target : " & target
shortcut.TargetPath = target
shortcut.Save
End If
End If
Next

Call FindEditShortcut(objFolder)
Next
End Sub

3 comments:

  1. If you can give a working script, that would be nice.

    For example I have a folder that I want to process all .lnk files in and run this against it

    ReplyDelete
  2. Hello,
    The script is now updated.
    The previous version was incomplete. Sorry about that

    ReplyDelete
  3. I'm not sure if you will see this as your script was posted in 2011. I have server changing from one IP to another, and shortcuts were used for everything. A recent search showed over 10K lnk files that need to be updated. Your program script worked AWESOME! Thank you so much, you have no idea how much time it has saved me!!!!!!

    ReplyDelete