I want to loop through all registry entries in a certain path and see if any of those match a string. I have the below bat file command:
reg query HKLM /f Software\Microsoft\Windows\CurrentVersion\Uninstall /v ProductName /s|findstr /r "abc - def (x64)"
I am using the %errorlevel% to determine the output of the above command. Expecting 0 when a registry exactly matching is found and non-zero when such a registry is not found.
However, I am getting 0 in both cases, i.e. a registry matches the string or it it doesn't.
Can you please suggest any correction to my bat command.
Related
In order to get the current Office installation path,
I set up this line
reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WINWORD.EXE"
and the result is:
(Default) REG_SZ C:\PROGRA~1\MICROS~1\Office16\WINWORD.EXE
Path REG_SZ C:\Program Files\Microsoft Office\Office16\
useURL REG_SZ 1
SaveURL REG_SZ 1
How to grep out the "C:\Program Files\Microsoft Office\Office16\" in a variable?
Thanks.
By using the command line
reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\winword.exe" /v Path
just the string value of Path is output which means on Windows XP:
! REG.EXE VERSION 3.0
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\winword.exe
Path REG_SZ C:\Program Files\Microsoft Office\Office16\
So this output starts with an empty line, a header of reg.exe version 3.0, one more empty line, the queried registry key and the queried registry value Path if found at all in Windows registry under specified key. There is a tab character between Path and REG_SZ and one more tab character between REG_SZ and the path string. The line with Path starts with four spaces.
On Windows Vista and later Windows versions the output is:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\winword.exe
Path REG_SZ C:\Program Files\Microsoft Office\Office16\
This output starts with an empty line like on Windows XP. But output is next already the queried registry key without any additional header. Last the line with queried registry value Path is output if found at all in Windows registry under specified key. The last line starts also with four spaces like on Windows XP. But there are four spaces between Path and REG_SZ and four spaces between REG_SZ and the path string instead of horizontal tabs.
This means for getting the path string using command FOR with option /F:
The first two lines of output of command REG can be always skipped.
It is necessary to check if third line contains already value Path or one more non empty line needs to be processed to have a batch file working also on Windows XP.
The last line has space or tab separated the strings Path, REG_SZ and the path string which could contain also 1 or more spaces or any other character allowed in a folder name.
The batch file code for this task:
#echo off
for /F "skip=2 tokens=1,2*" %%A in ('%SystemRoot%\System32\reg.exe query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\winword.exe" 2^>nul') do (
if /I "%%A" == "Path" if not "%%~C" == "" set "OfficePath=%%~C" & goto FoundPath
)
echo Could not determine MS Office path. MS Office is most likely not installed.
echo/
pause
goto :EOF
:FoundPath
rem Remove backslash at end of path if there is one at all.
if "%OfficePath:~-1%" == "\" set "OfficePath=%OfficePath:~0,-1%"
echo MS Office is installed in: "%OfficePath%"
rem Other command using environment variable OfficePath.
The command FOR executes in a background process started with cmd.exe /C the command line (with %SystemRoot% already expanded):
%SystemRoot%\System32\reg.exe query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\winword.exe" 2>nul
The output of this command process (= reg.exe) written to handle STDOUT is captured by FOR.
REG outputs to handle STDERR an error message if either the specified registry key or the specified registry value does not exist at all in Windows registry. This error output is redirected to device NUL to suppress it. Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded reg command line with using a separate command process started in background.
The command FOR could have nothing to process in case of registry key or registry value not found by REG resulting in execution of the command lines below the FOR loop informing the batch file user about this use case.
Otherwise command FOR skips because of skip=2 the first two captured lines which means on Windows Vista and later the first line processed by FOR is already the line containing Path. On Windows XP the third line being an empty line is ignored by FOR and the next line with queried registry key is processed next.
The line is split up because of tokens=1,2* and the default delimiters space/tab into 3 substrings.
On Windows Vista and later Windows:
Path is assigned to specified loop variable A.
REG_SZ is assigned to next loop variable B according to ASCII table.
C:\Program Files\Microsoft Office\Office16\ is assigned to loop variable C.This third string is not further split up on spaces/tabs because of *.
On Windows XP the first line tokenized by FOR results in:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App being assigned to specified loop variable A and
Paths\winword.exe being assigned to next loop variable B.
Nothing is assigned to loop variable C because there is no more string on processed line.
A case-insensitive string comparison of value of loop variable A is made with fixed string Path to verify if FOR processed already the right line with Path value. On Windows Vista and later Windows versions this is already true on first line tokenized by FOR. On Windows XP this condition is first false and FOR processes therefore the next line now assigned the same strings to the loop variables A, B and C as on Windows Vista and later Windows versions.
On a true condition the path string assigned to loop variable C and not being an empty string is assigned to environment variable OfficePath with removing enclosing double quotes if the path string is enclosed at all in ". And next a jump is made to label FoundPath exiting the loop and continue batch file execution in code block on having the MS Office path.
In this code block first a backslash at end of path is removed if there is one at all to have assigned to environment variable OfficePath always the path string without a backslash at end independent on path string in registry not having or having a backslash at end using string substitution.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
for /?
goto /?
if /?
pause /?
reg /?
reg query /?
rem /?
set /?
From cmd.exe if you run reg query /? there is a specific switch that stands out.
/v Queries for a specific registry key values.
If omitted, all values for the key are queried.
When looking at your complete output, you are very specifically requiring the registry key value Path
So by Simply running:
reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WINWORD.EXE" /v Path
we get less noise to deal with.
With that logic, here is the batch which simply uses the : of the path string extracted as the delimiter and then joining %%a being C Drive, with %%b being rest of the path after : and we simply just join them again with the colon.
#echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2 delims=:" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WINWORD.EXE" /v Path') do (
set result=%%a
set result=!result:~-1!
set output=!result!:%%b
)
echo !output!
Just use the (javascript) regex .:(?!.*:).*
What this captures is:
.: - Drive letter
(?!.*:) - Not followed by other colons (illegal in Windows paths)
.* - Followed by the path
I tried to delete a map/key in regedit using a batch file
I used this command but this gives me an error.
REG DELETE HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Scaling] /v Scaling /f
I tried to remove Scaling without a Windows message
the error message:
ERROR: the system was unable to find the specified Registry key or value
ERROR invalid key name
Start Regedit and check if that key exists and has some values set:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Scaling
If it does, it's value can be changed by running the command in Admin Cmd Prompt:
REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Scaling" /v Scaling /f REG_DWORD /d 96
You can also delete the entire key, but not its value, though it can be set to 0. You need to try what works for a particular setting, it may vary for different apps.
You copied a square bracket from the reg file. Remove it
DELETE HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Scaling HERE - ] /v Scaling /f
I'm trying to determine, in a batch file, if a particular registry key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion /v DevicePath
in Windows contains the following value:
%SystemRoot%\inf
The code I have so far is:
reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion /v DevicePath | find "%SystemRoot%\inf"
Unfortunately %errorlevel% always returns 1 and not 0, despite several attempts at playing with the command, adding and removing %'s and escape characters. Can someone assist please? Is there a better way to determine the value of this key?
to escape the percent signs in batch files, double them:
reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion /v DevicePath |find "%%SystemRoot%%\inf"
I've written a batch file that tests for a user's Microsoft Office version, copy an Excel Add-In to their device from a shared drive, and add a registry key to their device.
Each individual action in the batch works as designed. But when I combine them all together, the file doesn't like the REG add command and completely closes the command window (even if I put Pause after the REG add line).
To troubleshoot, I created a new batch file and added pieces of my code to it, one section at a time, and tested the file each time a new section was added. Every section ran fine until I got to this section:
CHDIR "C:\Windows\System32"
IF NOT %ALREADY_ENABLED%=="TRUE" (
REM Add the new key value if it doesn't exist already.
REG add HKCU\Software\Microsoft\Office\15.0\Excel\Options /v OPEN /t REG_SZ /d "%PATH%" /f
)
To see if the problem was my IF statement, I commented out the REG add line and put ECHO Hello World inside the IF statement. The file ran just fine and gave the Hello World output as expected.
I know the REG add command works because I have a batch file that only includes that piece of code and it works just fine:
#Echo off
setlocal enableDelayedExpansion ENABLEEXTENSIONS
chdir "C:\Windows\System32"
SET PATH=\"C:\Program Files (x86)\Microsoft Office\Office15\Library\Cerner_AddIn.xlam\"
REG add HKCU\Software\Microsoft\Office\15.0\Excel\Options /v OPEN /t REG_SZ /d "%PATH%" /f
I think the problem has something to do with the PATH variable, but I'm dumbfounded as to why this works in one file but not the other. Is it possible that the value of PATH is somehow changing during runtime after it's been set?
I'm not sure how to trap the error to even see what error is being thrown here. Everything I've tried to handle the error with doesn't work and the command window closes. Any ideas on what I'm doing wrong here?
Maybe this code snippet could help:
SET "myPATH=C:\Program Files (x86)\Microsoft Office\Office15\Library\Cerner_AddIn.xlam"
IF NOT "%ALREADY_ENABLED%"=="TRUE" (
REM Add the new key value if it doesn't exist already.
REG add HKCU\Software\Microsoft\Office\15.0\Excel\Options /v OPEN /t REG_SZ /d "%myPATH%" /f
)
Note:
do not change system environment variable PATH; use another variable name (myPATH);
quoting in set "variablename=variable value";
quoting in if statement;
to trap any error in a batch file, use echo ON while debugging.
Resources (required reading):
(command reference) An A-Z Index of the Windows CMD command line
(additional particularities) Windows CMD Shell Command Line Syntax
In my batch file I started to use variables and suddenly the following commands do not work anymore.
Here is the part of my code with the problem
SET "path=MyPath"
REG ADD "HKCU\Software\ETC\ETC" /f /v "MyRegNameA" /t REG_SZ /d "%path%\ETC\"
REG ADD "HKCU\Software\ETC\ETC" /f /v "MyRegNameB" /t REG_SZ /d "%path%"
PAUSE
START "" "%path%\MyProgram.exe"
This code works without the SET... and of course with MyPath instead of %path%. Error Message is:
The command "REG" is either spelled wrong or couldn't be found
I previously found how to use Variables here: stackEx.SetVariables
To my knowledge I am doing it exactly as supposed, and I couldn't find specific help so far.
path is a logical name, but it's not a good name to use as it is assigned by Windows.
path is a semicolon-separated list of the directories that Windows uses to find programs. When you change it, Windows can no longer find reg.exe since reg.exe is not in mypath.
Simply choose another name - don't use path. If you enter set at the prompt, you will see a list of many of the variables that are established by Windows. Simple rule - don't use any of them for user-variables.