Sign in to follow this  
Followers 0
Duivelke

Replace if Exist

5 posts in this topic

OK, I'm trying to do the following:

Some text needs to be added to a file.

If some part of the text is found, it needs to replace it.

If it is not found, it needs to be added.

How can I do this ?

Share this post


Link to post
Share on other sites



OK, I'm trying to do the following:

Some text needs to be added to a file.

If some part of the text is found, it needs to replace it.

If it is not found, it needs to be added.

How can I do this ?

This can be done with two seperate file handles, one for reading and one for writing.

Here is a example of a similar Problem i had, this does not match your problem exactly, but i think you can see the mechanism (i'll write it in autoit pseudo code):

;First open two handles

$HandleRead = FileOpen(Your original file)

$HandleWrite = FileOpen(tempfile)

;Check if file exists

if $HandleRead = -1 or $HandleWrite = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

;read the file line by line

While 1

$line = FileReadLine($HandleRead)

If @error = -1 Then ExitLoop

$part = StringSplit($line, "=")

If $part[1] = "Your search string" or $part[1] = "another search string" Then

;replace your text here by writing the new text in your tempfile (using write handle)

Else

;add your new text here (using the write handle)

EndIf

Wend

FileClose($HandleRead)

FileClose($HandleWrite)

FileDelete("your original one")

fileMove("tempfile", "new original")

Of course you have to modify the if statement to your own needs, but this mechanism will do what you want

Share this post


Link to post
Share on other sites

And how do I replace the text ?

Hereby the script, I added a comment where it needs to delete the line and write ...

$SMTPSERVER = String("")
$HandleRead = FileOpen ("C:\WINDOWS\SYSTEM32\DRIVERS\ETC\HOSTS", 1)
$HandleWrite = FileOpen ("C:\WINDOWS\SYSTEM32\DRIVERS\ETC\HOSTS.TMP", 1)

While $SMTPSERVER = ""
    If Not IsDeclared ("$sInputBoxAnswer") Then Dim $sInputBoxAnswer
    $sInputBoxAnswer = InputBox("SMTP2GO", "Please enter the SMTP Server you want to use.",""," ", "-1", "-1", "-1", "-1")
    Select
        Case @Error = 0; OK
            $SMTPSERVER = $sInputBoxAnswer
            if $SMTPSERVER = "" Then
                MsgBox(0+16+0+4096+262144, "SMTP2GO", "No SMTP Server given.  Please retry !")
            EndIf
            
        Case @Error = 1; CANCEL
            MsgBox(0+16+0+4096+262144, "SMTP2GO", "No SMTP Server given.  Please retry !")
            
        Case @Error = 3; FAILED
    EndSelect
WEnd

if $HandleRead = -1 or $HandleWrite = -1 Then
    MsgBox (0, "Error", "Unable to open required files.")
    Exit
EndIf

TCPStartup()

$SMTPIP = TCPNameToIP( $SMTPSERVER )

While 1
    $line = FileReadLine($HandleRead)
    If @Error = -1 Then ExitLoop
    $part = StringSplit($line, "=")
    If $part[1] = "MAILSERVER" Then
    ;here it has to remove the line that has the word MAILSERVER in it
    ;and then write @CRLF & $SMTPIP & @TAB & "MAILSERVER"
    Else
        FileWrite($file, @CRLF & $SMTPIP & @TAB & "MAILSERVER")
    EndIf
    
FileClose($file)

Thanks

Share this post


Link to post
Share on other sites

$search = FileFindFirstFile("C:\Programme\Cisco Systems\VPN Client\Profiles\*.pcf")

While 1

$file = FileFindNextFile($search)

If @error Then ExitLoop

IniWrite ( $file, "main", "GroupName", "NewGroup" )

WEnd

FileClose($search)

this script search for "Groupname" in all *.pcf files.

------old *.pcf------

GroupName OldGroup

------old *.pcf------

------new *.pcf------

GroupName NewGroup

------new *.pcf------


SYS 64738

Share this post


Link to post
Share on other sites

I still don't get it, which command do I need to use ?

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