Create a batch file to edit text file

May 13, 2009 at 02:41:43
Specs: Windows XP
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!


See More: Create a batch file to edit text file

Report •


#1
May 13, 2009 at 04:15:15
MYFDILE contains:
======================
line one
USERNAME PASSWORD
line three
======================

The script:

@echo off > newfile & setLocal EnableDelayedExpansion

set user=joe
set pass=abc

for /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


Report •

#2
May 14, 2009 at 04:13:08
wow! thank you Mechanix2Go!

will try out this one right away =)


Report •

#3
May 18, 2009 at 00:35:07
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 EnableDelayedExpansion

set user=abc
set pass=def

for /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.


Report •

Related Solutions

#4
May 18, 2009 at 04:29:49
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


Report •

#5
May 18, 2009 at 18:57:07
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-(


Report •

#6
May 18, 2009 at 18:59:13
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 EnableDelayedExpansion

set user=jgalaura
set pass=jgalaura

for /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*


Report •

#7
May 18, 2009 at 22:26:17
No idea why.


=====================================
If at first you don't succeed, you're about average.

M2


Report •


Ask Question