如何编写批量创建快捷方式脚本

wen 实用脚本 2

本文目录导读:

如何编写批量创建快捷方式脚本

  1. 方法1:Windows批处理脚本(.bat)
  2. 方法2:PowerShell脚本(推荐)
  3. 方法3:带参数的动态脚本
  4. 方法4:带界面的版本(VBScript)
  5. 使用方法
  6. 高级功能

我来帮你编写批量创建快捷方式的脚本,以下是几种常见的方法:

方法1:Windows批处理脚本(.bat)

@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion
echo ====================================
echo      批量创建快捷方式脚本
echo ====================================
REM 设置目标文件夹路径
set "TARGET_DIR=C:\你的目标文件夹"
if not exist "%TARGET_DIR%" (
    echo 创建目标文件夹...
    mkdir "%TARGET_DIR%"
)
REM 需要创建快捷方式的目标程序列表
set "programs[0]=C:\Windows\notepad.exe"
set "programs[1]=C:\Windows\system32\calc.exe"
set "programs[2]=C:\Program Files\Internet Explorer\iexplore.exe"
REM 创建快捷方式
for /l %%i in (0,1,2) do (
    if exist "!programs[%%i]!" (
        echo 正在为 !programs[%%i]! 创建快捷方式...
        powershell -Command "$ws = New-Object -ComObject WScript.Shell; $s = $ws.CreateShortcut('%TARGET_DIR%\' + [System.IO.Path]::GetFileNameWithoutExtension('!programs[%%i]!') + '.lnk'); $s.TargetPath = '!programs[%%i]!'; $s.Save()"
    ) else (
        echo 警告:!programs[%%i]! 不存在,跳过...
    )
)
echo.
echo 所有快捷方式创建完成!
pause

方法2:PowerShell脚本(推荐)

# 批量创建快捷方式.ps1
# 定义目标路径
$destinationPath = "$env:USERPROFILE\Desktop\Shortcuts"
# 创建目标文件夹(如果不存在)
if (-not (Test-Path $destinationPath)) {
    New-Item -ItemType Directory -Path $destinationPath
}
# 定义程序列表(键为显示名称,值为目标路径)
$programs = @{
    "记事本" = "C:\Windows\notepad.exe"
    "计算器" = "C:\Windows\system32\calc.exe"
    "命令提示符" = "C:\Windows\system32\cmd.exe"
    "PowerShell" = "C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe"
    # 添加更多程序...
}
# 创建快捷方式
$shell = New-Object -ComObject WScript.Shell
foreach ($name in $programs.Keys) {
    $targetPath = $programs[$name]
    if (Test-Path $targetPath) {
        $shortcutPath = Join-Path $destinationPath "$name.lnk"
        $shortcut = $shell.CreateShortcut($shortcutPath)
        $shortcut.TargetPath = $targetPath
        $shortcut.Save()
        Write-Host "已创建: $name" -ForegroundColor Green
    } else {
        Write-Host "错误: $targetPath 不存在,跳过 $name" -ForegroundColor Red
    }
}
Write-Host "`n所有快捷方式创建完成!" -ForegroundColor Yellow

方法3:带参数的动态脚本

# 动态批量创建快捷方式.ps1
param(
    [string]$SourceFile = "programs.txt",  # 程序列表文件
    [string]$Destination = "$env:USERPROFILE\Desktop\快捷方式"
)
function Create-ShortCut {
    param([string]$TargetPath, [string]$ShortcutPath)
    $shell = New-Object -ComObject WScript.Shell
    $shortcut = $shell.CreateShortcut($ShortcutPath)
    $shortcut.TargetPath = $TargetPath
    $shortcut.Save()
}
# 创建目标文件夹
if (-not (Test-Path $Destination)) {
    New-Item -ItemType Directory -Path $Destination -Force
}
# 读取程序列表文件(格式:程序路径|快捷方式名称)
if (Test-Path $SourceFile) {
    $lines = Get-Content $SourceFile
    foreach ($line in $lines) {
        if ($line -match '^#') { continue }  # 跳过注释行
        $parts = $line -split '\|'
        if ($parts.Count -eq 2) {
            $targetPath = $parts[0].Trim()
            $shortcutName = $parts[1].Trim()
            if (Test-Path $targetPath) {
                $shortcutPath = Join-Path $Destination "$shortcutName.lnk"
                Create-ShortCut -TargetPath $targetPath -ShortcutPath $shortcutPath
                Write-Host "已创建: $shortcutName" -ForegroundColor Green
            } else {
                Write-Host "错误: $targetPath 不存在" -ForegroundColor Red
            }
        }
    }
} else {
    Write-Host "找不到文件: $SourceFile" -ForegroundColor Red
}

方法4:带界面的版本(VBScript)

' batch_shortcut.vbs
Set objShell = CreateObject("WScript.Shell")
' 创建快捷方式列表
CreateShortcut "C:\Windows\notepad.exe", "记事本"
CreateShortcut "C:\Windows\system32\calc.exe", "计算器"
CreateShortcut "C:\Windows\system32\cmd.exe", "命令提示符"
Sub CreateShortcut(TargetPath, ShortcutName)
    If objShell.FileExists(TargetPath) Then
        strDesktop = objShell.SpecialFolders("Desktop")
        Set objShortCut = objShell.CreateShortcut(strDesktop & "\" & ShortcutName & ".lnk")
        objShortCut.TargetPath = TargetPath
        objShortCut.Save
        WScript.Echo "已创建: " & ShortcutName
    Else
        WScript.Echo "错误: " & TargetPath & " 不存在"
    End If
End Sub

使用方法

  1. 保存脚本:将代码保存为相应格式的文件(.bat, .ps1, .vbs)
  2. 运行脚本
    • 批处理:双击运行
    • PowerShell:右键->使用PowerShell运行,或在PowerShell中执行
    • VBScript:双击运行

高级功能

如果想支持更多功能,可以添加:

  • 设置图标 (IconLocation)
  • 设置运行方式(最小化/最大化)
  • 设置快捷方式参数
  • 添加到开始菜单
  • 批量处理子文件夹

需要我详细解释某个部分,或者添加特定功能吗?

抱歉,评论功能暂时关闭!