LNK 快界方式不是文本格式的,我不知道BAT行不行,只会用VBSCRIPT修改。
为了满足更大的灵活性,路径的修改采用的正则表达式,可以满足更多的需求。
比如从 D:\应用软件\cad2004\acad.exe 到 E:\应用\cad2004\复件 acad.exe
OldPathFormat = "^(D\:\\应用软件\\)(.*\\)(*)$"
NewPathFormat = "E:\应用\$2\复件 $3"
下面是针对你需求的代码。
' 要处理的文件夹,如果是 "" 则代表当前文件夹。
Folder = ""
' 替换之前和之后的路径格式,这里采用正则表达式,可适应各种复杂的变换方式。
' 本程序只会修改符合指向路径格式符合 OldPathFormat 的快捷方式。
OldPathFormat = "^(D\:\\应用软件\\)(.*)$"
NewPathFormat = "E:\应用\$2"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Folder = "" Then Folder = objFSO.GetFile(WScript.ScriptFullName).ParentFolder
ChangeFilesUnderTheFolder Folder
' 处理文件夹下(不含子文件夹)所有的 .lnk 快捷方式文件
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 "文件夹 “" & TheFolder & "” 下所有快捷方式文件(除了提示不能修改的)已处理完毕!", 4160, "完成"
End Function
' 修改快捷方式文件的指向
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 "不能修改 “" & ShortcutFile & "” 的指向!", 4112, "错误"
Err.Number = 0
End If
End Function
' 将旧的路径替换成新的路径(如果不符合定义的格式则不替换)
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
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.
Folder = ""
OldPathFormat = "^(D\:\\Application Software\\)(.*)$"
NewPathFormat = "E:\Applications\$2"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Folder = "" Then Folder = objFSO.GetFile(WScript.ScriptFullName).ParentFolder
ChangeFilesUnderTheFolder Folder
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
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
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