Jump to content

Replace if Exist


Duivelke
 Share

Recommended Posts

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

Link to comment
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

Link to comment
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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...