China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-18 19:34
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] How to modify the path of a shortcut View 1,948 Replies 2
Original Poster Posted 2009-10-17 08:40 ·  中国 江苏 扬州 电信
高级用户
★★★
Credits 620
Posts 329
Joined 2007-12-05 15:34
18-year member
UID 104754
Gender Male
Status Offline
Want to batch modify the paths of all shortcuts in the current folder through a BAT, as follows:

Original path pointed to by shortcuts:

1、D:\Application Software\cad2004\acad.exe
2、D:\Application Software\asd\a.exe
......

Change their paths through BAT to:

1、E:\Applications\cad2004\acad.exe
2、E:\Applications\asd\a.exe
......

(That is, the drive letter or the previous folder path may change, but the final EXE name remains unchanged)

Please, experts, masters, give me some pointers...

Thank you
Floor 2 Posted 2009-10-17 20:50 ·  中国 天津 联通
高级用户
★★★
据说是李先生
Credits 609
Posts 400
Joined 2008-04-23 15:55
18-year member
UID 116706
Gender Male
Status Offline
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

Attachments
ChangeShortcutTargetPath.zip (1019 bytes, Downloads: 25)
Floor 3 Posted 2009-10-17 22:23 ·  中国 上海 电信
新手上路
Credits 12
Posts 10
Joined 2009-10-04 12:03
16-year member
UID 152600
Gender Male
From 海南
Status Offline
start d:\d\d.exe
Hehe! How nice it is to use this instead of the shortcut
Forum Jump: