写个设置命令的VBS脚本代码

时间:2022-12-04 22:01:19

复制代码 代码如下:


'作者:刘先勇 (eric liu)
'将以下代码复制并保存为"系统命令.vbs",并运行安装。
'安装成功后,可通过在程序、文件或文件夹上点右键->发送到->系统命令来设置一个命令,然后在运行中就可以输入该命令打开文件了。
'脚本运行一次后在右键菜单中增加从这里运行cmd的快捷方式,还增加查找目标文件快捷方式

'on error resume next
if (lcase(right(wscript.fullname,11))<>"wscript.exe") then
set objshell=createobject("wscript.shell")
objshell.run("wscript //nologo "&chr(34)&wscript.scriptfullname&chr(34))
wscript.quit
end if

set pcmd=createobject("wscript.shell")
set pfso=createobject("scripting.filesystemobject")
set pshell = createobject("shell.application")
set psysenv = createobject("wscript.shell").environment("system")
strcomputer = "."
set pwmiservice = getobject("winmgmts:\\" & strcomputer & "\root\cimv2")

function lgetpath(pfile)
ilastseppos = instrrev(pfile, "\", -1, 1)
if ilastseppos <= 0 then
lgetpath=""
exit function
else
lgetpath = left(pfile, ilastseppos-1)
end if
end function

function getlnktarget(linkpath)
linkpath=replace(linkpath, "\", "\\")
set pfiles = pwmiservice.execquery("select * from win32_shortcutfile where name = " & "'" & linkpath & "'")
for each pfile in pfiles
getlnktarget=pfile.target
exit for
next
end function

function listsyscmd(pfilename)
syscmdpath=pcmd.regread("hkcu\software\microsoft\windows\currentversion\explorer\shell folders\syscmd")
set pfolder = pfso.getfolder(syscmdpath)
set pfile = pfso.opentextfile(pfilename, 2, true)

for each file in pfolder.files
linkpath=syscmdpath & "\" & file.name
if ucase(right(file.name, 4))=".lnk" then
set lnkfiles = pwmiservice.execquery("select * from win32_shortcutfile where name = " & "'" & replace(linkpath, "\", "\\") & "'")
for each lnkfile in lnkfiles
pfile.writeline(linkpath & " " & lnkfile.target)
next
else
pfile.writeline linkpath
end if
next
end function

function getconfigpath
set pfolder = pshell.browseforfolder(0, "请选择一个目录:", 0, "::{20d04fe0-3aea-1069-a2d8-08002b30309d}")
if pfolder is nothing then
wscript.quit
end if
set pfolderitem = pfolder.self
getconfigpath = pfolderitem.path
end function

sub setenv(ppath, pvalue)
if pvalue="" then
psysenv.remove(ppath)
else
psysenv(ppath) = pvalue
end if
end sub

function getenv(ppath)
getenv = psysenv(ppath)
end function

'判断一个字符串是否匹配一个正则表达式
' ^\w+[@]\w+[.]\w+$ e-mail地址
' ^[0-9-]+$ 数字
function ismatch(str, patrn)
set r = new regexp
r.pattern = patrn
ismatch = r.test(str)
end function

sub addnewcmd(pshortcutname, ptargetpath, pcmdlocation)
if pshortcutname<>"" then
linkdir = pcmdlocation
set pcmdlink = pcmd.createshortcut(linkdir & "\" & pshortcutname & ".lnk")
pcmdlink.targetpath = ptargetpath
pcmdlink.windowstyle = 1
pcmdlink.hotkey = ""
pcmdlink.iconlocation = "%systemroot%\system32\shell32.dll,146"
pcmdlink.description = "shortcut created at " & date() & " " & time()
pcmdlink.workingdirectory = lgetpath(ptargetpath)
pcmdlink.save
end if
end sub

set pargs=wscript.arguments
if pargs.count = 0 then '无参运行,复制自身到sendto文件夹。

msgbox "注意无参运行即执行安装过程!" & vbcrlf & _
"安装过程包括以下操作:" & vbcrlf & vbcrlf & _
"1、在发送到目录中建立该脚本的快捷方式;" & vbcrlf & _
"2、完成安装后可通过发送到建立快捷方式;" & vbcrlf & _
"3、所有快捷命令可通过运行(win+r)执行。" & vbcrlf
syscmdpath = getconfigpath
if syscmdpath = "" then wscript.quit()

pcmd.regwrite "hkcu\software\microsoft\windows\currentversion\explorer\shell folders\syscmd", syscmdpath, "reg_sz"
'path = pcmd.regread("hkey_local_machine\system\controlset001\control\session manager\environment\path")
path = getenv("path")

if right(syscmdpath, 1)="\" then syscmdpath = left(syscmdpath, len(syscmdpath)-1)
syscmdpathpattern ="(;)?(" & replace(syscmdpath, "\", "\\") & ")(\\)?(;|$)"
if not ismatch(path, syscmdpathpattern) then
path = path & ";" & syscmdpath
setenv "path", path
end if

thisfile = wscript.scriptfullname
if thisfile<>syscmdpath & "\" & wscript.scriptname then
pfso.copyfile thisfile, syscmdpath & "\"
thisfile = syscmdpath & "\" & wscript.scriptname
end if

addnewcmd "n系统命令", thisfile, pcmd.regread("hkcu\software\microsoft\windows\currentversion\explorer\shell folders\sendto")
'在右键菜单中增加创建当前文件或文件夹的系统快捷命令的菜单项
pcmd.regwrite "hkey_classes_root\*\shell\g创建系统命令\command\", "wscript.exe " & thisfile & " ""%1""", "reg_sz"
pcmd.regwrite "hkey_classes_root\directory\shell\g创建系统命令\command\", "wscript.exe " & thisfile & " ""%1""", "reg_sz"
'在右键菜单中增加在当前路径打开cmd窗口命令的菜单项
pcmd.regwrite "hkey_classes_root\*\shell\q在此打开cmd\command\", "cmd /k pushd ""%1\\..""", "reg_sz"
pcmd.regwrite "hkey_classes_root\directory\shell\q在此打开cmd\command\", "cmd /k pushd ""%1""", "reg_sz"

'在右键菜单中增加在查找快捷方式位置的菜单项
pcmd.regwrite "hkey_classes_root\*\shell\w查找目标位置\command\", "wscript.exe " & thisfile & " s ""%1""", "reg_sz"
pcmd.regwrite "hkey_classes_root\directory\shell\w查找目标位置\command\", "wscript.exe " & thisfile & " s ""%1""", "reg_sz"

addnewcmd "q", pcmd.regread("hkcu\software\microsoft\windows\currentversion\explorer\shell folders\syscmd"), pcmd.regread("hkcu\software\microsoft\windows\currentversion\explorer\shell folders\syscmd")
addnewcmd "qc", thisfile, pcmd.regread("hkcu\software\microsoft\windows\currentversion\explorer\shell folders\syscmd")
msgbox "安装成功!"
elseif pargs.count = 1 then
if ucase(pargs(0))="edit" then '只有一个参数且为edit时, 打开此脚本进行编辑。
pcmd.run("notepad.exe " & wscript.scriptfullname)
wscript.quit()
elseif ucase(pargs(0))="list" then '只有一个参数且为list时, 列出所有已经建立的快捷方式和其对应的目标文件。
resultfile=pcmd.regread("hkcu\software\microsoft\windows\currentversion\explorer\shell folders\syscmd") & "\listsyscmd.txt"
listsyscmd resultfile
pcmd.run("notepad.exe " & resultfile)
wscript.quit()
else '只有一个参数时, 默认处理方式是建立传入的文件路径的快捷方式。
pshortcutname=inputbox("请输入该快捷方式的名字:", "创建快捷命令...", "")
if pshortcutname="" then wscript.quit()
addnewcmd pshortcutname, pargs(0), pcmd.regread("hkcu\software\microsoft\windows\currentversion\explorer\shell folders\syscmd")
end if
elseif pargs.count = 2 then
if ucase(pargs(0))="s" then '查找快捷方式目标文件位置
'判断是否包括\,从右键菜单执行会直接传递目标地址,从快捷方式中需要组合快捷方式的地址。
if instr(pargs(1), "\") > 0 and instr(pargs(1), ".lnk") = 0 then '从右键菜单执行
pcmd.run("explorer.exe /select, " & pargs(1))
else '从命令行执行
if instr(pargs(1), "\") > 0 then '全路径.lnk路径
linkpath=pargs(1)
else
linkpath=pcmd.regread("hkcu\software\microsoft\windows\currentversion\explorer\shell folders\syscmd") & "\" & pargs(1) & ".lnk"
end if
linkpath=replace(linkpath, "\", "\\")
set pfiles = pwmiservice.execquery("select * from win32_shortcutfile where name = " & "'" & linkpath & "'")
for each pfile in pfiles
pcmd.run("explorer.exe /n, /select, " & pfile.target)
next
end if
else
'filelocation, shortcutname
pshortcutname=pargs(1)
addnewcmd pshortcutname, pargs(0), pcmd.regread("hkcu\software\microsoft\windows\currentversion\explorer\shell folders\syscmd")
end if
end if


复制好脚本存为syscmd.vbs或其它名称,就可以双击安装了,安装过程很简单,只有以下几步.

1.双击开始安装

 

写个设置命令的VBS脚本代码

2.选择安装目录

 写个设置命令的VBS脚本代码 写个设置命令的VBS脚本代码

3.安装成功

写个设置命令的VBS脚本代码

现在再来查看一下右键菜单

1. 创建快捷命令功能

写个设置命令的VBS脚本代码

在你要创建快捷命令的exe程序或任何其它文件上点右键,然后选择快捷菜单中的"g创建系统命令",或者直接按字母g,弹出以下对话框:

写个设置命令的VBS脚本代码

直接输入快捷命令,如qq,快捷命令qq就创建成功了.

然后想启动qq时直接win+r打开运行窗口,并输入qq回车,qq程序启动就这么简单

写个设置命令的VBS脚本代码

写个设置命令的VBS脚本代码

2.在这里打开cmd功能

在任何文件或文件夹上点右键,然后选择"q在此打开cmd"或者直接按q,就会以此目录为工作目录打开cmd窗口

写个设置命令的VBS脚本代码

 写个设置命令的VBS脚本代码

3.查找目标位置功能

对于windows快捷方式,经常遇到想要查找其它文件位置的情况,一般都是点右键然后查属性,查点查找文件找到,现在任何文件或文件夹上点右键都会有如下菜单,选择"w查找目标位置"或直接按w,可以查找到快捷方式的目标文件或文件夹位置,也可以查找到桌面上程序的目录位置.

写个设置命令的VBS脚本代码

直接定位文件所在目录并打开:

写个设置命令的VBS脚本代码