Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How can i check the return result of "reg query" command?  (Read 14290 times)

0 Members and 1 Guest are viewing this topic.

linuxyf

    Topic Starter


    Rookie

    I can list all subkeys and values of an appointed registry key with "reg query" command?. for example: reg query hklm\SOFTWARE\CryptionDirectory /s

    now, i want to check the return result of "reg query" command, if some subkeys exist, execute "test1.exe", if no subkeys exist, execute "test2.exe".

    how to write the batch file??

    thanks.

    blastman



      Hopeful

      Re: How can i check the return result of "reg query" command?
      « Reply #1 on: June 30, 2008, 03:58:14 AM »
      Hi,

      You might be able to create a for loop, but as i know very little about them, I'd use a if statement;

      You'd have to check for the keys individually using the /v switch in reg query. something like;


      Code: [Select]
      reg query /v hklm\SOFTWARE\CryptionDirectory\install
      if "%errorlevel%" GTR "0" (goto fail) ELSE goto pass

      echo This shoud never be seen


      :fail
      echo unable to find key
      start test2.bat
      exit

      :pass
      echo Key's exist ok
      start test1.bat
      exit


      I've never used reg query before, but I'd guess that if you looked for them individually you could use the %errorlevel% produced. If you looked for more than one at one time, I think you get unpredictable results.

      I might well be wrong, but I thought I'd throw in my 2 cents.  ;)


      Hope it helps.



      Blastman, you are the man. Thank You Very Much!!!!!!!!!



      Dias de verano

      • Guest
      Re: How can i check the return result of "reg query" command?
      « Reply #2 on: June 30, 2008, 04:33:21 AM »
      if errorlevel 1 actually means "if errorlevel is 1 or more".

      the && (success) and || (failure) operators might be useful here.

      e.g.

      reg query /v hklm\SOFTWARE\CryptionDirectory\install && goto yes



      linuxyf

        Topic Starter


        Rookie

        Re: How can i check the return result of "reg query" command?
        « Reply #3 on: July 01, 2008, 12:12:55 AM »
        The subkey count is  uncertain, and i don't know the name of subkey, how can i do???

        Dias de verano

        • Guest
        Re: How can i check the return result of "reg query" command?
        « Reply #4 on: July 01, 2008, 12:25:11 AM »
        Why do you want to do this?

        linuxyf

          Topic Starter


          Rookie

          Re: How can i check the return result of "reg query" command?
          « Reply #5 on: July 01, 2008, 12:57:45 AM »
          if no subkey exists, the execute result of reg query is as following:

          C:\Documents and Settings\linuxyf>reg query hklm\SOFTWARE\CryptionDirectory /s

          ! REG.EXE VERSION 3.0

          HKEY_LOCAL_MACHINE\SOFTWARE\CryptionDirectory



          if subkey  exists, the execute result of reg query is as following:


          C:\Documents and Settings\linuxyf>reg query hklm\SOFTWARE\CryptionDirectory /s

          ! REG.EXE VERSION 3.0

          HKEY_LOCAL_MACHINE\SOFTWARE\CryptionDirectory
              subkey1     REG_SZ  1
              subkey2     REG_SZ  2


          linuxyf

            Topic Starter


            Rookie

            Re: How can i check the return result of "reg query" command?
            « Reply #6 on: July 01, 2008, 01:00:15 AM »
            I only want to know whether subkey exists or not under the "hklm\SOFTWARE\CryptionDirectory" in registry?

            if exists, then execute test1.exe, or not, execute test2.exe

            blastman



              Hopeful

              Re: How can i check the return result of "reg query" command?
              « Reply #7 on: July 01, 2008, 02:00:43 AM »
              not knowing 'reg query' all that well, you might have to output it a txt file and pull the lines that contain the reg keys names back in. Then you can say 'if the lies are blank do this...'


              something like;

              Code: [Select]
              @echo off

              rem outputing result of reg query to a txt file

              reg query hklm\SOFTWARE\CryptionDirectory /s > C:\regOUTput.txt

              rem bringing in line 4

              setlocal enabledelayedexpansion
              set count=0
              for /f %%v in (c:\regOUTput.txt) do (
              set /a count=!count!+1
              if !count!==4 set /a reg=%%v
              )


              rem if line 3 is blank = no reg keys, run test1.bat

              if "%reg%" == "" (start test1.bat) ELSE start test2.bat


              This is untested and you'll have to play around with it to get it to work. I'd suggest running the first line (outputting to a txt file) from a command prompt and then opening the file to see the results. Count the number of lines down and note the line number that the first result is on. (from what you've posted, I'd say line 4, but better to check)

              You'll have to sub that number for the number 4 that I've used in the code above.

              Let us know how you get on.
              « Last Edit: July 01, 2008, 03:17:29 AM by blastman »

              Blastman, you are the man. Thank You Very Much!!!!!!!!!



              linuxyf

                Topic Starter


                Rookie

                Re: How can i check the return result of "reg query" command?
                « Reply #8 on: July 01, 2008, 03:12:33 AM »
                becase the subkey names are full path, so i meet requirement with following code:


                @echo off
                cls

                setlocal enabledelayedexpansion

                for /f "tokens=2 delims=:" %%i in ('reg query hklm\SOFTWARE\CryptionDirectory /s
                ') do goto success 

                echo no subkey!
                pause>nul

                :success
                echo find key ok
                pause>nul