Solved

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

Posted on 2011-02-25
7
302 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 68

Accepted Solution

by:
Qlemo earned 200 total points
Comment Utility
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
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 8

Assisted Solution

by:subhashchy
subhashchy earned 150 total points
Comment Utility
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 68

Assisted Solution

by:Qlemo
Qlemo earned 200 total points
Comment Utility
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
Comment Utility
Thanks
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction You may have a need to setup a group of users to allow local administrative access on workstations.  In a domain environment this can easily be achieved with Restricted Groups and Group Policies. This article will demonstrate how to…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
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…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now