Solved

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

Posted on 2011-02-25
7
305 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
7 Comments
 
LVL 69

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 69

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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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 69

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 69

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
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 …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

696 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