1

我正在编写一个 VBScript 来进行一些注册表更改,所以在进行更改之前,我会尝试将注册表备份到一个文件中。这在我的 Win7 64 位中运行良好,但是当我尝试在 WinXP 32 位中运行它时,它说它可以工作,但不会创建文件。代码如下所示。为什么这在 Xp 中不起作用,我该如何纠正?

Sub BackupRegistry()
        regFile = "%USERPROFILE%\Desktop\regBackup.reg" 
        Set oShell = CreateObject("WScript.Shell")
        Set fso = CreateObject("Scripting.FileSystemObject")

        If Not fso.FileExists(regFile) Then
            regCmd = "regedit.exe /E " & regFile
            oShell.Run regCmd, 0, True
            MsgBox("Registry backed up to " & regFile)
        End If
    End Sub
4

2 回答 2

1

Some basic debugging should solve this. add a

wscript.echo regCmd

before you execute it, copy the string and past it in a consolewindow on the XP box, when it runs, the culpit is your oShell.Run, otherwise correct the command in your console and adapt the code to generate it. Also, while testing change the 0 to 1 so that you see the console window while the command executes.

于 2013-04-30T19:29:23.927 回答
1

On XP %USERPROFILE% most likely contains spaces (C:\Documents and Settings\username in english versions), so you need to quote the path, otherwise the command will fail with a path not found error:

regCmd = "regedit.exe /E """ & regFile & """"

On Windows 7 %USERPROFILE% is C:\Users\username (without any spaces), so this issue doesn't arise.

于 2013-04-30T19:29:56.867 回答