溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何寫個(gè)設(shè)置命令的VBS腳本代碼

發(fā)布時(shí)間:2021-10-08 15:37:07 來源:億速云 閱讀:151 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)如何寫個(gè)設(shè)置命令的VBS腳本代碼,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

代碼如下:

'將以下代碼復(fù)制并保存為"系統(tǒng)命令.VBS",并運(yùn)行安裝。
'安裝成功后,可通過在程序、文件或文件夾上點(diǎn)右鍵->發(fā)送到->系統(tǒng)命令來設(shè)置一個(gè)命令,然后在運(yùn)行中就可以輸入該命令打開文件了。
'腳本運(yùn)行一次后在右鍵菜單中增加從這里運(yùn)行CMD的快捷方式,還增加查找目標(biāo)文件快捷方式

'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, "請(qǐng)選擇一個(gè)目錄:", 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

'判斷一個(gè)字符串是否匹配一個(gè)正則表達(dá)式
' ^\w+[@]\w+[.]\w+$ E-Mail地址
' ^[0-9-]+$ 數(shù)字
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 '無參運(yùn)行,復(fù)制自身到SendTo文件夾。

MsgBox "注意無參運(yùn)行即執(zhí)行安裝過程!" & VBCRLF & _
"安裝過程包括以下操作:" & VBCRLF & VBCRLF & _
"1、在發(fā)送到目錄中建立該腳本的快捷方式;" & VBCRLF & _
"2、完成安裝后可通過發(fā)送到建立快捷方式;" & VBCRLF & _
"3、所有快捷命令可通過運(yùn)行(WIN+R)執(zhí)行。" & 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系統(tǒng)命令", thisFile, pCmd.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\SendTo")
'在右鍵菜單中增加創(chuàng)建當(dāng)前文件或文件夾的系統(tǒng)快捷命令的菜單項(xiàng)
pCmd.RegWrite "HKEY_CLASSES_ROOT\*\shell\G創(chuàng)建系統(tǒng)命令\command\", "WScript.exe " & thisFile & " ""%1""", "REG_SZ"
pCmd.RegWrite "HKEY_CLASSES_ROOT\Directory\shell\G創(chuàng)建系統(tǒng)命令\command\", "WScript.exe " & thisFile & " ""%1""", "REG_SZ"
'在右鍵菜單中增加在當(dāng)前路徑打開CMD窗口命令的菜單項(xiàng)
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"

'在右鍵菜單中增加在查找快捷方式位置的菜單項(xiàng)
pCmd.RegWrite "HKEY_CLASSES_ROOT\*\shell\W查找目標(biāo)位置\command\", "WScript.exe " & thisFile & " S ""%1""", "REG_SZ"
pCmd.RegWrite "HKEY_CLASSES_ROOT\Directory\shell\W查找目標(biāo)位置\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 '只有一個(gè)參數(shù)且為Edit時(shí), 打開此腳本進(jìn)行編輯。
pCmd.Run("Notepad.exe " & WScript.ScriptFullName)
WScript.Quit()
ElseIF UCase(pArgs(0))="LIST" Then '只有一個(gè)參數(shù)且為L(zhǎng)ist時(shí), 列出所有已經(jīng)建立的快捷方式和其對(duì)應(yīng)的目標(biāo)文件。
ResultFile=pCmd.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\SysCmd") & "\ListSysCmd.txt"
ListSysCmd ResultFile
pCmd.Run("Notepad.exe " & ResultFile)
WScript.Quit()
Else '只有一個(gè)參數(shù)時(shí), 默認(rèn)處理方式是建立傳入的文件路徑的快捷方式。
pShortCutName=InputBox("請(qǐng)輸入該快捷方式的名字:", "創(chuàng)建快捷命令...", "")
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 '查找快捷方式目標(biāo)文件位置
'判斷是否包括\,從右鍵菜單執(zhí)行會(huì)直接傳遞目標(biāo)地址,從快捷方式中需要組合快捷方式的地址。
IF Instr(pArgs(1), "\") > 0 And Instr(pArgs(1), ".lnk") = 0 Then '從右鍵菜單執(zhí)行
pCmd.Run("Explorer.exe /select, " & pArgs(1))
Else '從命令行執(zhí)行
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


復(fù)制好腳本存為Syscmd.vbs或其它名稱,就可以雙擊安裝了,安裝過程很簡(jiǎn)單,只有以下幾步.

1.雙擊開始安裝

如何寫個(gè)設(shè)置命令的VBS腳本代碼

2.選擇安裝目錄

 如何寫個(gè)設(shè)置命令的VBS腳本代碼 如何寫個(gè)設(shè)置命令的VBS腳本代碼

3.安裝成功

如何寫個(gè)設(shè)置命令的VBS腳本代碼

現(xiàn)在再來查看一下右鍵菜單

1. 創(chuàng)建快捷命令功能

如何寫個(gè)設(shè)置命令的VBS腳本代碼

在你要?jiǎng)?chuàng)建快捷命令的exe程序或任何其它文件上點(diǎn)右鍵,然后選擇快捷菜單中的"G創(chuàng)建系統(tǒng)命令",或者直接按字母G,彈出以下對(duì)話框:

如何寫個(gè)設(shè)置命令的VBS腳本代碼

直接輸入快捷命令,如QQ,快捷命令QQ就創(chuàng)建成功了.

然后想啟動(dòng)QQ時(shí)直接WIN+R打開運(yùn)行窗口,并輸入QQ回車,QQ程序啟動(dòng)就這么簡(jiǎn)單

如何寫個(gè)設(shè)置命令的VBS腳本代碼

如何寫個(gè)設(shè)置命令的VBS腳本代碼

2.在這里打開CMD功能

在任何文件或文件夾上點(diǎn)右鍵,然后選擇"Q在此打開CMD"或者直接按Q,就會(huì)以此目錄為工作目錄打開CMD窗口

如何寫個(gè)設(shè)置命令的VBS腳本代碼

 如何寫個(gè)設(shè)置命令的VBS腳本代碼

3.查找目標(biāo)位置功能

對(duì)于Windows快捷方式,經(jīng)常遇到想要查找其它文件位置的情況,一般都是點(diǎn)右鍵然后查屬性,查點(diǎn)查找文件找到,現(xiàn)在任何文件或文件夾上點(diǎn)右鍵都會(huì)有如下菜單,選擇"W查找目標(biāo)位置"或直接按W,可以查找到快捷方式的目標(biāo)文件或文件夾位置,也可以查找到桌面上程序的目錄位置.

如何寫個(gè)設(shè)置命令的VBS腳本代碼

直接定位文件所在目錄并打開:

如何寫個(gè)設(shè)置命令的VBS腳本代碼

關(guān)于“如何寫個(gè)設(shè)置命令的VBS腳本代碼”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

vbs
AI