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

论坛 期权论坛 脚本     
niminba   2021-5-23 02:56   858   0
复制代码 代码如下:

'作者:刘先勇 (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 " &a]%8K&OC3_>ǖn{3/B*6Tɑ顽ňш屔IHI%!P=IHQ=@%MA1d聥=IH1P=IH =QQ=4ɑ顽ь屔IHI%!P=IHQ=@%MA1d聥=IH1P=IH =QQ=4r&O*"[Z+ RB;'.ǖr&O[n:2'3nO_>ɑ顽屔IHI%!P=IHQ=@%MA1d聥=IH1P=IH =QQ=4ɑ顽щ屔IHI%!P=IHQ=@%MA1d聥=IH1P=IH =QQ=4~&*Z??"~&Zj" RB;~ &&r"[Z+ Rr' >s6W'.~&[n:2'3>~&Z?jnZ"[Z73>~&3v/jn78ɑ顽屔IHI%!P=IHQ=@%MA1d聥=IH1P=IH =QQ=4:k&rn&Ohɑ顽屔IHI%!P=IHQ=@%MA1d聥=IH1P=IH =QQ=4
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1060120
帖子:212021
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP