Solved

update a string value with a text file by a DOS script

Posted on 2011-02-25
7
307 Views
Last Modified: 2012-05-11
is it possible to write a DOS batch to replace a string value in a text file with the %username% variable?  

For example, if I have a text file:
[hklm\system\currentcontrolset\control\session manager\environment]
"idata"="johnt\\ivalue"

after Maryh logins and run the script, the text file will become:
[hklm\system\currentcontrolset\control\session manager\environment]
"idata"="maryh\\ivalue"

assuming this text file is under c:\temp\test.txt.  Please feel free to correct the format of my test.txt file.  

Thanks.
0
Comment
Question by:nav2567
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 70

Accepted Solution

by:
Qlemo earned 200 total points
ID: 34984755
It is possible, and in your case not very complicated. But are you aware that the above is only done once on each machine? Wouldn't it be better to to use the HKCU branch with a REG_EXPAND_SZ of %UserName%?

The batch file would look like this, if the source text file contains $UserName$ where the user name needs to be put in:
@echo off
setlocal EnableDelayedExpansion
(for /F "tokens=* delims=" %%A in (C:\temp\test.txt) do (
  set line=%%A
  echo,!line:$UserName$=%UserName%!
) > c:\temp\result.txt

Open in new window

0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 150 total points
ID: 34984766
Instead of replacing the string you can have a batch file that recreates the file when executed.  Something like this:

envupdate.bat
@echo off
echo [hklm\system\currentcontrolset\control\session manager\environment] >c:\temp\test.txt
echo "idata"="%username%\\ivalue" >>c:\temp\test.txt

Open in new window

0
 
LVL 70

Expert Comment

by:Qlemo
ID: 34984787
And as you (most likely) do not need to use a file, but change the registry key directly:
reg add /f "hklm\system\currentcontrolset\control\session manager\environment" /v idata /d "%UserName%"

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Assisted Solution

by:subhashchy
subhashchy earned 150 total points
ID: 34985105
Just add \ivalue after "/d "%UserName%" " in Qlemo's command. seems he missed (Typo).
The command would be
reg add /f "hklm\system\currentcontrolset\control\session manager\environment" /v idata /d "%UserName%"\ivalue

Open in new window


0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 200 total points
ID: 34985130
Oops! Yes, I forgot that, but it then needs to be inside the quotes, too:
reg add /f "hklm\system\currentcontrolset\control\session manager\environment" /v idata /d "%UserName%\ivalue"

Open in new window

0
 

Author Closing Comment

by:nav2567
ID: 35063157
Thanks
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 35063178
Thanks for the points.
Just for the records: Are you aware that a "B" grade is like having said "useful information, but not totally applying to my situation, or incomplete"?
0

Featured Post

Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question