Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 



Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > The IT Pro > Programming
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Programming A discussion forum for programs and programming used in tech-related businesses.

Reply
 
LinkBack Thread Tools
Old 05-18-2009, 02:12 AM   #1 (permalink)
Registered User
 
Join Date: May 2009
Posts: 4
OS: Win XP SP 3


Post creating a batch file to edit a text file

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!
jrgalaura is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Old 05-18-2009, 03:19 AM   #2 (permalink)
TSF Enthusiast
 
TheOutcaste's Avatar
 
Join Date: Mar 2009
Location: Portland, OR
Posts: 759
OS: MS-Dos 6.22 - Win7


Re: creating a batch file to edit a text file

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
__________________
Microsoft MVP - Windows Desktop Experience
Of course I know all the answers; I just don't always match the answers to the right questions.
Rated R for Violence -- When your PC flies through a window, that's violent, right?
TheOutcaste is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 05-19-2009, 12:09 AM   #3 (permalink)
Registered User
 
Join Date: May 2009
Posts: 4
OS: Win XP SP 3


Red Faced 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.
jrgalaura is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 05-19-2009, 12:13 AM   #4 (permalink)
Registered User
 
Join Date: May 2009
Posts: 4
OS: Win XP SP 3


Red Faced Re: creating a batch file to edit a text file

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
jrgalaura is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 05-19-2009, 02:18 AM   #5 (permalink)
TSF Enthusiast
 
TheOutcaste's Avatar
 
Join Date: Mar 2009
Location: Portland, OR
Posts: 759
OS: MS-Dos 6.22 - Win7


Re: creating a batch file to edit a text file

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
__________________
Microsoft MVP - Windows Desktop Experience
Of course I know all the answers; I just don't always match the answers to the right questions.
Rated R for Violence -- When your PC flies through a window, that's violent, right?
TheOutcaste is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 03:39 AM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85