我正在创建一个打开的脚本,在regedit中指定一个键,问题是我需要remoear缩写(因为不支持LastKey键),它可能会干扰正确位置的键。
示例:
HKLM\SOFTWARE\HKLM\SOFTWARE\NVIDIA Corporation\Global\OEM\WhiteList
应该是:
HKEY_LOCAL_MACHINE\SOFTWARE\HKLM\SOFTWARE\NVIDIA Corporation\Global\OEM\WhiteList
目前正在:
HKEY_LOCAL_MACHINE\SOFTWARE\HKEY_LOCAL_MACHINE\SOFTWARE\NVIDIA Corporation\Global\OEM\WhiteList
注意:有一个密钥至少有一个字母,所以也应该使用它" HKU \" (HKEY_USERS)
我的代码:
Set WshShell = CreateObject("WScript.Shell")
Dim sKey, bFound
'-----------------------------------------------
Sub Main()
NameScript = "Jump to Key"
MsgScript1 = "Type the Registry path."
MsgScript2 = "Not found."
'-----------------------------------------------
sKey = Inputbox(MsgScript1,NameScript,sKey)
If sKey = "" Then WScript.quit()
'-----------------------------------------------
sKey = sKey & "\"
sKey = Replace(sKey, "\\", "\", 1, -1, 1)
sKey = Replace(sKey, "HKCR\", "HKEY_CLASSES_ROOT\", 1, -1, 1)
sKey = Replace(sKey, "HKCU\", "HKEY_CURRENT_USER\", 1, -1, 1)
sKey = Replace(sKey, "HKLM\", "HKEY_LOCAL_MACHINE\", 1, -1, 1)
sKey = Replace(sKey, "HKU\", "HKEY_USERS\", 1, -1, 1)
sKey = Replace(sKey, "HKCC\", "HKEY_CURRENT_CONFIG\", 1, -1, 1)
'-----------------------------------------------
with CreateObject("WScript.Shell")
on error resume next ' turn off error trapping
sValue = .regread(sKey) ' read attempt
bFound = (err.number = 0) ' test for success
on error goto 0 ' restore error trapping
end with
'
if not bFound then
'-----------------------------------------------
Msgbox MsgScript2,vbInformation,NameScript
Call Main
'-----------------------------------------------
Else
'-----------------------------------------------
KillProc "Regedit.exe"
WshShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit\Lastkey",sKey,"REG_SZ"
WshShell.Run "Regedit.exe", 1,True
Call Main
End if
Set WshShell = Nothing
End Sub
On Error Resume Next
Main
IF Err.Number Then
WScript.Quit 4711
End if
'-----------------------------------------------
Sub KillProc( myProcess )
'Purpose: Kills a process and waits until it is truly dead
Dim blnRunning, colProcesses, objProcess
blnRunning = False
Set colProcesses = GetObject( _
"winmgmts:{impersonationLevel=impersonate}" _
).ExecQuery( "Select * From Win32_Process", , 48 )
For Each objProcess in colProcesses
If LCase( myProcess ) = LCase( objProcess.Name ) Then
' Confirm that the process was actually running
blnRunning = True
' Get exact case for the actual process name
myProcess = objProcess.Name
' Kill all instances of the process
objProcess.Terminate()
End If
Next
If blnRunning Then
' Wait and make sure the process is terminated.
' Routine written by Denis St-Pierre.
Do Until Not blnRunning
Set colProcesses = GetObject( _
"winmgmts:{impersonationLevel=impersonate}" _
).ExecQuery( "Select * From Win32_Process Where Name = '" _
& myProcess & "'" )
WScript.Sleep 100 'Wait for 100 MilliSeconds
If colProcesses.Count = 0 Then 'If no more processes are running, exit loop
blnRunning = False
End If
Loop
End If
End Sub
答案 0 :(得分:1)
问题是Replace
的第五个参数。这告诉函数应该替换您要搜索的文本的实例数。 -1
表示全部。因此,如果注册表路径包含“HKLM”5次,并且您说Replace(path,"HKLM\", "blah", 1, -1, 1)
,则会将path
中的所有五个“HKLM”实例替换为“blah”。
因此,您的第五个参数应该是您想要替换的发生次数。如果您只想替换第一个,则应为1
- 例如Replace(path, "HKLM\", "blah", 1, 1, 1)
。
答案 1 :(得分:0)
以下是我的脚本的最终结果:
If WScript.Arguments.Named.Exists("elevated") = False Then
CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 1
WScript.Quit
End If
Set WshShell = CreateObject("WScript.Shell")
Dim BoxKey, sKey, bFound
'-----------------------------------------------
Sub Main()
NameScript = "Jump to Key"
MsgScript1 = "Type the Registry path."
MsgScript2 = "Not found."
'-----------------------------------------------
BoxKey = Inputbox(MsgScript1,NameScript,BoxKey)
If BoxKey = "" Then WScript.quit()
'-----------------------------------------------
sKey = BoxKey & "\"
sKey = Replace(sKey, "\\", "\", 1, -1, 1)
sKey = Replace(sKey, "HKCR\", "HKEY_CLASSES_ROOT\", 1, 1, 1)
sKey = Replace(sKey, "HKCU\", "HKEY_CURRENT_USER\", 1, 1, 1)
sKey = Replace(sKey, "HKLM\", "HKEY_LOCAL_MACHINE\", 1, 1, 1)
sKey = Replace(sKey, "HKU\", "HKEY_USERS\", 1, 1, 1)
sKey = Replace(sKey, "HKCC\", "HKEY_CURRENT_CONFIG\", 1, 1, 1)
'-----------------------------------------------
with CreateObject("WScript.Shell")
on error resume next ' turn off error trapping
sValue = .regread(sKey) ' read attempt
bFound = (err.number = 0) ' test for success
on error goto 0 ' restore error trapping
end with
'
if not bFound then
'-----------------------------------------------
Msgbox MsgScript2,vbInformation,NameScript
Call Main
'-----------------------------------------------
Else
'-----------------------------------------------
KillProc "Regedit.exe"
sKey = Left(sKey, (LEN(sKey) -1))
WshShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit\Lastkey",sKey,"REG_SZ"
WshShell.Run "Regedit.exe", 1,True
Call Main
End if
Set WshShell = Nothing
End Sub
On Error Resume Next
Main
IF Err.Number Then
WScript.Quit 4711
End if
'-----------------------------------------------
Sub KillProc( myProcess )
'Purpose: Kills a process and waits until it is truly dead
Dim blnRunning, colProcesses, objProcess
blnRunning = False
Set colProcesses = GetObject( _
"winmgmts:{impersonationLevel=impersonate}" _
).ExecQuery( "Select * From Win32_Process", , 48 )
For Each objProcess in colProcesses
If LCase( myProcess ) = LCase( objProcess.Name ) Then
' Confirm that the process was actually running
blnRunning = True
' Get exact case for the actual process name
myProcess = objProcess.Name
' Kill all instances of the process
objProcess.Terminate()
End If
Next
If blnRunning Then
' Wait and make sure the process is terminated.
' Routine written by Denis St-Pierre.
Do Until Not blnRunning
Set colProcesses = GetObject( _
"winmgmts:{impersonationLevel=impersonate}" _
).ExecQuery( "Select * From Win32_Process Where Name = '" _
& myProcess & "'" )
WScript.Sleep 100 'Wait for 100 MilliSeconds
If colProcesses.Count = 0 Then 'If no more processes are running, exit loop
blnRunning = False
End If
Loop
End If
End Sub