Hey, gang...have another cry for help. I have the script below that does what I need, tweaked it to work in my environment, but am stumped on how to change the description of the shortcut.
This does what it should, changing the mapping of the shortcut (in this case on the user's desktop), but the shortcut will still have the old servername in the description, i.e., "Migration Data (oldserverfs01)" instead of the server name path it was changed to "(Migration Data (newserverfs01)", for example.
Also, if you could offer me some help, how do I add multiple locations to search for shortcuts on the client machine? Right now I can only get it to look at their desktop. I'd like to look, say also in their "MyDocuments" and maybe some other places, if possible?
Much thanks in advance for any help! I'm struggling over here! Ugggghh
Script to search and change path of shortcut to new server location, but does not change 'shortcut name'
Dim SearchFor : SearchFor = "\\old_server_name\"
Dim ReplaceWith : ReplaceWith = "\\new_server_name\"
Dim DirectoryPath : DirectoryPath = GetSpecialFolderPath("Desktop")
EnumFolder DirectoryPath
Sub EnumFolder(DirectoryPath)
Dim FSO, Folder, Files, File, ShortcutPath, ShortcutTargetPath
Set FSO = CreateObject("Scripting.FileSystemObject")
If NOT FSO.FolderExists(DirectoryPath) Then Exit Sub
Set Folder = FSO.GetFolder(DirectoryPath) : Set Files = Folder.Files
For Each File in Files
If IsShortcut(File.Path) Then
ShortcutPath = File.Path
ShortcutTargetPath = GetShortcutTargetPath(File.Path)
If ValueMatchesSearchPattern(ShortcutTargetPath, SearchFor) Then
ShortcutTargetPath = GetNewShortcutTargetPath(ShortcutTargetPath)
Call SetShortcutTargetPath(ShortcutPath, ShortcutTargetPath)
End If
End If
Next
Set File = Nothing
Set Files = Nothing
Set Folder = Nothing
Set FSO = Nothing
End Sub
Function ValueMatchesSearchPattern(SearchValue, SearchPattern)
Dim i
ValueMatchesSearchPattern = False
If Instr(1, SearchValue, SearchPattern, 1) > 0 Then
ValueMatchesSearchPattern = True
End If
End Function
Function GetNewShortcutTargetPath(ShortcutTargetPath)
GetNewShortcutTargetPath = Replace(ShortcutTargetPath, SearchFor, ReplaceWith, 1, 1, 1)
End Function
Function IsShortcut(FilePath)
Dim FileExtension
IsShortcut = False
FileExtension = UCase(Mid(FilePath, InStrRev(FilePath, ".")+1))
If "LNK" = FileExtension Then IsShortcut = True
End Function
Function GetSpecialFolderPath(SpecialFolder)
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
GetSpecialFolderPath = WshShell.SpecialFolders(SpecialFolder)
Set WshShell = Nothing
End Function
Function GetShortcutTargetPath(ShortcutPath)
Dim WshShell, Shortcut
Set WshShell = CreateObject("WScript.Shell")
Set Shortcut = WshShell.CreateShortcut(ShortcutPath)
GetShortcutTargetPath = Shortcut.TargetPath
Set Shortcut = Nothing
Set WshShell = Nothing
End Function
Function SetShortcutTargetPath(ShortcutPath, ShortcutTargetPath)
Dim WshShell, Shortcut
Set WshShell = CreateObject("WScript.Shell")
Set Shortcut = WshShell.CreateShortcut(ShortcutPath)
Shortcut.TargetPath = ShortcutTargetPath
Shortcut.Save
Set Shortcut = Nothing
Set WshShell = Nothing
End Function
This does what it should, changing the mapping of the shortcut (in this case on the user's desktop), but the shortcut will still have the old servername in the description, i.e., "Migration Data (oldserverfs01)" instead of the server name path it was changed to "(Migration Data (newserverfs01)", for example.
Also, if you could offer me some help, how do I add multiple locations to search for shortcuts on the client machine? Right now I can only get it to look at their desktop. I'd like to look, say also in their "MyDocuments" and maybe some other places, if possible?
Much thanks in advance for any help! I'm struggling over here! Ugggghh
Script to search and change path of shortcut to new server location, but does not change 'shortcut name'
Dim SearchFor : SearchFor = "\\old_server_name\"
Dim ReplaceWith : ReplaceWith = "\\new_server_name\"
Dim DirectoryPath : DirectoryPath = GetSpecialFolderPath("Desktop")
EnumFolder DirectoryPath
Sub EnumFolder(DirectoryPath)
Dim FSO, Folder, Files, File, ShortcutPath, ShortcutTargetPath
Set FSO = CreateObject("Scripting.FileSystemObject")
If NOT FSO.FolderExists(DirectoryPath) Then Exit Sub
Set Folder = FSO.GetFolder(DirectoryPath) : Set Files = Folder.Files
For Each File in Files
If IsShortcut(File.Path) Then
ShortcutPath = File.Path
ShortcutTargetPath = GetShortcutTargetPath(File.Path)
If ValueMatchesSearchPattern(ShortcutTargetPath, SearchFor) Then
ShortcutTargetPath = GetNewShortcutTargetPath(ShortcutTargetPath)
Call SetShortcutTargetPath(ShortcutPath, ShortcutTargetPath)
End If
End If
Next
Set File = Nothing
Set Files = Nothing
Set Folder = Nothing
Set FSO = Nothing
End Sub
Function ValueMatchesSearchPattern(SearchValue, SearchPattern)
Dim i
ValueMatchesSearchPattern = False
If Instr(1, SearchValue, SearchPattern, 1) > 0 Then
ValueMatchesSearchPattern = True
End If
End Function
Function GetNewShortcutTargetPath(ShortcutTargetPath)
GetNewShortcutTargetPath = Replace(ShortcutTargetPath, SearchFor, ReplaceWith, 1, 1, 1)
End Function
Function IsShortcut(FilePath)
Dim FileExtension
IsShortcut = False
FileExtension = UCase(Mid(FilePath, InStrRev(FilePath, ".")+1))
If "LNK" = FileExtension Then IsShortcut = True
End Function
Function GetSpecialFolderPath(SpecialFolder)
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
GetSpecialFolderPath = WshShell.SpecialFolders(SpecialFolder)
Set WshShell = Nothing
End Function
Function GetShortcutTargetPath(ShortcutPath)
Dim WshShell, Shortcut
Set WshShell = CreateObject("WScript.Shell")
Set Shortcut = WshShell.CreateShortcut(ShortcutPath)
GetShortcutTargetPath = Shortcut.TargetPath
Set Shortcut = Nothing
Set WshShell = Nothing
End Function
Function SetShortcutTargetPath(ShortcutPath, ShortcutTargetPath)
Dim WshShell, Shortcut
Set WshShell = CreateObject("WScript.Shell")
Set Shortcut = WshShell.CreateShortcut(ShortcutPath)
Shortcut.TargetPath = ShortcutTargetPath
Shortcut.Save
Set Shortcut = Nothing
Set WshShell = Nothing
End Function