Solved

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

Posted on 2011-02-25
7
304 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
Is Your AD Toolbox Looking More Like a Toybox?

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

777 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