View Single Post
Old 05-18-2009, 03:19 AM   #2 (permalink)
TheOutcaste
TSF Enthusiast
 
TheOutcaste's Avatar
 
Join Date: Mar 2009
Location: Portland, OR
Posts: 822
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   Reply With Quote