TAGS :Viewed: 9 - Published at: a few seconds ago

[ vbs registry backup working in win7, but not in xp ]

I'm writing a VBScript to make some registry changes, so before I make the changes, I try to backup the registry to a file. This works fine in my Win7 64bit, but when I try to run it in WinXP 32 bit it says it works, but does not create a file. Code is showed below. Why is this not working in Xp, and how can I correct?

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

Answer 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.

Answer 2

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.