Modify part of an existing registry key value - batch-file

I want to change the value in the registry.
In a batch file I have:
ECHO Changes in reg
reg import "C:\modifySip.reg"
and in modifySip.reg I have:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Lync]
"ServerSipUri"="User.Test#domainA.com"
And it works!
However, now I want to use part of the existing registry value and modify it.
I need to keep the User.Test# part of the URI, and replace the domainA.com part, with domainB.com.

Given the information you've provided, and as mentioned in my comment, there's no need to use a .reg file for this task. I'd offer the following batch file solution:
#Echo Off
SetLocal EnableExtensions
Set "NewDomain=domainb.com"
Set "RegKey=HKCU\Software\Microsoft\Office\16.0\Lync"
Set "ValName=ServerSipUri"
For /F "EOL=H Tokens=2*" %%G In ('""%__AppDir__%reg.exe" Query "%RegKey%" /V "%ValName%" 2> NUL"'
) Do For /F "Tokens=1* Delims=#" %%I In ("%%H") Do "%__AppDir__%reg.exe" Add "%RegKey%" /V "%ValName%" /D "%%I#%NewDomain%" /F 1> NUL
Just modify line 4 to the actual domain string you require.

I used the code from Compo answer to get and add value to registry.
Changes of the string was made in that way:
set ServerSipUri=%ServerSipUri:domainA.com=domainB.com%
Thanks a lot for help.
Pablo

Related

export string value in a registry

i want to export HKEY_LOCAL_MACHINE\SOFTWARE\ABC\EFGH string XYZ value 12. i looked into regedit /e and Reg export. it gives options to export till HKEY_LOCAL_MACHINE\SOFTWARE\ABC\EFGH, but not my string value XYZ.
I can think of no native method of exporting just a particular value with its data directly as a .reg file.
The best advice would be that you parse the output from reg query for your particular value data, and save it as a reg add command to another batch file. Then when, or if, you need to replace that value data with the previously saved data, you can just run that saved batch file.
Below is a basic example, (designed only for use with value type REG_SZ). I've used a common registry subkey and value for demonstration purposes, (because yours was not clear to me); please replace those on lines 4 and 5 as per your specific requirements:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "RegistryKey=HKEY_CURRENT_USER\Control Panel\Desktop"
Set "ValueName=Wallpaper"
Set "ValueData="
For /F "Tokens=*" %%G In ('%SystemRoot%\System32\reg.exe Query "%RegistryKey%"
/V "%ValueName%" 2^>NUL ^| %SystemRoot%\System32\findstr.exe /R "\<REG_SZ\>"'
) Do (Set "ValLine=%%G" & SetLocal EnableDelayedExpansion
For /F "UseBackQ Tokens=1,*" %%H In ('!ValLine:*%ValueName%^=!'
) Do EndLocal & Set "ValueData=%%I")
If Defined ValueData Echo #%%SystemRoot%%\System32\reg.exe Add "%RegistryKey%"^
/V "%ValueName%" /T REG_SZ /D "%ValueData:"=\"%" /F 1^>NUL 1>"%ValueName%.cmd"
If the string value is successfully found, its data will be saved to a local variable %ValueData%, for further use within the script if required. In addition, a batch file with the name of the registry value, should be output to the current directory. If you wish to change that name or location, please replace %ValueName%.cmd on the last line as needed. To restore the data at a later time just run the saved batch file.

Batch File: "Environment Variable Not Defined" on a path

I have the following in a batch file
for /f "delims=" %%a in ("C:\Program Files (x86)\Wowza\creds.txt") do set %%a
net use J: https://csv/dav %p% /user:%u% /persistent:yes
I get an error:
Environment variable C:\Program Files (x86)\Wowza\creds.txt not defined
What do I need to resolve this?
Secondly, it works for all colleagues apart from one. Same laptop make, model and build. I used my details and it failed on his but worked on mine.
What fails is that it asks for the credentials to map the drive instead of taking them from the file
creds.txt
u:JoeBloggs
p:Password1234
Any idea?
Thanks
the reason for your errormessage is, your for /f loop doesn't evaluate the contents of the file. It takes a quoted string as string not as filename. Usebackq changes that behaviour.
You have another failure in your script: With your code, set %%a translates to set u:JoeBloggs, which is invalid syntax. Correct syntax requrires set u=Joebloggs. Therefore you have to split the line in a part before the colon and a part after the colon and build your set command accordingly (just set %%a would work, when the contents of the file would look like u=JoeBloggs)
Change your for loop to:
for /f "usebackq tokens=1,* delims=:" %%a in ("C:\Program Files (x86)\Wowza\creds.txt") do set "%%a=%%b"
I was going to post a similar answer to #stephan but he beat me to it. If however you have the option to change your creds.txt file to the below:
u=JoeBloggs
p=Password1234
You can shorten the for loop a bit to this:
for /f "usebackq delims=" %%a in ("C:\Program Files (x86)\Wowza\creds.txt") do set "%%~a"
which effectively just does this:
set "u=JoeBloggs"
set "p=Password1234"

Get install location from registry key value data

I basically want to know if its possible for me to get a path from the registry and use it in a Batch file.
Basically what I have is some code I've gather from this site
#echo off
reg query "HKLM\SOFTWARE\Wow6432Node\Rockstar Games\Grand Theft Auto V" /v "InstallFolder"
That line returns
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Rockstar Games\Grand Theft Auto V
InstallFolder REG_SZ D:\Rockstar Games\Grand Theft Auto V
All I want to do here is add the path of GTA to the batch file so I can then launch the executable (PlayGTAV.exe) through the batch file. The reason I'm not using the path i already know is because I want this batch file to work on some friends computers too.
#ECHO OFF
SETLOCAL
FOR /f "delims=" %%a IN ('reg query "HKLM\SOFTWARE\Embarcadero\Interbase\Servers" ^|find " REG_SZ "') DO (
SET "target=%%a"
)
SET "target=%target:*REG_SZ =%"
ECHO "%target%"
GOTO :EOF
This should demonstrate how - I've obviously used a different key.
The ^|find... finds the appropriate line containing the REG_SZ (the caret is used to tell cmd that the piping is of the reg query command, not part of the for) and the for /f "delims=" selects the entire line for application to %%a.
From there, it's simply a matter of applying substringing to the ordinary environment variable target (since substringing of the metavariable %%a is not allowed) - replace any characters before and including the first occurrence of REG_SZ+4spaces with nothing (the string between the = and terminal %

CMD Variable for registry

Alright so I was trying to delete the shell data in the registry. I can get to it and get all of the information right, but I want to automate it for all users. The one I can use right now only targets a specific file.
reg delete "HKEY_USERS\S-1-5-21-3793956547-500355711-2568367668-1002\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell\Bags" /f
What I wanted to do was skip the input for S-1-5-21 and have it target all of the keys within HKEY_USERS. This way I can get all of the shell data deleted with the press of a button.
I am not sure if there is a variable for this, or maybe I am going in the wrong direction here. Any input is appreciated and I will attempt to answer any questions I can.
To enumerate the HKEY_USERS you can Reg Query within a For /f
#Echo off
Set "Hive=HKEY_USERS"
For /F "delims=" %%A in (
'Reg Query "%Hive%" ^|findstr "%Hive%\S-1-5-21" '
) Do Echo %%A
Replace Echo with any cmd you like to execute.
Sample scrambled output:
> SO_41773670.cmd
HKEY_USERS\S-1-5-21-2140113576-3579786329-1990256020-1001
HKEY_USERS\S-1-5-21-2140113576-3579786329-1990256020-1001_Classes
HKEY_USERS\S-1-5-21-2140113576-3579786329-1990256020-1005
HKEY_USERS\S-1-5-21-2140113576-3579786329-1990256020-1006
Something like this may very well suit your needs, it is very likely language dependent, and blind automated removal of registry subkeys is not my recommendation.
#Echo Off
For /F "EOL=E Delims=" %%A In ('Reg Query HKU /S /F Bags /K'
) Do Echo=Reg Delete "%%A" /F&&Echo=Reg Add "%%A"
Timeout -1
remove the two instances of Echo= and the last line if you're happy with the output and wish to continue.

Using "for /F" command to find specific text in xml file and use as variable in a batch file

I am trying to find the following version number in a app.config file.
The file is XML format.
Line 8 in the file (Adding line in again as the greater/less than symbols were stripped from the post initially)
add key="ReleaseVersion" value="5.2.0.2"
I been using various FOR /F commands, have been close a couple of times.
However I have not been able to extract the 5.2.0.2 value and use as a variable
so far in my script.
Additionally while I am looking for this value 5.2.0.2, going forward the version number will change so I am not looking for a exact match e.g. "5.2.0.2", I am looking to capture what is in the inverted commas e.g. value="", and then using this as a variable in my script.
Example of what I have tried so far...
FOR /f "tokens=3 delims=5." %%a IN ('TYPE appsettings.config ^| FIND "ReleaseVersion"') DO SET do set word3=%%a
FOR /F delims^=^"^ tokens^=2 %%G IN ('FINDSTR /L "ReleaseVersion" "appsettings.config"')
FOR /f "tokens=3 usebackq delims== " %%G in (`appsettings.config`) do #echo %~G
Have tried a number of techniques but as yet, nothing has been successful.
Can post more information as required however that essentially covers the issue.
Supposing the add key="ReleaseVersion" value="5.2.0.2" portion is in a single line and the related value parameter appears after the ReleaseVersion substring, the following could work for you:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem define constants:
set "SEARCH=ReleaseVersion"
set "KEYWORD=value"
rem get line of interest and assign it to `LINE`:
for /F "delims=" %%L in ('findstr /L "%SEARCH%" "app.config"') do (
set "LINE=%%L"
)
setlocal EnableDelayedExpansion
rem cut everything off up to the search string:
set "LINE=!LINE:*%SEARCH%=!"
rem cut everything off up to the keyword:
set "LINE=!LINE:*%KEYWORD%=!"
rem extract the version number:
for /F tokens^=1^ delims^=^"^=^/^<^>^ %%N in ("!LINE!") do (
set "VNUM=%%N"
)
rem transfer the version number over the `setlocal`/`endlocal` barrier:
endlocal & endlocal & set "VNUM=%VNUM%"
echo ReleaseVersion: %VNUM%
exit /B
The string portion of interest does not need to look exactly like shown above, but may contain more or less spaces (for example add key = "ReleaseVersion" value = "5.2.0.2"), or include the " or not (like add key=ReleaseVersion value=5.2.0.2). The only condition is that the attribute key needs to appear before the attribute value.
If the search line is precisely this one:
add key="ReleaseVersion" value="5.2.0.2"
... then this code should work:
#echo off
setlocal
for /F "tokens=3" %%a in ('findstr "ReleaseVersion" "appsettings.config"') do set %%a
set "value=%value:~1,-1%"
echo %value%
If the layout of the search line change (more blank spaces or other characters, less quotes, etc) then previous code should need an adjustment.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q34445384.txt"
FOR /f "tokens=3delims==" %%a IN (
'findstr /L /c:"add key=\"ReleaseVersion\" value=" "%filename1%"') DO SET "release=%%~a"
ECHO release=%release%
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I used a file named q34445384.txt containing your data for my testing.
Simply find the target string using findstr and set the environment variable to the third token using delimiters of =, removing the quotes from the value with ~.
This assumes uniqueness of the target text and that the structure of the line is exactly as posted.
Assuming app.config is valid, well-formed XML, the best way to scrape the release version is to query it via XPath. You can invoke PowerShell for this.
#echo off
setlocal
set "psCommand=powershell "^
select-xml \"//add[#key^='ReleaseVersion']\" app.config ^| %%{ $_.node.value };^
""
for /f %%I in ('%psCommand%') do set "version=%%~I"
echo %version%
This will parse app.config for a node named "add" which has an attribute named "key" whose value is "ReleaseVersion", then will return that node's "value" attribute's value. for /f captures it to a batch variable.

Resources