Sign in to follow this  
Followers 0
zvd

StringReplace

5 posts in this topic

Hello, I have a file that has several lines in it that may contain the following:

Users="adomain.user" or Users="adomain.user","another.user" etc. There may be 1 to 5 different "adomain.user" entries in that line.

Using StringReplace, I'm trying to remove the entire line, and replace it with Users="mydomain.myuser". The code I'm using removes the User= alright, and replaces it with Users="mydomain.myuser" OK, but leaves all the other "adomain.user" entries. Could anyone tell me how to achieve what I'm trying to do?

Code:

$szFile = "c:\temp\reg.ini"

$var = "Users="

$answer = ("Users="""& @LogonDomain &"."& @Username & """")

$szText = FileRead($szFile,FileGetSize($szFile))

$szText = StringReplace($szText, $var, $answer)

FileRecycle($szFile)

FileWrite($szFile,$szText)

Share this post


Link to post
Share on other sites



"adomain.user" entries. Could anyone tell me how to achieve what I'm trying to do?

Use FileReadLine() and check each line for "user=".

If the string is part of the line, replace it with a new line and write it to a tempfile with FileWriteLine.

If the string is NOT part of the line, write the original line to a tempfile with FileWriteLine.

See helpfile for FileReadLine(), etc.

Cheers

Kurt


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

Unfortunately, there are also alot of lines in the file with user in it (without the =). It seems that FileReadLine ignores the = when I specify user=, and returns all lines with user in it.

Share this post


Link to post
Share on other sites

It seems that FileReadLine ignores the = when I specify user=, and returns all lines with user in it.

????

$file = FileOpen("C:\test.txt")
$newfile = FileOpen("C:\testnew.txt")

while 1
   $line = FileReadLine($file)
   if @error = -1 then exitloop
   if StringInStr($line, "user=") then 
      $newline = "myuser=xxxxxx"; or whatever you like here !!
      FileWriteLine($newfile, $newline)
   else 
      FileWriteLine($newfile, $line)
   endif
wend
FileClose($file)
FileClose($newfile)

Cheers

Kurt


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

Thanks alot for the help! That worked.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0