Batch Script that extracts a value from registry and replaces values on a ini file

xpertoPC
xpertoPC used Ask the Experts™
on
Hello, im looking for a simple batch script that i can edit easy that does this:

The batch scrip should extract from registry the value of the "Server ID"={M5668ABA} and replace this value in several lines on a Config.ini file

The "Server ID" value changes on every computer so if a run the batch script on evey computer should give me the Config.ini file with the new values of each computer.

BatchScript.bat
[HKEY_CURRENT_USER\Program]
"Server ID"="{M5668ABA}" <--- Value to be extracted from registry

This is the Config.ini file where VALUETOVEREPLACED should be replaced with the extracted from the registry = {M5668ABA}

Config.ini
[HKEY_CURRENT_USER\ProgramConfiguracion\VALUETOVEREPLACED\Uptime]
"Language"="Jap"
"Other"="VALUETOVEREPLACED"
"VALUETOVEREPLACED"="00X"


The final Config.ini should be like this:

Config.ini
[HKEY_CURRENT_USER\ProgramConfiguracion\{M5668ABA}\Uptime]
"Language"="Jap"
"Other"="{M5668ABA}"
"{M5668ABA}"="00X"

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Is it ok to generate the Config.ini from scratch each time? Replacing thru batch file is more complicated then generating new files.

Author

Commented:
That works too but it has to be very simple, because what i need would make it for me impsible to edit because the batch script will grow very very much and it would take very long time to edit it because the config.ini file is very big in many cases.

  For exaple:

The normal or original code on .ini file that im going to convert to .reg file, so we can work on a .reg file.

This is the code that i can insert to the batch script as it is i can acept to modify only the value to be replaced.

[HKEY_CURRENT_USER\ProgramConfiguracion\VALUETOVEREPLACED\Uptime]
"Language"="Jap"
"Other"="VALUETOVEREPLACED"
"VALUETOVEREPLACED"="00X"

Because if i need to edit the original code on the batch script to something like this i will never end because of the big original config.ini or .reg files. (Its just too confusing for me and many many lines to edit)

REG ADD "HKEY_CURRENT_USER\ProgramConfiguracion\VALUETOVEREPLACED\Uptime" /f /v "" /t REG_SZ /d "Language"

Is something like this what you where telling me?
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Hmm, kind of confusing. Are you talking about creating a reg file, which can be applied automatically (respective direct registry changes) or not, or create a INI file looking similar to a reg file?

And are both the batch file and the generated / changed file big? This is important to know - to prefer a easy solution versus a speedy one.
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Author

Commented:
Let me explain it with a real example i hope i can explain my self a little better. The thing is that i prefer it on a way that i can understand and edit the files easyly so i can use it on diferent batch scripts or reg files.

This batch script below its already working and does the work directly on the registry but only configures a couple of registry lines, but when i have a lot of lines i want to configure it takes a lot of time to edit and its a little confusing for me so im looking to do this the easy way for me so i can use it on other similar configurations.

Batch Script:
FOR /F "skip=4 tokens=5" %%I IN ('REG QUERY "HKCU\Identities" /v "Default User ID"') DO (
<--- Extracts the value i need.
REG ADD "HKCU\Identities\%%I\Software\Microsoft\Outlook Express\5.0" /f /v "Store Root" /t
<--- Sets and writes the value i extracted on the path that i need to configure.
REG_EXPAND_SZ /d "C:\New\Email\DBPath"
<--- Values or configurations for the registry that i already have but want to write on the correct registry path.
)

What i want can be scripts something like this:

A script like the one i just explained but much easyer to understand and edit, probably one that replaces or edits te variable aded on the same batch script and applays it directly to the registry.

--- Begin of example of the script i want somehow to be if posible ---
* Batch Script that extracts the value:
FOR /F "skip=4 tokens=5" %%I IN ('REG QUERY "HKCU\Identities" /v "Default User ID"') DO (

* On the same batch script i would like to add all the original registry code, only that i just i modify or edit the variable that i want replace with the one extracted.

[HKEY_CURRENT_USER\Identities\VARIABLETOBEREPLACED\Software\Microsoft\Outlook Express\5.0\Mail]
"Accounts Checked"=dword:00000001
--- End of example of the script i want somehow to be if posible ---


Or probably the other way is a batch scrip that extracts the variable and edits and replaces the extracted variable on the external original .reg file.

Thanks for your help.
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
Ok, I think I understood. Your problem is the amount of registry entries respective the obstruction of changing information versus static one.

One way is to use chgstr.exe (http://www.paulslore.com/utils/chgstr.zip) to replace text in a file. I reckon this is the easiest way for you. You construct your registry template file, and the script generates and executes the corresponding file.

I suppose the value used to replace data is always Default User ID, so I set it as a static part of the batch file. It will apply all .reg files found in the same folder as the batch file. Optionally you can provide a single .reg file name as an argument when calling. The reg files used need to have the proper format for REG IMPORT, i.e. best created by REG EXPORT.

@echo off
REM %1 = Registry template file to use 
REM if none, all .reg files will be used.
set files=%1
if not defined files set files=*.reg 
 
FOR /F "skip=4 tokens=5" %%I IN ('REG QUERY "HKCU\Identities" /v "Default User ID"') DO set UID=%%I
 
(for %%F in (%files%) do chgstr VARIABLETOBEREPLACED %UID% %%F /c 2>nul ) > results.reg
reg import results.reg
 
rem del results.reg

Open in new window

Author

Commented:
Qlemo it works very good thanks, is there a way to do this without chgstr file, just with the batch file? and the reg file writen in the same batch script?, something like the one i posted but easyer to manage?

Can you explain what are this values for so i can understand a little bit more.

What does this values mean in general on a batch script?
%1 (Is it like all files or something like that?)
%%I (This i have no idea)

I have this other post maybe you can do something to solve it.
http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_24583273.html

Thank you very much for your help.

Author

Commented:
You are very good, thank you very much for your help
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Strictly seen, I've done too much for that little points already. If you want to attract experts, allow for more points. This question alone was worth at least 125 points. With your pointer question you will not have luck, I think.

You can follow this frame:
  20 points question = easy to answer, with one or two sentences.
125 points question = some work needed, but not too difficult
250 points question = master level question
500 points question = guru level question, needs experience and/or some programming / scripting, or interactive work thru asking and answering.

You will get real attention only with points above 100. 20 points tell us experts "it would be good to know, but it is not all important to any degree".

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial