The LNK shortcut is not in text format. I don't know if BAT works. I only know how to modify it with VBSCRIPT.
In order to meet greater flexibility, the modification of the path adopts regular expressions, which can meet more needs.
For example, from D:\Application Software\cad2004\acad.exe to E:\Applications\cad2004\Copy acad.exe
OldPathFormat = "^(D\:\\Application Software\\)(.*\\)(*)$"
NewPathFormat = "E:\Applications\$2\Copy $3"
The following is the code for your requirement.
' The folder to be processed. If it is "", it means the current folder.
Folder = ""
' The path format before and after replacement, here regular expressions are used, which can adapt to various complex transformation methods.
' This program will only modify the shortcuts whose target path format matches OldPathFormat.
OldPathFormat = "^(D\:\\Application Software\\)(.*)$"
NewPathFormat = "E:\Applications\$2"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Folder = "" Then Folder = objFSO.GetFile(WScript.ScriptFullName).ParentFolder
ChangeFilesUnderTheFolder Folder
' Process all.lnk shortcut files under the folder (excluding subfolders)
Function ChangeFilesUnderTheFolder(TheFolder)
With objFSO.GetFolder(TheFolder)
For Each Subfile in .Files
If LCase(Right(Subfile.Name, 4)) = ".lnk" Then
ChangeShortcutTargetPath(Subfile.Path)
End If
Next
End With
MsgBox "All shortcut files under the folder “" & TheFolder & "” (except those that cannot be modified) have been processed!", 4160, "Completed"
End Function
' Modify the target of the shortcut file
Function ChangeShortcutTargetPath(ShortcutFile)
On Error Resume Next
With CreateObject("WScript.Shell").CreateShortCut(ShortcutFile)
.TargetPath = ConvertTargetPath(.TargetPath, OldPathFormat, NewPathFormat)
.Save
End With
If Err.Number<>0 Then
MsgBox "Cannot modify the target of “" & ShortcutFile & "”!", 4112, "Error"
Err.Number = 0
End If
End Function
' Replace the old path with the new path (if it does not match the defined format, it will not be replaced)
Function ConvertTargetPath(OldTargetPath, OldPattern, NewPattern)
Dim tempStr
Set regEx = New RegExp
regEx.Pattern = OldPattern
If regEx.Test(OldTargetPath) Then
tempStr = regEx.Replace(OldTargetPath, NewPattern)
Else
tempStr = OldTargetPath
End If
ConvertTargetPath = tempStr
End Function