WSH的SpecialFolders对象教程
前面我做过一个VBS,来实现添加网站快捷方式到开始菜单,当然你也可以添加到别的地方,这就是那个对像的一些用法返回 SpecialFolders 对象(特殊文件夹集)。
object.SpecialFolders(objWshSpecialFolders)
参数
object
WshShell 对象。
objWshSpecialFolders
特殊文件夹的名称。
说明
WshSpecialFolders 对象是一个集合,其中包含整套 Windows 特殊文件夹,如 “Desktop” 文件夹、”Start Menu” 文件夹和 “Personal Documents” 文件夹。特殊文件夹名称用于索引该集合以检索所需的特殊文件夹。如果请求的文件夹 (strFolderName) 不可用,则 SpecialFolders 属性返回一个空字符串。例如,Windows 95 中没有 AllUsersDesktop 文件夹,如果 strFolderName 是 AllUsersDesktop,则返回一个空字符串。
下面的特殊文件夹可用:
AllUsersDesktop
AllUsersStartMenu
AllUsersPrograms
AllUsersStartup
Desktop
Favorites
Fonts
MyDocuments
NetHood
PrintHood
Programs
Recent
SendTo
StartMenu
Startup
Templates 以下是关于使用 **WSH 的 `SpecialFolders` 对象**的详细教程和示例,帮助您管理特殊文件夹(如桌面、开始菜单等)中的快捷方式:
---
### **1. 基本概念**
- **`SpecialFolders`** 是 `WScript.Shell` 对象的一个属性,返回系统特殊文件夹的路径集合。
- 通过指定文件夹名称(如 `"Desktop"`、`"StartMenu"`),可直接获取其完整路径。
- **典型用途**:创建、删除或管理快捷方式、配置文件等。
---
### **2. 核心步骤**
#### **(1) 创建 `WScript.Shell` 对象**
```vbscript
Set objShell = CreateObject("WScript.Shell")
```
#### **(2) 获取特殊文件夹路径**
```vbscript
sFolderPath = objShell.SpecialFolders("StartMenu")' 例如:获取开始菜单路径
```
#### **(3) 创建快捷方式**
```vbscript
' 定义快捷方式路径
sShortcutPath = sFolderPath & "\MyWebsite.lnk"
' 创建快捷方式对象
Set objShortcut = objShell.CreateShortcut(sShortcutPath)
' 设置快捷方式属性
objShortcut.TargetPath = "http://www.example.com"' 目标网址
objShortcut.IconLocation = "shell32.dll,13" ' 自定义图标(可选)
objShortcut.Description = "访问示例网站" ' 描述(可选)
' 保存快捷方式
objShortcut.Save()
```
---
### **3. 常用特殊文件夹列表**
| 文件夹名称 | 描述 |
|---------------------|---------------------------|
| `Desktop` | 当前用户桌面 |
| `AllUsersDesktop` | 所有用户的桌面(需管理员权限)|
| `StartMenu` | 当前用户的开始菜单 |
| `AllUsersStartMenu` | 所有用户的开始菜单(需管理员权限)|
| `Programs` | 当前用户的开始菜单程序组|
| `Startup` | 当前用户的启动文件夹 |
| `MyDocuments` | 我的文档 |
| `Favorites` | 收藏夹(浏览器) |
---
### **4. 完整示例脚本**
#### **将网站快捷方式添加到当前用户的桌面和开始菜单**
```vbscript
Set objShell = CreateObject("WScript.Shell")
' 添加到桌面
sDesktopPath = objShell.SpecialFolders("Desktop")
CreateWebsiteShortcut objShell, sDesktopPath, "我的网站"
' 添加到开始菜单
sStartMenuPath = objShell.SpecialFolders("StartMenu")
CreateWebsiteShortcut objShell, sStartMenuPath, "我的网站"
Sub CreateWebsiteShortcut(objShell, sFolder, sName)
sShortcutPath = sFolder & "\" & sName & ".lnk"
Set objShortcut = objShell.CreateShortcut(sShortcutPath)
objShortcut.TargetPath = "http://www.example.com"
objShortcut.IconLocation = "shell32.dll,13"
objShortcut.Save()
End Sub
```
---
### **5. 错误处理与注意事项**
#### **(1) 检查文件夹是否存在**
```vbscript
sFolder = objShell.SpecialFolders("AllUsersDesktop")
If sFolder = "" Then
MsgBox "AllUsersDesktop 不支持当前系统!", vbExclamation
WScript.Quit
End If
```
#### **(2) 管理员权限**
- 修改 `AllUsers*` 文件夹(如所有用户的桌面)需要以管理员身份运行脚本。
- 在VBS中,可通过 manifest 或外部工具提权,但通常建议手动以管理员身份运行。
#### **(3) 路径斜杠处理**
- 使用 `& "\"` 拼接路径时,确保文件夹名不以反斜杠结尾(多数系统自动处理)。
---
### **6. 高级用法**
#### **(1) 枚举所有特殊文件夹**
```vbscript
Set objShell = CreateObject("WScript.Shell")
arrFolders = Array("Desktop", "StartMenu", "MyDocuments", "Startup", "AllUsersDesktop")
For Each sFolder In arrFolders
sPath = objShell.SpecialFolders(sFolder)
If sPath <> "" Then
WScript.Echo sFolder & ": " & sPath
Else
WScript.Echo sFolder & ": 不支持"
End If
Next
```
#### **(2) 删除快捷方式**
```vbscript
sDesktopPath = objShell.SpecialFolders("Desktop")
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(sDesktopPath & "\MyWebsite.lnk") Then
objFSO.DeleteFile sDesktopPath & "\MyWebsite.lnk"
End If
```
---
### **7. 常见问题**
**Q:为什么 `AllUsersDesktop` 返回空路径?**
A:请检查操作系统版本和权限。Windows家庭版可能不支持某些系统级文件夹。
**Q:快捷方式图标不显示?**
A:确保 `IconLocation` 格式正确(如 `"C:\Path\icon.ico,0"`),或使用系统内置图标。
---
通过上述方法,您可以灵活管理不同系统位置的快捷方式。根据需求调整目标路径和文件夹名称即可! 您提到的`WshShell.SpecialFolders`对象在VBScript中非常实用,它可以帮助我们快速定位Windows系统中的各类特殊文件夹路径。以下是详细的用法说明和完整代码示例:
---
### ⚡ 基础用法示例
```vbscript
Set WshShell = CreateObject("WScript.Shell")
' 获取当前用户桌面路径
DesktopPath = WshShell.SpecialFolders("Desktop")
MsgBox "桌面路径: " & DesktopPath
' 获取开始菜单程序目录
StartMenuPrograms = WshShell.SpecialFolders("Programs")
MsgBox "开始菜单程序路径: " & StartMenuPrograms
```
---
### 📁 所有特殊文件夹列表
以下是Windows系统支持的主要特殊文件夹名称及其用途:
| 文件夹名称 | 典型路径 | 适用系统 |
|---------------------|------------------------------------------|---------------------|
| AllUsersDesktop | C:\Users\Public\Desktop | Win7及以上 |
| AllUsersStartMenu | C:\ProgramData\Microsoft\Windows\Start Menu | Win7及以上 |
| Desktop | C:\Users\[用户名]\Desktop | 所有系统 |
| MyDocuments | C:\Users\[用户名]\Documents | 所有系统 |
| Startup | C:\Users\[用户名]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup | 所有系统 |
| Favorites | C:\Users\[用户名]\Favorites | 所有系统 |
| Fonts | C:\Windows\Fonts | 所有系统 |
---
### 🛠️ 实战:创建网站快捷方式到不同位置
```vbscript
Set WshShell = CreateObject("WScript.Shell")
' 选择要存放快捷方式的目录
strFolder = WshShell.SpecialFolders("Desktop")' ← 修改这里的文件夹名称
' 创建快捷方式对象
Set oShellLink = WshShell.CreateShortcut(strFolder & "\知乎网站.url")
' 配置快捷方式属性
oShellLink.TargetPath = "https://www.zhihu.com"
oShellLink.IconLocation = "shell32.dll,13"' 使用系统默认IE图标
oShellLink.Save
MsgBox "快捷方式已创建到:" & strFolder
```
---
### ⚠️ 注意事项
1. **权限问题**
- 修改`AllUsers`开头的目录需要管理员权限
- 解决方法:右键VBS文件 → 以管理员身份运行
2. **空值处理**
```vbscript
strPath = WshShell.SpecialFolders("AllUsersDesktop")
If strPath = "" Then
MsgBox "当前系统不支持此文件夹!"
End If
```
3. **路径验证技巧**
可以在资源管理器中直接粘贴路径进行验证:
```cmd
:: 快速打开启动目录
explorer shell:startup
```
---
### 🌟 扩展应用
**批量创建多个快捷方式**
```vbscript
Set WshShell = CreateObject("WScript.Shell")
basePath = WshShell.SpecialFolders("MyDocuments") & "\常用工具\"
websites = Array("知乎|https://zhihu.com", "GitHub|https://github.com")
For Each site In websites
arr = Split(site, "|")
Set lnk = WshShell.CreateShortcut(basePath & arr(0) & ".url")
lnk.TargetPath = arr(1)
lnk.Save
Next
```
**获取所有可用文件夹路径**
```vbscript
Set WshShell = CreateObject("WScript.Shell")
folders = Array("Desktop", "StartMenu", "MyDocuments", "Startup", "Fonts")
For Each f In folders
path = WshShell.SpecialFolders(f)
If path <> "" Then
WScript.Echo f & " → " & path
End If
Next
```
---
通过灵活运用`SpecialFolders`对象,您可以轻松实现各种系统级路径操作。建议保存常用路径到变量中重复使用,并根据不同Windows版本做好兼容性测试。 您提到的 `WshShell.SpecialFolders` 对象在 VBScript 中非常实用,它可以通过简单的代码访问 Windows 系统的特殊文件夹路径(如桌面、开始菜单、启动项等)。以下是详细的使用教程和示例代码:
---
### **1. 基本语法**
```vbscript
Set WshShell = CreateObject("WScript.Shell")
FolderPath = WshShell.SpecialFolders("FolderName")
```
- **`FolderName`** 是特殊文件夹的名称(如 `"Desktop"`, `"StartMenu"` 等)。
- 如果文件夹不存在(如旧系统不支持某些文件夹),会返回空字符串 `""`。
---
### **2. 支持的特殊文件夹列表**
以下是常用的特殊文件夹名称及其对应路径:
| 名称 | 描述 |
|---------------------|--------------------------|
| `Desktop` | 当前用户的桌面 |
| `AllUsersDesktop` | 所有用户的桌面 |
| `StartMenu` | 当前用户的开始菜单 |
| `AllUsersStartMenu` | 所有用户的开始菜单 |
| `Programs` | 当前用户的开始菜单程序组 |
| `Startup` | 当前用户的启动文件夹 |
| `MyDocuments` | 我的文档 |
| `Favorites` | 收藏夹 |
| `Fonts` | 字体文件夹 |
| `SendTo` | "发送到"文件夹 |
---
### **3. 使用示例**
#### **示例1:获取并打印桌面路径**
```vbscript
Set WshShell = CreateObject("WScript.Shell")
DesktopPath = WshShell.SpecialFolders("Desktop")
If DesktopPath <> "" Then
MsgBox "桌面路径: " & DesktopPath
Else
MsgBox "未找到桌面路径!"
End If
```
#### **示例2:创建网站快捷方式到当前用户的启动项**
```vbscript
Set WshShell = CreateObject("WScript.Shell")
StartupPath = WshShell.SpecialFolders("Startup")
If StartupPath <> "" Then
Set Shortcut = WshShell.CreateShortcut(StartupPath & "\MyWebsite.url")
Shortcut.TargetPath = "https://www.example.com"
Shortcut.Save
MsgBox "快捷方式已添加到启动项!"
Else
MsgBox "启动文件夹不可用!"
End If
```
---
### **4. 完整实战:添加快捷方式到不同位置**
以下代码允许用户选择目标文件夹并创建快捷方式:
```vbscript
Set WshShell = CreateObject("WScript.Shell")
' 定义要操作的特殊文件夹名称
folderNames = Array("Desktop", "StartMenu", "MyDocuments", "Startup")
For Each folderName In folderNames
folderPath = WshShell.SpecialFolders(folderName)
If folderPath <> "" Then
' 创建 .url 快捷方式
Set urlShortcut = WshShell.CreateShortcut(folderPath & "\Example Website.url")
urlShortcut.TargetPath = "https://www.example.com"
urlShortcut.Save
' 创建 .lnk 程序快捷方式(如记事本)
Set lnkShortcut = WshShell.CreateShortcut(folderPath & "\Notepad.lnk")
lnkShortcut.TargetPath = "C:\Windows\System32\notepad.exe"
lnkShortcut.WorkingDirectory = "C:\"
lnkShortcut.Description = "打开记事本"
lnkShortcut.Save
End If
Next
MsgBox "快捷方式已成功添加到所有支持的文件夹!"
```
---
### **5. 注意事项**
1. **权限问题**:修改 `AllUsers` 开头的文件夹(如 `AllUsersDesktop`)可能需要管理员权限。
2. **兼容性**:部分文件夹(如 `AllUsersStartMenu`)在 Windows 家庭版中可能不可用。
3. **错误处理**:始终检查返回路径是否为空,避免因路径无效导致脚本崩溃。
---
通过 `SpecialFolders` 对象,您可以轻松实现文件、快捷方式的自动化管理。如果需要更复杂的操作(如操作注册表或环境变量),可以进一步研究 `WshShell` 对象的其他方法(如 `RegWrite`、`ExpandEnvironmentStrings`)。 你提到的 `WshShell.SpecialFolders` 对象在 Windows 脚本中非常实用,可以快速获取系统特殊文件夹路径。以下是结合你需求的详细用法说明和代码示例:
---
### 核心用法说明
1. **创建对象**
```vbscript
Set WshShell = CreateObject("WScript.Shell")
```
2. **获取路径**
```vbscript
strPath = WshShell.SpecialFolders("FolderName")
```
3. **验证路径**
当请求的文件夹不存在时,返回空字符串,建议先校验:
```vbscript
If strPath = "" Then
WScript.Echo "特殊文件夹不存在!"
End If
```
---
### 完整代码示例
```vbscript
' 创建网站快捷方式到指定特殊文件夹
Set WshShell = CreateObject("WScript.Shell")
' 示例1:创建到当前用户桌面
CreateShortcut WshShell, "Desktop", "https://example.com", "我的网站"
' 示例2:创建到所有用户启动项(需管理员权限)
CreateShortcut WshShell, "AllUsersStartup", "notepad.exe", "开机自启记事本"
Sub CreateShortcut(WshShell, folderName, targetPath, shortcutName)
On Error Resume Next ' 忽略错误
' 获取特殊文件夹路径
folderPath = WshShell.SpecialFolders(folderName)
If folderPath = "" Then
WScript.Echo folderName & " 不存在或不可访问!"
Exit Sub
End If
' 创建快捷方式
Set shortcut = WshShell.CreateShortcut(folderPath & "\" & shortcutName & ".lnk")
shortcut.TargetPath = targetPath
shortcut.Save
If Err.Number = 0 Then
WScript.Echo "快捷方式已创建到:" & folderPath
Else
WScript.Echo "创建失败,原因:" & Err.Description
End If
End Sub
```
---
### 常用特殊文件夹对照表
| 文件夹名称 | 典型路径 (Win10) |
|--------------------|------------------------------------------|
| `Desktop` | C:\Users\用户名\Desktop |
| `AllUsersDesktop`| C:\Users\Public\Desktop |
| `StartMenu` | C:\Users\用户名\AppData\Roaming\Microsoft\Windows\Start Menu |
| `AllUsersStartMenu`| C:\ProgramData\Microsoft\Windows\Start Menu |
| `Startup` | C:\Users\用户名\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup |
| `MyDocuments` | C:\Users\用户名\Documents |
| `Programs` | C:\Users\用户名\AppData\Roaming\Microsoft\Windows\Start Menu\Programs |
---
### 注意事项
1. **权限问题**
`AllUsers` 开头的文件夹需要管理员权限才能写入,脚本运行时可能需要右键选择"以管理员身份运行"。
2. **系统差异**
- `AllUsersDesktop` 在家庭版 Windows 中可能不可用
- `NetHood`/`PrintHood` 主要用于网络打印机场景
3. **快捷方式类型**
`.lnk` 用于程序/文件,`.url` 用于网址(创建方式不同):
```vbscript
' 创建网站快捷方式(.url)
Set fso = CreateObject("Scripting.FileSystemObject")
Set urlFile = fso.CreateTextFile(folderPath & "\网站.url")
urlFile.WriteLine ""
urlFile.WriteLine "URL=https://example.com"
urlFile.Close
```
---
### 高级技巧
1. **遍历所有特殊文件夹**
```vbscript
For Each folderName In Array("Desktop", "StartMenu", "MyDocuments", "Startup")
WScript.Echo folderName & ": " & WshShell.SpecialFolders(folderName)
Next
```
2. **兼容性处理**
```vbscript
' 优先尝试 AllUsersDesktop,失败则用当前用户桌面
folderPath = WshShell.SpecialFolders("AllUsersDesktop")
If folderPath = "" Then
folderPath = WshShell.SpecialFolders("Desktop")
End If
```
希望这些示例能帮助你更灵活地管理系统快捷方式!如果遇到具体问题欢迎继续交流。 您提到的 `WshShell.SpecialFolders` 对象非常实用,它可以帮助您快速定位系统特殊文件夹(如桌面、开始菜单、我的文档等)。以下是一个详细的教程,结合您的需求(添加快捷方式到不同位置),提供示例代码和关键说明。
---
### **1. 核心概念**
- **`WshShell.SpecialFolders`** 是 Windows Script Host (WSH) 提供的一个集合,包含系统预定义的特殊文件夹路径。
- 通过指定文件夹名称(如 `"Desktop"`、`"StartMenu"`),可以直接获取其完整路径。
- **兼容性**:不同系统版本可能支持不同的特殊文件夹(如 `AllUsersDesktop` 在 Win95 不可用)。
---
### **2. 常用特殊文件夹列表**
| 文件夹名称 | 说明 |
|---------------------|-------------------------------|
| `Desktop` | 当前用户的桌面 |
| `AllUsersDesktop` | 所有用户的桌面(需管理员权限)|
| `StartMenu` | 当前用户的开始菜单 |
| `AllUsersStartMenu` | 所有用户的开始菜单 |
| `Programs` | 当前用户的“程序”菜单项 |
| `Startup` | 当前用户的启动文件夹 |
| `MyDocuments` | 我的文档 |
| `Favorites` | 收藏夹 |
| `Fonts` | 系统字体文件夹 |
---
### **3. 基础用法示例**
#### **获取路径**
```vbscript
Set WshShell = WScript.CreateObject("WScript.Shell")
' 获取当前用户桌面路径
desktopPath = WshShell.SpecialFolders("Desktop")
' 获取所有用户的开始菜单路径(需权限)
allUsersStartMenu = WshShell.SpecialFolders("AllUsersStartMenu")
```
#### **错误处理**
```vbscript
folderName = "AllUsersDesktop"
path = WshShell.SpecialFolders(folderName)
If path = "" Then
WScript.Echo "文件夹 " & folderName & " 不存在或不可访问!"
WScript.Quit
End If
```
---
### **4. 实战:添加快捷方式到指定位置**
以下脚本将网站快捷方式添加到 **当前用户的“启动”文件夹**(开机自启):
```vbscript
Set WshShell = WScript.CreateObject("WScript.Shell")
' 获取启动文件夹路径
startupPath = WshShell.SpecialFolders("Startup")
If startupPath = "" Then
WScript.Echo "启动文件夹不可用!"
WScript.Quit
End If
' 创建快捷方式对象
Set shortcut = WshShell.CreateShortcut(startupPath & "\MyWebsite.lnk")
' 设置快捷方式属性
shortcut.TargetPath = "https://www.example.com"' 目标网址
shortcut.IconLocation = "C:\Windows\System32\SHELL32.dll,13"' 自定义图标(可选)
shortcut.Save
WScript.Echo "快捷方式已添加到启动文件夹!"
```
---
### **5. 扩展应用**
#### **添加到所有用户的桌面**
```vbscript
allUsersDesktop = WshShell.SpecialFolders("AllUsersDesktop")
If allUsersDesktop <> "" Then
Set shortcut = WshShell.CreateShortcut(allUsersDesktop & "\Example Site.lnk")
shortcut.TargetPath = "https://www.example.com"
shortcut.Save
Else
WScript.Echo "无法访问所有用户桌面!"
End If
```
#### **批量创建到不同位置**
```vbscript
folders = Array("Desktop", "StartMenu", "MyDocuments")
For Each folder In folders
path = WshShell.SpecialFolders(folder)
If path <> "" Then
Set scut = WshShell.CreateShortcut(path & "\MyLink.lnk")
scut.TargetPath = "notepad.exe"
scut.Save
End If
Next
```
---
### **6. 注意事项**
1. **权限问题**:写入 `AllUsers*` 文件夹可能需要管理员权限。
2. **路径验证**:始终检查 `SpecialFolders` 返回的路径是否有效。
3. **跨平台兼容**:部分文件夹在旧系统(如 WinXP)中名称不同,需测试。
---
通过灵活使用 `SpecialFolders`,您可以轻松管理快捷方式和文件在系统关键位置的部署。如果需要更复杂的逻辑(如环境变量解析),可结合 `WshShell.ExpandEnvironmentStrings` 方法使用。
页:
[1]