Jump to content

Recommended Posts

Posted

What I want to do is the following:

I want AutoIt to open a file, search for a string of text, when found, delete the line where that string was found in, and then put something else in that file ...

Now how do I do that ?

  • Moderators
Posted

FileRead()/StringInStr()/_FileWriteToLine()

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Posted (edited)

This is what I did, but the messagebox with $result does not give me the correct line !

What do I do wrong ?

$SMTPServer = String("")
$file = FileOpen("C:\WINDOWS\SYSTEM32\Drivers\Etc\HOSTS", 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("SMTP2GO", "You didn't fill in an SMTP Server, please do so")
            EndIf
        Case @Error = 1; CANCEL
            MsgBox("SMTP2GO", "You didn't fill in an SMTP Server, please do so")
        Case @Error = 3; FAILED
    EndSelect
WEnd

If $file = -1 Then
    MsgBox("SMTP2GO", "Unable to open the required files")
    Exit
EndIf

TCPStartup()

$SMTPIP = TCPNameToIP($SMTPServer)

$fileread = FileRead($file, 1)
$result = StringInStr($fileread, "MAILSERVER")
MsgBox(0, "Search Result:", $result)
Edited by Duivelke
Posted

Now how do I do that ?

Translate this into AutoIT code.

Read the whole file into an array. Loop through the whole array and search for the string. If you find it delete that array element. When you are done with the loop, write the array back to a file.

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 *

Posted

OK, I looked at it a little and I found some things that works allright.

But problem is still --> it doesn't write to the file, it doesn't change a thing ...

Someone sees what the problem is ???

#include <File.au3>
#include <Array.au3>

SplashImageOn ("", "splash.gif", 400, 263, -1, -1, 1)
Sleep(5000)
SplashOff()

$SMTPServer = String("")
$TheFile = FileOpen("C:\WINDOWS\SYSTEM32\Drivers\Etc\HOSTS", 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("SMTP2GO", "You didn't fill in an SMTP Server, please do so")
            EndIf
        Case @Error = 1; CANCEL
            MsgBox("SMTP2GO", "You didn't fill in an SMTP Server, please do so")
        Case @Error = 3; FAILED
    EndSelect
WEnd

If $TheFile = -1 Then
    MsgBox("SMTP2GO", "Unable to open the required files")
    Exit
EndIf

TCPStartup()

$SMTPIP = TCPNameToIP($SMTPServer)

Func _StringInFile($file, $string)
    Local $avLines[_FileCountLines($file)]
    _FileReadToArray($file, $avLines)
    For $i = 1 to $avLines[0]
        if (StringInStr($avLines[$i], $string)) Then
            $words = StringSplit($avLines[$i], " ")
            for $x = 1 to $words[0]
                if (StringInStr($words[$x], $string)) Then
                    Return $words[$x]
                Else
                EndIf
            Next
        Else
        EndIf
    Next
    Return False
EndFunc

$HereItIs = _StringInFile("C:\WINDOWS\SYSTEM32\DRIVERS\ETC\HOSTS", "MAILSERVER")
$NewSMTP = $SMTPIP & @TAB & "MAILSERVER"

_ReplaceStringInFile($TheFile, $HereItIs, $NewSMTP, 0, 1)

FileClose($TheFile)

MsgBox(0, "SMTP2GO", "SMTP Server changed.")

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
  • Recently Browsing   0 members

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