# creating a batch file to edit a text file



## jrgalaura

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. This is an excerpt of the file:
--------------------


line 1
line 2

<user username="*USER*" password="*pass00*"/>
line 4
...



--------------------

and it will replace the word "*USER*" and "*pass00*" with the Windows environment variable *%username%*


Can anybody here help me?


Thank you!


----------



## TheOutcaste

Welcome to TSF!
Just getting ready to head out, but got a couple of questions:
Are you wanting to replace it with %username%, or replace it with the actual _value_ of the %username% variable at the time the file is run?
Does the file contain any of these symbols in addition to the < and >?
~!%^&()|
This is just off the top of my head, haven't tested. Will probably fail as outputing a < or > without quoting the line takes more than a simple Echo.
The <, > and % symbols always mess me up without testing, so this might not work, and depending on the answers to the above, might need some tweaking:


Code:


@Echo Off
SetLocal EnableDelayedExpansion
Set _Path=C:\Test1
Set _FileName=FiletoEdit
PushD _Path
If Exist %_FileName%.tmp Del /F /Q %_FileName%.tmp
For /F "Tokens=1 delims=" %%I In (%_FileName%.txt) Do (
Set _Temp="%%I"
If /I "!_Temp:~2,17!"=="user username=" (
>>%_FileName%.tmp Echo.^<user username="^%%Username^%%" password="^%%Username^%%"/^>
) Else (
>>%_FileName%.tmp Echo.%%I
))
If Exist %_FileName%.txt Del /F /Q %_FileName%.txt
Ren %_FileName%.tmp %_FileName%.txt
PopD
EndLocal

Jerry


----------



## jrgalaura

Thanks for the welcome and the reply Jerry! 

I'm loving this site already.

To answer your question, yes the actual value of the %username% variable when the file runs. And it is just letters and numbers, no special characters included. 

I came up with this:
---------------------------------------------
@echo off > newfile.txt
setLocal EnableDelayedExpansion
*
set user=%USERNAME%
set pass=%USERNAME%*

for /f "tokens=* delims= " %%a in (c:\try\tomcat-users.txt) do (
set str=%%a
set str=!str:SYSUSER=%user%!
set str=!str:sysuser00=%pass%!
echo !str! >> newfile.txt
)

copy newfile.txt c:\try\tomcat-users.txt
---------------------------------------------

and this is a sample of 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> 
---------------------------------------------

My scripts seems to do the job, as it would only require to change both *SYSUSER*and *sysuser00* to whatever the %USERNAME% is.

The only problem I have is on the substitution of *sysuser00*(for example the value of %USERNAME% is jerry) - the output is still *jerry00*. The *two zeros* keeps on showing up, even after I enclosed it with single/double quotes:
*
set str=!str:'sysuser00'=%pass%!*
or
*set str=!str:"sysuser00"=%pass%!*

How can I make the whole thing just *jerry* - in small caps btw?

Please help me, I'm lost in the world of scripting.


----------



## jrgalaura

i mean

*SYSUSER* will be *JERRY* (of course, since the %USERNAME% environment variable is always on all caps.

But *sysuser00* will be *jerry* (same %USERNAME% but in small caps)...

sorry for the bother, i really need help :sigh:


----------



## TheOutcaste

The substring replacement is not case sensitive, and replaces ALL occurrences of the string, so *SYSUSER* matches both *SYSUSER* and *sysuser*, so the first statement changes sysuser00 to JERRY00 and the second statement can't find a match. Just reverse the statements so the sysuser00 is replaced first.
And the %USERNAME% should match whatever case was used when the account was created, at least it does on mine. All lower case on some, mixed case on others. If sign in with TheOutcaste account, it's TheOutcaste, even if I actually login typing all lower case.
If you need the User to be all caps, and the Pass all lower, you'll need to convert the username first.

You also need to remove the space between !str! and >>. That space will be output, so you are adding on a space to each line. I always like to put the redirection at the start of the line. Helps avoid that, and is easier to read if you have multiple echo statements to the same file.
I don't see why you are using a space as a delimiter. I would just set it to no delimiters.
So give this a whirl:


Code:


@Echo off
If Exist newfile.txt Del newfile.txt
SetLocal EnableDelayedExpansion
Set user=%USERNAME%
Set pass=%USERNAME%
For %%I In ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I" "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R" "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z") Do Call Set "user=%%user:%%~I%%"
For %%I In ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i" "J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r" "S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z") Do Call Set "pass=%%pass:%%~I%%"
For /f "tokens=* delims=" %%a In (c:\Test1\tomcat-users.txt) Do (
Set str=%%a
Set str=!str:sysuser00=%pass%!
Set str=!str:SYSUSER=%user%!
>>newfile.txt Echo !str!
)

HTH

Jerry


----------

