Reading child registry key from regedit in batch file - batch-file

I want to read a registry key's child key name and assign the child key name to a variable. I am not getting an registry query to do it.
For ex: My current key is like this "ProviderName/SoftwareName/8.0". The last part 8.0 can change as and when I switch versions. I would want to read this version number.
Thanks in advance !!
Prakash

the REG command is what you will have to parse the output for.
REG QUERY HKLM\Software\Microsoft\Office
will show you office versions (as well as some common keys)
for /f "tokens=5 delims=\" %a in ('REG QUERY HKLM\Software\Microsoft\Office') do echo %a
will output the information. you will have to convert the info into a number, then test to see if it is bigger.

Related

Creating Registry Using Batch Files

I am working on creating a batch file that adds will create registry entries in order to add custom commands into the UI of our file management software.
The issue is that the location of the key varies based on the version of the software a user is on.
Is it possible to create a patch file that will search within registry keys/sub keys and create new values there?
Here is an example of the registry location, the portion surrounded by ** will change based on version:
[HKEY_CURRENT_USER\Software\Motive\M-Files\**12.0.6550.8**\Client\MFShell\Rombald\TasksBar\ShellCommands\1]
"Name"="Calendar"
"Executable"="\\\\mfiles-server\\M-Files setup\\M-Files
Calendar\\MFCalendar_v1.2.6\\MFCalendar_v1.2.6\\M-Files Calendar.exe"
"Icon"="\\\\mfiles-server\\M-Files setup\\M-Files
Calendar\\MFCalendar_v1.2.6\\MFCalendar_v1.2.6\\M-Files Calendar.exe,0"
"Arguments"="{B1438CAB-2E53-474E-AA82-3D48F787F6B7}"
Essentially I would have to look for the version of the software and insert that into the key. If more than one exists it would be the largest value.
Thanks in advance for the help!
#echo off
setlocal
for /f "delims=" %%A in (
'reg query HKCU\SOFTWARE\Motive\M-Files'
) do set "root=%%~A" & set "version=%%~nxA"
echo root: %root%
echo version: %version%
if defined root (
echo Write new reg information.
reg add "%root%\testkey" /v testvalue /d data /f
reg add "%root%\testkey" /v testversion /d "%version%" /f
)
The for loop uses the command reg query to search through
the entries in the registry key. The last entry is saved as
root and version. The variable root will have the full key path
while the variable version will be the last segment of the path
which will be just the key name. These are echoed to console.
If defined root, subkey testkey will be added with values and
data as the code shows.
So long as you only have version keys under the key of M-Files
then this may be all you may need. If not, then you may need more
code to skip the other keys.

REG ADD syntax issue

I'm trying to make a batch file that creates a entry in the registry.
The entry must be link to a executable. It needs two parameters :
(path) The folder to process (see below project1)
(path) The folder that will contain the file returned by the process. I want this to be the parent folder of the first parameter (see below DirectoryProject)
Tree :
../somewhere/my/program/program.exe
../DirectoryProject
../DirectoryProject/project1
../DirectoryProject/output.data
Exemple :
"../somewhere/my/program/program.exe" "../DirectoryProject/project1" "../DirectoryProject"
The first parameter is easy, it is %%1. It has the folder's path I right clicked as value. The last parameter is however harder to get.
I first tried to manipulate the variable %%1.
reg add "%RootKey%\Software\Classes\Directory\shell\%KeyName%\command" /VE /D "cmd /c cd \"%%1\" & \"%Exec%\" \"%%1\" \"%%~dp1\"" /F
The program logs the parameters it reveice. The last parameter is equal to %%~dp1 so it does not work.
Then I tried a tricky way using the first parameter and the CD command
reg add "%RootKey%\Software\Classes\Directory\shell\%KeyName%\command" /VE /D "cmd /c cd \"%%1\" & \"%Exec%\" \"%%1\" \"%CD%\"" /F
In this case %CD% will always be the path of the batch file so clearly not what I want nether
I'm still looking
Thanks for your interest and help
If the value of %1 is a fully qualified and existing path then the root folder would generally be the drive letter followed by a backslash, normally denoted by %~d1\. However, given your latest clarification, it appears your looking for the current working directory, commonly denoted as %CD%.
I'm assuming that you wanted to manipulate the second instance of %1, so based on my assessment, here's how I'd do it:
#Echo Off
Set "ExePath=C:\SomePathTo\MadeUp.exe"
Set "MenuTxt=Process with MadeUp"
Set "KeyName=LaunchTest"
Set "RootKey=HKCU"
Reg Add "%RootKey%\Software\Classes\Directory\shell\%KeyName%" /VE /D "%MenuTxt%" /F >Nul
Reg Add "%RootKey%\Software\Classes\Directory\shell\%KeyName%\command" /VE /D "\"%ExePath%\" \"%%~1\" \"%CD%\"" /F >Nul
Please make sure that you modify your specific executable path on line 2 as required.
Note that the text description you want in the context menu, currently Process with MadeUp can be changed to suit your preference, you can do that on line 3.
I have left your registry key name as you had it, LaunchTest, this can be changed in line 4, (when it's no longer a test).
A user menu entry should be entered into the HKCU key, so I'm using that.(If you want it for all users then change HKCU to HKLM on line 5. Whilst it does not match your provided key, it is the correct way to do it. Entries from both HKCU\Software\Classes and HKLM\SOFTWARE\Classes are auto propagated to HKCR, you shouldn't add things directly to it.If you change it to HKLM be aware that there's a likelihood the script will need to be run As administrator)
If you had wanted the root directory of the right clicked folder then you'd change \"%CD%\" to \"%%~d1\\\" on line 7.

How to use FINDSTR inside of a FOR loop to delete registry keys?

I want to delete all *.bak profile registry keys off all computers at work.
We gave everyone temporary profiles a while back, and we're trying to remove them now. The .bak registry keys are forcing temp profiles where people don't want them.
I have this command that LISTS all the *.bak registry keys:
for /f %f in (windowscomputers.txt) do reg query "\\%f.mydomain.com\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" | findstr /E bak
But that just lists them. I need to delete them.
And I TRIED to delete the *.bak keys off of just one workstation as a test:
for /f %f in ('reg query "\\workstationFQDN\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" ^| findstr /e bak') do reg delete \\workstationFQDN\%f
The above tries to delete \\workstationFQDN\HKLM\SOFTWARE\Microsoft\Windows. Even though I have "" around the path, it's breaking at the space between Windows and NT.
How do I use findstr inside of a for loop, so that it executes on each of the registry keys that findstr finds?
After that, I need to do a nested for loop so that it deletes each of the .bak registry keys that findstr finds, on each of the computers in my text file windowscomputers.txt.
This is a basic question. I tried googling findstr reg query for loop, and found lots of batch script examples that frankly went over my head, using options like tokens=, delims=, and %~ that I've read about in the for /? help file, but don't understand their usage or how it might apply to my specific need (delete each .bak profile registry key on each computer at work).
I've also only used one-liners in cmd prompt. I understand that batch has some syntax differences if I need to use multiple commands, like %%f instead of %f.
Try
for /f "delims=" %f ...
(And echo the reg delete... just to be safe)
(also - quote the string being deleted)
%f is referred to as a metavariable. Metavariables require one % directly from the prompt, buttwo if used within a batch file.
From the prompt, use for /? |more for (somewhat cryptic) documentation about the delims= (and other) options. Or look through some examples here on SO.
(as a batch line) the following listed the appropriate keys for me (I can't duplicate your setup)
FOR /f "tokens=1*delims=\" %%q IN ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"') DO ECHO %%r
Prefixing "HKLM" above with "\machinename\" and filtering using a findstr filtered out the unwanted lines. %%r contained \software\..., so all you'd appear to need is to pop the prefix string \\machinename\HKLM\ before that and your command should be ready-to-go.

Put value in registry using .reg or .bat or .vbs with REG_EXPAND_SZ type

My reg:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\cultest]
#="URL:cultest Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\cultest\DefaultIcon]
#="%APPDATA%\\musicDownloader\\test.bat,0"
[HKEY_CLASSES_ROOT\cultest\shell]
[HKEY_CLASSES_ROOT\cultest\shell\open]
[HKEY_CLASSES_ROOT\cultest\shell\open\command]
#="%APPDATA%\\musicDownloader\\test.bat \"%1\""
This .reg creating keys but my problem is that I want to use %APPDATA% and I can't do it with REG_SZ. It's possible to use with REG_EXPAND_SZ but I don't know how to change it.
You can put the following into a .bat file:
reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run" /v "SomeValue" /t REG_EXPAND_SZ /d "%SomeVariable%"
So for you example, you could probably use:
reg add "HKEY_CLASSES_ROOT\cultest\shell\open\command" /v "musicDownloader" /t REG_EXPAND_SZ /d "%APPDATA%\\musicDownloader\\test.bat \"%1\""
The above is untested but should give an idea of how to add reg values for a .bat file.
From your comment on the other answer, I assume you actually want to put the literal string "%APPDATA%" into the registry key, rather than have %APPDATA% resolve itself to its actual value.
This can be accomplished by escaping the special characters, i.e. the %'s, in the batch file. You simply replace every % with %%.
I must admit I've never seen someone use a registry key like that though. They're usually the actual path you want to use. If this registry key is for your own program, you're of course welcome to go ahead and do whatever you like when reading/writing the keys. But if you're trying to use someone else's program I'd question if what you are trying to do is actually what you mean to do. For example, if you want the %APPDATA% to resolve differently based on the user of the computer, you can put those registry keys into the HKEY_CURRENT_USER area, and then each user can have their own value of it.

Reg query/Reg delete key with embedded & failure

I am attempting to perform a registry query as per the following in a Win XP reg v3.0 via batch file environment:
reg query /v HKLM\SOFTWARE\DSAC\usb\vid_08ec&pid_204c
I am receiving argument errors truncating at the &. If I escape ^&, the query parses the full key but fails to locate the registry key because, I believe, the search key is then vid_08ec^&pid_204c.
Can anyone advise how I can achieve this reg query and deletion against the vid_08ec&pid_204c string?
Try like this :
reg query /v "HKLM\SOFTWARE\DSAC\usb\vid_08ec&pid_204c"

Resources