'设置变量必须要声明才能使用 Option Explicit '声明 3 个变量分别表示文件对象、文件和子目录 Dim Fso, Folders '创建文件对象 Set Fso = CreateObject("Scripting.FileSystemObject") '获取当前目录下所有文件,"." 代表当前目录的路径 '这里 GetFolder 返回的是一个 Folder 对象 Set Folders = Fso.GetFolder(".")
'创建 WshShell 对象,用于后面创建快捷方式 Dim WshShell Set WshShell = WScript.CreateObject("WScript.Shell")
'声明一个变长数组,和对应的下标 Dim ShellLink() Dim index index = 0
'为文件设置快捷方式并隐藏原文件 Dim Files, file Set Files = Folders.Files For Each file In Files file.attributes = 0 '设置为隐藏 redim ShellLink(index) '创建快捷方式,快捷方式默认放在当前目录下,名称与原文件一致,后缀为 .lnk Set ShellLink(index) = WshShell.CreateShortcut(".\" & file.Name & ".lnk") ShellLink(index).TargetPath = file '快捷方式的目标路径为原文件 ShellLink(index).IconLocation = file '快捷方式的图标为原文件 ShellLink(index).Save '保存快捷方式 index = index + 1 '保存快捷方式 Next
'为目录设置快捷方式的过程类似 Dim SubFolders, folder Set SubFolders = Folders.SubFolders '获取子目录 For Each folder In SubFolders folder.attributes = 0 redim ShellLink(index) Set ShellLink(index) = WshShell.CreateShortcut(".\" & folder.Name & ".lnk") ShellLink(index).TargetPath = folder ShellLink(index).IconLocation = folder ShellLink(index).Save index = index + 1 Next