Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
Could you guys help me make a batch file which will replace an entry in the text file - coming from an environment variable?
Basically it'll just replace the username and password:
<user username="SYSUSER" password="sysuser00"/>
coming from the environment variable %username%
Can anybody here help me?
Thank you!

MYFDILE contains:
======================
line one
USERNAME PASSWORD
line three
======================The script:
@echo off > newfile & setLocal EnableDelayedExpansion
set user=joe
set pass=abcfor /f "tokens=* delims= " %%a in (myfile) do (
set str=%%a
set str=!str:USERNAME=%user%!
set str=!str:PASSWORD=%pass%!
echo !str! >> newfile
)
=====================================
If at first you don't succeed, you're about average.M2

Thanks Mechanix2Go, I have tried your script and it worked. I'm just stuck at two parts:
This is what my script now looks like:
@echo off > newfile.txt
setLocal EnableDelayedExpansionset user=abc
set pass=deffor /f "tokens=* delims= " %%a in (c:\file.txt) do (
set str=%%a
set str=!str:USERNAME=%user%!
set str=!str:PASSWORD00=%pass%!
echo !str! >> newfile.txt
)If it's not too much of a bother, could you help me with these =(
1.) The variable PASSWORD, isn't just PASSWORD, it's PASSWORD00 (with two zeros after). Even if I do
set str=!str:'PASSWORD00'=%pass%!
or
set str=!str:"PASSWORD00"=%pass%!it will still show up as def00. How can I make the whole thing just 'def'?
2.) The output of the file is newfile.txt, but I really need it to have the same name as its input file - "file.txt".Please help me, I'm lost in the world of scripting.

I don't know why you're using quotes, but it works as posted.
set str=!str:PASSWORD00=%pass%!
As to file name, end with:
copy newfile c:\myfile
=====================================
If at first you don't succeed, you're about average.M2

thanks! gosh i didnt know what i was thinking - i was on the zone - i keep on thinking of complicated things for my #2 that i forgot that a simple 'copy' would suffice X-(

it worked on you =(
it won't work on me:
here is my file:
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="wamusers"/>
<role rolename="cisusers"/>
<role rolename="nmsusers"/>
<role rolename="mwmusers"/>
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="manager"/>
<role rolename="bausers"/>
<role rolename="admin"/>
<user username="SYSUSER" password="sysuser00" roles="cisusers,admin,manager"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="role1" password="tomcat" roles="role1"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="SPLSLK" password="SPLSLK12" roles="cisusers,admin,manager"/>
</tomcat-users>and here is my batch script:
@echo off > newfile.txt
setLocal EnableDelayedExpansionset user=jgalaura
set pass=jgalaurafor /f "tokens=* delims= " %%a in (c:\batch\tomcat-users.xml) do (
set str=%%a
set str=!str:SYSUSER=%user%!
set str=!str:sysuser00=%pass%!
echo !str! >> newfile.txt
)copy newfile.txt c:\batch\tomcat-users.xml
----------with or without the quotes it still won't work on me. *sigh*

No idea why.
=====================================
If at first you don't succeed, you're about average.M2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |