问题
I have a script that changes a registry value, before doing that, it makes a backup of the registry key using regedit /e
to create a .reg file.
If the script is run a second time and the .reg backup file exists, I am asking the user if they want to add their backup back into the registry.
Because I don't want to confuse users who will have no real idea of whats happening with extra prompts they might not understand, I am using the following code to do this with out asking the user if they want to import:
Set objShell = CreateObject("WScript.Shell")
objShell.Run"regedit /s " & """" & BackupFile & """" , 0, True
Is there a way to get a exit code from the regedit
command for success or failure of regedit
importing the backup .reg file?
回答1:
regedit
doesn't return a status code. Use reg.exe
with the import
subcommand instead:
rc = objShell.Run("reg import """ & BackupFile & """" , 0, True)
If rc = 0 Then
WScript.Echo "Import successful."
Else
WScript.Echo "Import failed. (" & rc & ")"
End If
回答2:
Use
Return = objShell.Run("regedit /s " & """" & BackupFile & """" , 0, True)
Return
contains the returned value.
来源:https://stackoverflow.com/questions/18296926/can-i-get-an-exit-code-from-running-regedit-s