

InterviewSolution
Saved Bookmarks
1. |
Solve : Using Batch file to QUERY registry data? |
Answer» <html><body><p>Hi there,<br/><br/>I've never used a batch file to query a registry entry, So I believe my problem is there!<br/><br/>What I am trying to do is:<br/><br/>1 Look for a registry setting, if found, move to 5, if not, move on<br/>2 Look for another registry setting, if found, move to 6, if not, move on<br/>3 Look for another registry setting, if NOT found, move to 7, if not, move on to 8<br/>4 Look for another registry setting, if found, move to 9<br/>5 run specific uninstaller, when finished, go to 8<br/>6 run specific uninstaller, when finished, go to 8<br/>7 Install specific version of software, go to 8<br/>8 Restart machine<br/>9 Exit<br/><br/>This will be run from the startup folder and will cycle till complete, then delete itself.<br/><br/>Here is the code<br/> Code: <a>[Select]</a>ECHO OFF<br/><br/><br/>IF EXIST REG QUERY HKLM\Software\TrendMicro\PC-cillinNTCorp\CurrentVersion\Misc /v ProgramVer /t REG_SZ /d 5.58 GOTO ONE ELSE<br/>IF EXIST REG QUERY HKLM\Software\TrendMicro\PC-cillinNTCorp\CurrentVersion\Misc /v ProgramVer /t REG_SZ /d 8.0 GOTO TWO ELSE<br/>IF NOT EXIST REG QUERY HKLM\Software\TrendMicro\PC-cillinNTCorp\CurrentVersion\Misc /v ProgramVer /t REG_SZ /d 10.0 GOTO THREE<br/>IF EXIST REG QUERY HKLM\Software\TrendMicro\PC-cillinNTCorp\CurrentVersion\Misc /v ProgramVer /t REG_SZ /d 10.0 GOTO FIVE ELSE<br/><br/>:ONE<br/>START /WAIT ".\5.53\CmnUnins.exe"<br/>GOTO FOUR<br/><br/>:TWO <br/>START /WAIT ".\8.0\CmnUnins.exe"<br/>GOTO FOUR<br/><br/>:THREE<br/>START /WAIT ".\10.0\msiexec /a /forcerestart Osce10.msi"<br/>EXIT<br/><br/>:FOUR<br/>START C:\Windows\RUNDLL.EXE user.exe,exitwindowsexec<br/>EXIT<br/><br/>:FIVE<br/>; Need to add in a <a href="https://interviewquestions.tuteehub.com/tag/del-947443" style="font-weight:bold;" target="_blank" title="Click to know more about DEL">DEL</a> command for the file in the Startup menu<br/>EXIT<br/>To Test the process, I Changed the first part to:<br/> Code: <a>[Select]</a>IF NOT EXIST REG QUERY HKLM\Software\TrendMicro\PC-cillinNTCorp\CurrentVersion\Misc /v ProgramVer /t REG_SZ /d 5.58 GOTO ONE ELSE<br/>and <br/> Code: <a>[Select]</a>:ONE<br/>ECHO IT WORKS<br/>PAUSE EXIT<br/><br/><br/>But it gives me an <a href="https://interviewquestions.tuteehub.com/tag/error-25548" style="font-weight:bold;" target="_blank" title="Click to know more about ERROR">ERROR</a><br/> Code: <a>[Select]</a>Invalid Parameter(s)<br/>QUERY { PROCESS | SESSION | TERMSERVER | USER }<br/>Invalid Parameter(s)<br/>QUERY { PROCESS | SESSION | TERMSERVER | USER }<br/>Is this to do with an incorrect Reg Query? or is it to do with my user rights (Admin)?<br/><br/>Anyone able to help?<br/><br/>(If I post this on any other forums, I will place links here!)<br/><br/>BirdI'm reasonably certain you cannot use an existence test for whether a registry entry <a href="https://interviewquestions.tuteehub.com/tag/exists-979838" style="font-weight:bold;" target="_blank" title="Click to know more about EXISTS">EXISTS</a> or not.<br/><br/>Another approach would be to test the <em>errorleve</em>l.<br/><br/> Code: <a>[Select]</a>ECHO OFF<br/><br/>REG QUERY HKLM\Software\TrendMicro\PC-cillinNTCorp\CurrentVersion\Misc /v ProgramVer /t REG_SZ /d 5.58<br/>if not errorlevel 1 goto one<br/>REG QUERY HKLM\Software\TrendMicro\PC-cillinNTCorp\CurrentVersion\Misc /v ProgramVer /t REG_SZ /d 8.0 <br/>if not errorlevel 1 GOTO TWO <br/>REG QUERY HKLM\Software\TrendMicro\PC-cillinNTCorp\CurrentVersion\Misc /v ProgramVer /t REG_SZ /d 10.0 <br/>if errorlevel 1 GOTO THREE<br/>REG QUERY HKLM\Software\TrendMicro\PC-cillinNTCorp\CurrentVersion\Misc /v ProgramVer /t REG_SZ /d 10.0 <br/>if not errorlevel 1 GOTO FIVE <br/><br/>:ONE<br/>START /WAIT ".\5.53\CmnUnins.exe"<br/>GOTO FOUR<br/><br/>:TWO <br/>START /WAIT ".\8.0\CmnUnins.exe"<br/>GOTO FOUR<br/><br/>:THREE<br/>START /WAIT ".\10.0\msiexec /a /forcerestart Osce10.msi"<br/>EXIT<br/><br/>:FOUR<br/>START C:\Windows\RUNDLL.EXE user.exe,exitwindowsexec<br/>EXIT<br/><br/>:FIVE<br/>; Need to add in a DEL command for the file in the Startup menu<br/>EXIT<br/><br/>Good luck. Now it comes up with the error:<br/><br/> Code: <a>[Select]</a>Error: Too many command line parameters<br/>BLYAT!<br/><br/>Any clues anyone?Where are you getting all those <strong>reg query</strong> switches from? According to <strong>reg query /?</strong>, the only switches available are /v, /ve and /s.<br/><br/>This line <strong>START /WAIT ".\10.0\msiexec /a /forcerestart Osce10.msi"</strong> should be <strong>START /WAIT ".\10.0\msiexec /a Osce10.msi /forcerestart"</strong><br/><br/>You might also consider turning on the /passive switch for msiexec. As long as you're writing a batch file, you may not want to be bothered with those pesky interactive prompts.<br/><br/>Try using the /? switch for command line help. There is a wealth of <a href="https://interviewquestions.tuteehub.com/tag/information-238790" style="font-weight:bold;" target="_blank" title="Click to know more about INFORMATION">INFORMATION</a> awaiting you.<br/><br/> Sidewinder,<br/><br/>The switches are from various other boards - and I have used them before - but here they are not working for me now - no idea if they are version specific, or to do with the expanded syntax that can be installed from the server CDs!!??!!<br/><br/>What I am trying (and failing!! GRRR!) to do is basically to find out which version of a program is installed - in this case Trend's Office Scan Client - , then use the correct uninstaller to remove it from the target PC, restart the PC and recheck - if it finds no key, install v10 and finally delete the files from the startup folder.<br/><br/>If you can suggest another way of doing this - I'd be most grateful!I haven't got those keys on my system, but assuming you typed out the correct keys, you may need to do something like this:<br/><br/> Code: <a>[Select]</a>echo off<br/>for /f "skip=4 tokens=1-3*" %%i in ('REG QUERY HKLM\Software\TrendMicro\PC-cillinNTCorp\CurrentVersion\Misc /v ProgramVer') do (<br/> set pgmVer=%%k<br/>)<br/>echo %pgmVer%<br/><br/>The %%k token will have the version number, the value of which can be persisted by setting a variable to the value of the %%k temporary token.<br/><br/>Hope this helps. Quote from: Sidewinder on March 02, 2010, 01:56:20 PM</p><blockquote>The %%k token will have the version number, the value of which can be persisted by setting a variable to the value of the %%k temporary token.</blockquote> <br/>Your going way past my level here! :-) Can you explain a bit further though, as I really would like to understand how to go that next step and setup the variable - would I use 'IF / ELSE'? Code: <a>[Select]</a>echo off<br/><br/>reg query hklm\software\trendmicro\pc-cillinntcorp\currentversion\misc /v programver<br/>if errorlevel 1 goto three<br/><br/>for /f "tokens=3" %%i in ('reg query hklm\software\trendmicro\pc-cillinntcorp\currentversion\misc /v programver') do (<br/> if %%i equ 5.58 goto one<br/> if %%i equ 8.0 goto two<br/> if %%i equ 10.0 goto five<br/> goto three<br/>)<br/><br/>:one<br/>start /wait ".\5.53\cmnunins.exe"<br/>goto four<br/><br/>:two <br/>start /wait ".\8.0\cmnunins.exe"<br/>goto four<br/><br/>:three<br/>start /wait ".\10.0\msiexec /a osce10.msi /forcerestart"<br/>exit<br/><br/>:four<br/>start c:\windows\rundll.exe user.exe,exitwindowsexec<br/>exit<br/><br/>:five<br/>:: need to add in a del command for the file in the startup menu<br/>exit<br/><br/>Based on your original code, I came up with this. I added an extra <strong>reg query</strong> so the batch file would not error out before it got to the <strong>for</strong> instruction. You might want to change the target of the <strong>goto</strong>.<br/><br/>If you run the <strong>reg query</strong> from the prompt, you'll notice that the version is in the third chunk of data on the last line. I simply skipped the other lines (including the blanks) and extracted the version to do the compares. Run <strong>for /?</strong> at the prompt for a detailed explanation.<br/><br/>The <a href="https://interviewquestions.tuteehub.com/tag/directory-11615" style="font-weight:bold;" target="_blank" title="Click to know more about DIRECTORY">DIRECTORY</a> in :one (5.53) does not match the <strong>if</strong> statement (5.58). Is there a reason for this?<br/><br/>After uninstalling an older version, why are you skipping over :three and not installing the latest and greatest version? Just curious. <br/><br/> Quote from: Sidewinder on March 02, 2010, 11:36:21 AM<blockquote>Where are you getting all those <strong>reg query</strong> switches from? According to <strong>reg query /?</strong>, the only switches available are /v, /ve and /s.<br/></blockquote> <br/>The switches he is using were added starting with Windows Vista:<br/><br/> Quote<blockquote>REG QUERY KeyName [/v [ValueName] | /ve] [/s]<br/> [/f Data [/k] [/d] [/c] [/e]] [/t Type] [/z] [/se Separator]<br/><br/> KeyName [\\Machine\]FullKey<br/> Machine - Name of remote machine, omitting defaults to the<br/> current machine. Only HKLM and HKU are available on<br/> remote machines<br/> FullKey - in the form of ROOTKEY\SubKey name<br/> ROOTKEY - [ HKLM | HKCU | HKCR | HKU | HKCC ]<br/> SubKey - The full name of a registry key under the<br/> selected ROOTKEY<br/><br/> /v Queries for a specific registry key values.<br/> If omitted, all values for the key are queried.<br/><br/> Argument to this switch can be optional only when specified<br/> along with /f switch. This specifies to search in valuenames only.<br/><br/> /ve Queries for the default value or empty value name (Default).<br/><br/> /s Queries all subkeys and values recursively (like dir /s).<br/><br/> /se Specifies the separator (length of 1 character only) in<br/> data string for REG_MULTI_SZ. Defaults to "\0" as the separator.<br/><br/> /f Specifies the data or pattern to search for.<br/> Use double quotes if a string contains spaces. Default is "*".<br/><br/> /k Specifies to search in key names only.<br/><br/> /d Specifies the search in data only.<br/><br/> /c Specifies that the search is case sensitive.<br/> The default search is case insensitive.<br/><br/> /e Specifies to return only exact matches.<br/> By default all the matches are returned.<br/><br/> /t Specifies registry value data type.<br/> Valid types are:<br/> REG_SZ, REG_MULTI_SZ, REG_EXPAND_SZ,<br/> REG_DWORD, REG_BINARY, REG_NONE<br/> Defaults to all types.<br/><br/> /z Verbose: Shows the numeric equivalent for the type of the valuename.<br/></blockquote> <br/>However, they are listing their OS as XP, which probably explains the error message regarding Too many parameters they got in their original code.<br/><br/><br/><br/></body></html> | |