Re: creating a batch file to edit a text file
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 SYSUSERand 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.
|