如何创建批处理文件或脚本来创建 EXE 的多个快捷方式

如何创建批处理文件或脚本来创建 EXE 的多个快捷方式

您好,我有大约 122 个文件夹,每个文件夹中都有与文件夹名称匹配的 exe 文件。有没有办法编写脚本来为文件夹内的每个 exe 文件创建快捷方式,并且不带“快捷方式”标签。只是想通过一些巧妙的脚本来避免手动逐个执行此操作...

答案1

rem must be run in a window with admin privileges
rem windows 10 cmd batch file to create a hard shortcut to all .exe files on a volume if placed in the root folder; 
rem in a folder and all subfolders if not placed in the root folder. the shortcut is created in the folder where the .exe is.
rem the "shortcut" extension should not be part of the filename.  the .lnk extension should be part of the filename.  
rem links that already exist to .exe files should be deleted.
rem see mklink /? and https://ss64.com/nt/mklink.html
rem mklink saveShortcutAs targetOfShortcut
@echo off 
setlocal enableextensions
rem begin turn off shortcut name extension
rem see https://www.tenforums.com/tutorials/4663-shortcut-name-extension-turn-off-windows-10-a.html
REG ADD "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer" /V link /T REG_Binary /D 00000000 /F
Reg Delete "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\NamingTemplates" /V ShortcutNameTemplate /F
taskkill /f /im explorer.exe
start explorer.exe
rem end turn off shortcut name extension
rem the following command deletes all links to .exe files.          
FOR /f "tokens=*" %%a IN ('dir /b /s *.exe.lnk') DO del "%%a"
FOR /f "tokens=*" %%a IN ('dir /b /s *.exe') DO mklink /h "%%a.lnk" "%%a"
exit /b

看到 mklink /?

ss64.com mklink

看到 cmd /?

参见 /?

参见目录 /?

ss64.com 为

关闭/打开快捷方式名称扩展

Windows 10 中的符号链接、硬链接和目录连接

构建一个 Windows 10 cmd 批处理文件,该文件必须在具有管理员权限的窗口中运行,如果放在根文件夹中,它将为卷上的每个 .exe 文件创建硬快捷方式,如果不放在根文件夹中,它将为文件夹和所有子文件夹中的每个 .exe 文件创建硬快捷方式。 “快捷方式”扩展名不应是文件名的一部分。 .lnk 扩展名应该是文件名的一部分。 已经存在的指向 .exe 文件的链接应该被删除。

答案2

我创建快捷方式的常用方法是在批处理中创建一个临时的 VB 脚本。您的解决方案可能如下所示:

@echo off

set "des=C:\Where\You\Want\The\Shortcuts"
set "src=C:\Root\Folder\Of\Executables"

setlocal enabledelayedexpansion
for /r "%src%" %%A in (*.exe) do (
    set "exe=%%A"
    set "name=%%~nA"

    set "SCRIPT=%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"

    echo Set oWS = WScript.CreateObject^("WScript.Shell"^) >> !SCRIPT!
    echo sLinkFile = "%des%\!name!.lnk" >> !SCRIPT!
    echo Set oLink = oWS.CreateShortcut^(sLinkFile^) >> !SCRIPT!
    echo oLink.TargetPath = "!exe!" >> !SCRIPT!
    echo oLink.Save >> !SCRIPT!

    cscript /nologo !SCRIPT!
    del !SCRIPT!

)

这将为您想要快捷方式的位置(des- 即如果您希望它们放在您的%USERPROFILE%\Desktop或其他文件夹上)和可执行文件的位置(src)设置变量。我们必须setlocal enabledelayedexpansion这样做,以便我们可以在 for 循环中使用变量 - 即:位于我们目录中的for所有可执行文件( ) ,将 .exe 的完整限定路径设置为,然后将 .exe 的名称设置为(不包括扩展名)。下一部分是 VB 脚本 - 我们在 TEMP 文件夹中设置为一些随机命名的 .vbs 文件,然后将 VBS 命令回显到该脚本中,运行它,然后删除它。这将在 for 循环的每次迭代中发生,直到它为每个可执行文件创建了一个快捷方式。*.exesrcexenameSCRIPT

关于 VBS 行需要注意的重要事项:sLinkFile确定创建快捷方式的位置及其名称;oLink.TargetPath引用可执行文件的完整路径;在 for 循环中,变量!使用 's 而不是%'s;当我们将括号回显到 VBS 文件中时,必须使用 对其进行转义^。希望这能完全满足您的要求 - 只需要您设置dessrc变量。

答案3

忍不住发帖电源外壳版本:

$TopFolder = 'C:\FolderWithExeSubfolders'

Get-ChildItem -Path $TopFolder -Filter *.exe -File -Recurse | ForEach-Object -Begin {
    $WSHshell = New-Object -com wscript.shell
} -Process {
    ### Using the folder name as the base name for the shortcut. This allows you the
    ###    option of giving the folder a "display name" to be used.
    $NewLnkPath             = '{0}\{1}.lnk' -f $_.DirectoryName, $_.Directory.Name
    $NewShortcut            = $WSHshell.CreateShortcut($NewLnkPath)
    $NewShortcut.TargetPath = $_.FullName

    ### Optional
    ### $NewShortcut.Arguments        = <string>
    ### $NewShortcut.Description      = <string>
    ### $NewShortcut.Hotkey           = <string>
    ### $NewShortcut.IconLocation     = <string>
    ### $NewShortcut.WindowStyle      = <1|2|7>
    ### $NewShortcut.WorkingDirectory = <string>

    $NewShortcut.Save()
} -End {}

相关内容