Windows 環境下需批次修改「開始功能表」資料夾名稱與桌面捷徑,手動操作繁瑣且易出錯。VBScript 配合 Scripting.FileSystemObject 可輕鬆完成,適合軟體安裝/更新腳本。
Option Explicit
Dim fso, f, fsoGetFolder
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
' === 設定區 ===
Const OldFolderName = "263 Enterprise Messenger"
Const NewFolderName = "Peacemap Messenger"
Const OldShortcut1 = "263 Enterprise Messenger.lnk"
Const NewShortcut1 = "Peacemap Messenger.lnk"
Const OldShortcut2 = "卸載 263 Enterprise Messenger.lnk"
Const NewShortcut2 = "卸載 Peacemap Messenger.lnk"
Const BasePath = "C:\Documents and Settings\All Users\開始功能表\程式集\"
Const NewPath = BasePath & "Peacemap Messenger\"
Const DeskPath = "C:\Documents and Settings\All Users\桌面\"
' ============
' 1. 重命名資料夾
If fso.FolderExists(BasePath & OldFolderName) Then
Set fsoGetFolder = fso.GetFolder(BasePath & OldFolderName)
fsoGetFolder.Name = NewFolderName
WScript.Echo "資料夾重命名完成:" & NewFolderName
End If
' 2. 重命名開始功能表捷徑
If fso.FileExists(NewPath & OldShortcut1) Then
Set f = fso.GetFile(NewPath & OldShortcut1)
f.Name = NewShortcut1
End If
If fso.FileExists(NewPath & OldShortcut2) Then
Set f = fso.GetFile(NewPath & OldShortcut2)
f.Name = NewShortcut2
End If
' 3. 重命名桌面捷徑
If fso.FileExists(DeskPath & OldShortcut1) Then
Set f = fso.GetFile(DeskPath & OldShortcut1)
f.Name = NewShortcut1
End If
MsgBox "所有重命名操作完成!", vbInformation, "完成"
| 物件/方法 | 用途 |
|---|---|
CreateObject("Scripting.FileSystemObject") | 建立檔案系統操作核心物件 |
FolderExists / FileExists | 檢查存在性,避免錯誤 |
GetFolder / GetFile | 取得資料夾/檔案物件參考 |
Folder.Name = 新名稱 | 重命名資料夾 (屬性寫入) |
File.Name = 新名稱 | 重命名檔案/捷徑 |
\\ 明確語意。FileExists 失效。C:\Documents and Settings\All Users 僅適用 XP/Server 2003。Vista+ 應用 %ALLUSERSPROFILE% 或 WScript.Shell 取得特殊資料夾:Dim shell, startMenu, desktop
Set shell = WScript.CreateObject("WScript.Shell")
startMenu = shell.SpecialFolders("AllUsersStartMenu") & "\程式集\"
desktop = shell.SpecialFolders("AllUsersDesktop") & "\"
Dim renameMap
renameMap = Array( _
Array("Old Name 1", "New Name 1"), _
Array("Old Name 2", "New Name 2"), _
Array("舊名稱", "新名稱") _
)
For Each pair In renameMap
Dim oldN, newN
oldN = pair(0); newN = pair(1)
If fso.FileExists(targetPath & oldN & ".lnk") Then
fso.GetFile(targetPath & oldN & ".lnk").Name = newN & ".lnk"
End If
Next
rename_shortcuts.vbs (編碼 UTF-8 with BOM)。cscript rename_shortcuts.vbs (顯示 Console 輸出)。| 錯誤現象 | 可能原因 | 解決 |
|---|---|---|
| 「找不到檔案」 | 路徑錯誤、編碼不符、捷徑實為 .url 非 .lnk | 用 `dir /b` 確認實際檔名;檢查腳本編碼 |
| 權限拒絕 | 目標資料夾需管理員權限 | 以管理員身分執行 cscript/wscript |
| 中文亂碼導致判斷失效 | 腳本檔案編碼非系統預設 | 另存為 ANSI 或 UTF-8 with BOM |
VBScript 雖屬老技術,但在 Windows 系統管理自動化上仍極實用。掌握 FileSystemObject 核心操作,配合環境變數路徑解析與編碼處理,即能編寫穩健的重命名/部署腳本。