Computing.Net > Forums > Programming > Create a batch file to edit text file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Create a batch file to edit text file

Reply to Message Icon

Name: babyj (by jrgalaura)
Date: May 13, 2009 at 02:41:43 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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!



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: May 13, 2009 at 04:15:15 Pacific
Reply:

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


0

Response Number 2
Name: babyj (by jrgalaura)
Date: May 14, 2009 at 04:13:08 Pacific
Reply:

wow! thank you Mechanix2Go!

will try out this one right away =)


0

Response Number 3
Name: babyj (by jrgalaura)
Date: May 18, 2009 at 00:35:07 Pacific
Reply:

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.


0

Response Number 4
Name: Mechanix2Go
Date: May 18, 2009 at 04:29:49 Pacific
Reply:

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


0

Response Number 5
Name: babyj (by jrgalaura)
Date: May 18, 2009 at 18:57:07 Pacific
Reply:

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-(


0

Related Posts

See More



Response Number 6
Name: babyj (by jrgalaura)
Date: May 18, 2009 at 18:59:13 Pacific
Reply:

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*


0

Response Number 7
Name: Mechanix2Go
Date: May 18, 2009 at 22:26:17 Pacific
Reply:

No idea why.


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

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Create a batch file to edit text file

Create a batch file to rename text www.computing.net/answers/programming/create-a-batch-file-to-rename-text/14900.html

Help for create a batch file www.computing.net/answers/programming/help-for-create-a-batch-file/15796.html

Create a Batch file to extract data www.computing.net/answers/programming/create-a-batch-file-to-extract-data/20346.html