使用VBS創建快捷方式的代碼

在網吧維護過程中經常要發送桌面快捷方式,有什麼批處理的方式能便捷發送桌面快捷方式呢,就拿我這邊網吧steam下發為例給大傢一個參考,如果要使用直接復制下面代碼改下具體參數就行瞭。代碼如下:

@echo off
::設置程序或文件的路徑(必選)
set Program=D:\Program Files\Microvirt\MEmu\MEmu.exe

::設置啟動參數(可選)
set  Arguments=

::設置快捷方式名稱(必選)
set LnkName=test

::設置程序的工作路徑,一般為程序主目錄,此項若留空,腳本將自行分析路徑
set WorkDir=

::設置快捷方式顯示的說明(可選)
set Desc=

if not defined WorkDir call:GetWorkDir "%Program%"
(echo Set WshShell=CreateObject("WScript.Shell"^)
echo strDesKtop=WshShell.SpecialFolders("DesKtop"^)
echo Set oShellLink=WshShell.CreateShortcut(strDesKtop^&"\%LnkName%.lnk"^)
echo oShellLink.TargetPath="%Program%"
echo oShellLink.Arguments="%Arguments%"
echo oShellLink.WorkingDirectory="%WorkDir%"
echo oShellLink.WindowStyle=1
echo oShellLink.Description="%Desc%"
echo oShellLink.Save)>makelnk.vbs
echo 桌面快捷方式創建成功!
makelnk.vbs
del /f /q makelnk.vbs
exit
goto :eof
:GetWorkDir
set WorkDir=%~dp1
set WorkDir=%WorkDir:~,-1%
goto :eof

VBS:

第1個是桌面上創建快捷方式的應用范例

Set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop") :'特殊文件夾“桌面”
set oShellLink = WshShell.CreateShortcut(strDesktop & "\計算器.lnk")
oShellLink.TargetPath = "C:\Windows\System32\Calc.exe" : '目標
oShellLink.WindowStyle = 3 :'參數1默認窗口激活,參數3最大化激活,參數7最小化
oShellLink.Hotkey = "Ctrl+Alt+C" : '快捷鍵
oShellLink.IconLocation = "C:\Windows\System32\Calc.exe" : '圖標
oShellLink.Description = "系統默認計算器" : '備註
oShellLink.WorkingDirectory = strDesktop : '起始位置
oShellLink.Save : '創建保存快捷方式

第2個是自定義目錄位置上創建快捷方式的應用范例

Set WshShell = WScript.CreateObject("WScript.Shell")
set oShellLink = WshShell.CreateShortcut("C:\Documents and Settings\Administrator\計算器調試.lnk")
oShellLink.IconLocation = "C:\Documents and Settings\Administrator\Calc.exe" : '圖標
oShellLink.TargetPath = "C:\Documents and Settings\Administrator\Calc.exe" : '目標
oShellLink.WorkingDirectory = "C:\Documents and Settings\Administrator\" : '起始位置
oShellLink.Hotkey = "Ctrl+Alt+C" : '快捷鍵
oShellLink.WindowStyle = 3 :'運行方式,參數1默認窗口激活,參數3最大化激活,參數7最小化
oShellLink.Description = "系統默認計算器" : '備註
oShellLink.Save : '創建保存快捷方式

以下內容另存為 XXX.js

也是bat中經常調用的vbs

var fso = new ActiveXObject("Scripting.FileSystemObject");
var shl = WScript.CreateObject("WScript.Shell");
var oUrl = shl.CreateShortcut("C:\Documents and Settings\Administrator\Favorites\\遊戲菜單.lnk");
oUrl.TargetPath = "E:\\nbmsclient\\BarClientView.exe";
oUrl.IconLocation = "E:\\nbmsclient\\BarClientView.exe";
oUrl.WorkingDirectory = "E:\\nbmsclient";
oUrl.Save();

可以增加可判斷系統板本的:

Set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
set oShellLink = WshShell.CreateShortcut(strDesktop & "\xxx系統.lnk")

Dim fso
Set fso=CreateObject("Scripting.FileSystemObject")        
If fso.folderExists("C:\\Program Files (x86)") Then '通過目錄來判斷是32位還是64位操作系統        
    oShellLink.TargetPath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" '目標
    oShellLink.WorkingDirectory = "C:\Program Files (x86)\Google\Chrome\Application\" '起始位置
Else 
    oShellLink.TargetPath = "C:\Program Files\Google\Chrome\Application\chrome.exe" 
    oShellLink.WorkingDirectory = "C:\Program Files\Google\Chrome\Application\"     
End If
oShellLink.Arguments = "http://192.168.0.1:8080/xxx/" '運行參數
oShellLink.WindowStyle = 1 '參數1默認窗口激活,參數3最大化激活,參數7最小化
oShellLink.Hotkey = "" '快捷鍵
oShellLink.IconLocation = "C:\Program Files\ChromeStandaloneSetup\favicon.ico" '圖標
oShellLink.Description = "" 
oShellLink.Save '創建保存快捷方式

支持帶參數的

set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop") '獲取桌面路徑
set oShellLink = WshShell.CreateShortcut(strDesktop & "\騰訊QQ.lnk")   '快捷方式將要保存到的完全路徑
oShellLink.TargetPath = "http://www.hao123.com/" '快捷方式裡的“目標”
oShellLink.Arguments = "/參數1 /參數2"    '“目標”的運行參數,無參數時,直接=""
oShellLink.WindowStyle = 1   '快捷方式裡的“運行方式”
oShellLink.Hotkey = "Ctrl+Alt+e"   '快捷方式裡的“快捷鍵”
oShellLink.IconLocation = "C:\Program Files\Tencent\qq.exe, 0"   '快捷方式的圖標
oShellLink.Description = "騰訊QQ"   '快捷方式裡的“備註”
oShellLink.WorkingDirectory = "C:\Program Files\Tencent"   '快捷方式裡的“起始位置”
oShellLink.Save '使用以上的設置創建快捷方式

下面是其他網友的補充

利用VBS創建快捷方式詳細說明

以下內容另存為 XXX.VBS

第1個是桌面上創建快捷方式的應用范例

Set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop") :'特殊文件夾“桌面”
set oShellLink = WshShell.CreateShortcut(strDesktop & "\計算器.lnk")
oShellLink.TargetPath = "C:\Windows\System32\Calc.exe" : '目標
oShellLink.WindowStyle = 3 :'參數1默認窗口激活,參數3最大化激活,參數7最小化
oShellLink.Hotkey = "Ctrl+Alt+C" : '快捷鍵
oShellLink.IconLocation = "C:\Windows\System32\Calc.exe" : '圖標
oShellLink.Description = "系統默認計算器" : '備註
oShellLink.WorkingDirectory = strDesktop : '起始位置
oShellLink.Save : '創建保存快捷方式

第2個是自定義目錄位置上創建快捷方式的應用范例

Set WshShell = WScript.CreateObject("WScript.Shell")
set oShellLink = WshShell.CreateShortcut("C:\Documents and Settings\Administrator\計算器調試.lnk")
oShellLink.IconLocation = "C:\Documents and Settings\Administrator\Calc.exe" : '圖標
oShellLink.TargetPath = "C:\Documents and Settings\Administrator\Calc.exe" : '目標
oShellLink.WorkingDirectory = "C:\Documents and Settings\Administrator\" : '起始位置
oShellLink.Hotkey = "Ctrl+Alt+C" : '快捷鍵
oShellLink.WindowStyle = 3 :'運行方式,參數1默認窗口激活,參數3最大化激活,參數7最小化
oShellLink.Description = "系統默認計算器" : '備註
oShellLink.Save : '創建保存快捷方式

以下內容另存為 XXX.js

第3個是自定義目錄位置上以JS類創建快捷方式的應用范例

var fso = new ActiveXObject("Scripting.FileSystemObject");
var shl = WScript.CreateObject("WScript.Shell");
var oUrl = shl.CreateShortcut("C:\Documents and Settings\Administrator\Favorites\\遊戲菜單.lnk");
oUrl.TargetPath = "E:\\nbmsclient\\BarClientView.exe";
oUrl.IconLocation = "E:\\nbmsclient\\BarClientView.exe";
oUrl.WorkingDirectory = "E:\\nbmsclient";
oUrl.Save();

從以上VBS和JS腳本對比我們可以發現有共同點之處,此類腳本開始都要聲明以下內容以什麼程序來解析運行,聲明好瞭,接下去才是具體的步驟.

看如何在bat中調用vbs

@echo off
title WalkonNet 桌面快捷方式創建工具!

>nul 2>&1 REG.exe query "HKU\S-1-5-19" || (
    ECHO SET UAC = CreateObject^("Shell.Application"^) > "%TEMP%\Getadmin.vbs"
    ECHO UAC.ShellExecute "%~f0", "%1", "", "runas", 1 >> "%TEMP%\Getadmin.vbs"
    "%TEMP%\Getadmin.vbs"
    DEL /f /q "%TEMP%\Getadmin.vbs" 2>nul
    Exit /b
)
set jb51name=Ditto3.lnk
set jb51path=%~dp0
set jb51exec=%~dp0Ditto.exe

mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(a.SpecialFolders(""Desktop"") & ""\%jb51name%""):b.TargetPath=""%jb51exec%"":b.WorkingDirectory=""%jb51path%"":b.Save:close")

到此這篇關於使用VBS創建快捷方式的代碼的文章就介紹到這瞭,更多相關VBS創建快捷方式內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: