IF in registry

June 26, 2009 at 03:25:53
Specs: Microsoft Windows XP Professional, 3.192 GHz / 3070 MB
hey there again, id like this:

Id like a batch to make a REG_SZ in HKLM\Software\
and then go on about its workings.

Then id like it on its next run (Which would mean this code would have to go b4 the REG_SZ is created) for the batch to see if there is the reg there.


See More: IF in registry

Report •


#1
June 27, 2009 at 04:00:26
2>nul reg query "hklm\software\<your key>" || (
	::This is where stuff goes to be executed
	::when the key doesn't exist
	reg add "hklm\software\<your key>" /v "valuname" /t reg_sz /d "data for value"
)


Report •

#2
June 27, 2009 at 13:26:00
Okay, thnx...

Few Questions:

Whats 2>nul for? I know what >Nul does, but whats the 2 for?

This works, but what if I want it to check a specific reg_sz's value to see if its either 0 or 1, or what ever is there?


Report •

#3
June 27, 2009 at 16:58:22
The 2 in 2>nul redirects errors instead of normal output, try it
without the "2" on a key that doesn't exist and you will see
what I mean.

for /f "skip=4 tokens=2* delims=	" %%a in ('2^>nul reg query "hklm\software\yourkey" /v "yourvalue"') do (
	if /i "%%a"=="reg_sz" set "regvalue=%%b"
)
if not defined regvalue (
	rem Either key or value doesn't exist
	rem or it isn't a reg_sz.
	rem Do whatever you want to do when
	rem It doesn't exist here
)
If "%regvalue%"=="whatever" (
	rem Do whatever when your reg value==whatever
)
:: repeat as nessacery
....
....


Report •

Related Solutions

#4
June 27, 2009 at 23:47:47
Ok, i got a little lost that one. This is what I think it should be when i get done adding my crap.

for /f "skip=4 tokens=2* delims= " %%a in ('2^>nul reg query "hklm\software\CaclsUI" /v "runonce"') do (
if /i "%%a"=="reg_sz" set "1=%%b"
)
if not defined regvalue (
rem Either key or value doesn't exist
rem or it isn't a reg_sz.
rem Do whatever you want to do when
rem It doesn't exist here
@GOTO FIRSTRUN
)
If "%regvalue%"=="1" (
rem Do whatever when your reg value==whatever
@Goto RANALREADY
)


See what i want to do is make it so that when my batch it checks for a regkey to see if its been ran once already, and if the key is there it moves along in the batch to another ":place" (label) and skips what i want it to, and if its not been ran before, it adds a regkey for it to look for the next time its ran and then does what i want it to, which would be an INTRO of the batch and what not. =]


Report •

#5
June 28, 2009 at 00:51:11
Here's the problem your having

if /i "%%a"=="reg_sz" set "1=%%b"

This sets the variable "1" to the contents of the registry value then
you check against the non-existant "regval" variable. In general
it is not a good idea to use variables that start with numbers
because non-dynamic variable conflict with argument
variables - e.g %1 vs %1%.

Since you only want to use goto's the process can be simplified:

for /f "skip=4 tokens=2* delims=	" %%a in ('2^>nul reg query "hklm\software\CaclsUI" /v "runonce"') do (
	if /i "%%a"=="reg_sz" if "%%b"=="1" goto ranalready
)
goto firstrun


Report •

#6
June 28, 2009 at 00:53:36
Thank you, i will try that directly.

Report •

#7
June 28, 2009 at 00:56:03
I just gave it a quick edit cause I had the labels mixed up and a goto in the wrong spot. You may have copied it before I edited.

Report •

#8
June 28, 2009 at 01:02:26
Actually I havnet tested it yet, Im also working on other parts of the batch that i want to finish. ill go ahead and try it now.

Report •

#9
June 28, 2009 at 01:09:40
Okay, I ran this..


@ECHO OFF
@CLS
@for /f "skip=4 tokens=2* delims= " %%a in ('2^>nul reg query "hklm\software\CaclsUI" /v "runonce"') do (
if /i "%%a"=="reg_sz" if "%%b"=="1" goto firstrun
goto ranalready
)


:Firstrun
@cls
@reg add hklm\software\CaclsUI /v runonce /t reg_sz /d 1
@Echo First Run!
@Pause
@exit

:ranalready
@cls
@echo welcome back!
@pause


and it said Operation success, so it made the reg file.

then i ran it again and got.
"Value runonce exists, overwrite(Y/N)?"

So its hitting firstrun again and not skipping to ranalready. idk why.


Report •

#10
June 28, 2009 at 01:15:10
Yep, you copied it before I edited...

The labels are mixed up and the goto firstrun is inside the for
loop where it shouldn't be. This is how it should be (exactly the
same as the edit):

for /f "skip=4 tokens=2* delims=	" %%a in ('2^>nul reg query "hklm\software\CaclsUI" /v "runonce"') do (
	if /i "%%a"=="reg_sz" if "%%b"=="1" goto ranalready
)
goto firstrun


Report •

#11
June 28, 2009 at 01:19:25
Thnk you, now for my next section of the batch =] Your good at this man, i knw a little of this batch, but im not good at making things like this..


for /f "skip=4 tokens=2* delims= " %%a in ('2^>nul reg query "hklm\software\CaclsUI" /v "runonce"') do (
if /i "%%a"=="reg_sz" if "%%b"=="1" goto ranalready
)
goto firstrun


Report •


Ask Question