Question

I'm looking for a quick and easy way (I don't need any error checking) to switch between two reg files by opening a batch file.

If a user runs a batch file, the batch file should merge reg file A to the registry. If the user runs it again, the batch file should merge reg file B to the registry. If the user runs it again, file A is merged... you get the point.

In general, I think there are two ways to do this:

  1. Create some sort of variable to store which reg should be run next.
  2. Check the registry key values to see which reg has been merged most recently (probably the preferred method).

Currently, I'm doing this:

REG QUERY "KeyName" /v "ValueName" | Find "x"

IF ERRORLEVEL 1 regedit /S file1.reg
IF ERRORLEVEL 0 regedit /S file2.reg

The REG QUERY part seems to work, but at the IF ERRORLEVEL statements something is going wrong. But maybe I should use a different method altogether.

Hoping for some suggestions.. Thanks in advance!

EDIT

Sorry for being a bit vague: I believe the previous solution didn't work, because when the first IF is correct, the second IF will be too after the first IF's command. So I will need an IF ELSE statement to prevent running the second IF.

I've now come up with this solution, which works:

REG QUERY "KeyName" /v "ValueName" | Find "x
IF ERRORLEVEL 1 (REGEDIT /S "file1.reg") ELSE REGEDIT /S "file2.reg"

I was also wondering: would it be an improvement to have the registry key/values added inside the batch file instead of using seperate .reg files? The value types are REG_BINARY and REG_DWORD.

Was it helpful?

Solution

I would try this:

REG QUERY "KeyName" /v "ValueName" | Find "x"

IF ERRORLEVEL 1 (
  regedit /S file1.reg
  goto :EOF
)
regedit /S file2.reg

The IF condition is set to TRUE when the ERRORLEVEL is equal to, or greater than, the ERRORLEVEL number (see here: http://support.microsoft.com/kb/39585/en-us) so the second IF condition is always evaluated as true.

OTHER TIPS

You can store a value in a enviroment variable

@Echo OFF
:: By Elektro H@cker
Set | FIND "Merged" >NUL && Regedit /S "File_B.reg" || Regedit /S "File_A.reg" && SETX Merged YES >NUL
Pause&Exit

This is the same code but indented and with a quickly explanation:

@Echo OFF

:: By Elektro H@cker

REM If it's batfile first launch then I add a value "YES" to a variable and then only merges the File A.
REM If isn't batfile first launch then only merges FILE B

Set | FIND "Merged" >NUL && (
     Regedit /S "File_B.reg"
) || Regedit /S "File_A.reg" && (
     SETX Merged YES >NUL
)

Pause&Exit
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top