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