?
Solved

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

Posted on 2011-02-25
7
Medium Priority
?
317 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 71

Accepted Solution

by:
Qlemo earned 600 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 450 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 71

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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
LVL 8

Assisted Solution

by:subhashchy
subhashchy earned 450 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 71

Assisted Solution

by:Qlemo
Qlemo earned 600 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 71

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

Industry Leaders: 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!

Question has a verified solution.

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

This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
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 …

840 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