发送到->系统命令来设置一个命令,然后在..." />
 手机
当前位置:查字典教程网 >脚本专栏 >vbs >写个设置命令的VBS脚本代码
写个设置命令的VBS脚本代码
摘要:复制代码代码如下:'作者:刘先勇(EricLiu)'将以下代码复制并保存为"系统命令.VBS",并运行安装。'安装成功后,可通过在程序、文件...

复制代码 代码如下:

'作者:刘先勇 (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 & "rootcimv2")

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("HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerShell FoldersSysCmd")

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%system32shell32.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 "HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerShell FoldersSysCmd", SysCmdPath, "REG_SZ"

'Path = pCmd.RegRead("HKEY_LOCAL_MACHINESYSTEMControlSet001ControlSession ManagerEnvironmentPath")

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("HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerShell FoldersSendTo")

'在右键菜单中增加创建当前文件或文件夹的系统快捷命令的菜单项

pCmd.RegWrite "HKEY_CLASSES_ROOT*shellG创建系统命令command", "WScript.exe " & thisFile & " ""%1""", "REG_SZ"

pCmd.RegWrite "HKEY_CLASSES_ROOTDirectoryshellG创建系统命令command", "WScript.exe " & thisFile & " ""%1""", "REG_SZ"

'在右键菜单中增加在当前路径打开CMD窗口命令的菜单项

pCmd.RegWrite "HKEY_CLASSES_ROOT*shellQ在此打开CMDcommand", "CMD /K PUSHD ""%1..""", "REG_SZ"

pCmd.RegWrite "HKEY_CLASSES_ROOTDirectoryshellQ在此打开CMDcommand", "CMD /K PUSHD ""%1""", "REG_SZ"

'在右键菜单中增加在查找快捷方式位置的菜单项

pCmd.RegWrite "HKEY_CLASSES_ROOT*shellW查找目标位置command", "WScript.exe " & thisFile & " S ""%1""", "REG_SZ"

pCmd.RegWrite "HKEY_CLASSES_ROOTDirectoryshellW查找目标位置command", "WScript.exe " & thisFile & " S ""%1""", "REG_SZ"

AddNewCmd "Q", pCmd.RegRead("HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerShell FoldersSysCmd"), pCmd.RegRead("HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerShell FoldersSysCmd")

AddNewCmd "QC", thisFile, pCmd.RegRead("HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerShell FoldersSysCmd")

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("HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerShell FoldersSysCmd") & "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("HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerShell FoldersSysCmd")

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("HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerShell FoldersSysCmd") & "" & 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("HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerShell FoldersSysCmd")

End IF

End If

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

1.双击开始安装

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

2.选择安装目录

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

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

3.安装成功

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

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

1. 创建快捷命令功能

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

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

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

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

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

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

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

2.在这里打开CMD功能

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

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

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

3.查找目标位置功能

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

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

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

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

【写个设置命令的VBS脚本代码】相关文章:

用vbs检索在运行对话框中键入的一系列命令的代码

vbs定时发送邮件的方法与代码

PDF的VBS小程序代码

vbs实现的eMule电驴自动关机脚本代码

禁止QQ上网的vbs脚本代码

使用vbs下载文件的代码加强版

vbs判读盘符被双击的脚本

vbs复制文件的脚本

vbs下通过日期查找文件夹的代码

WINDOWS脚本实践:为SAP补丁制作的VBS脚本代码

精品推荐
分类导航