Duivelke Posted September 16, 2006 Posted September 16, 2006 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 SmOke_N Posted September 16, 2006 Moderators Posted September 16, 2006 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.
Duivelke Posted September 16, 2006 Author Posted September 16, 2006 (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 September 16, 2006 by Duivelke
/dev/null Posted September 16, 2006 Posted September 16, 2006 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.CheersKurt __________________________________________________________(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 *
Duivelke Posted September 16, 2006 Author Posted September 16, 2006 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 ??? expandcollapse popup#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.")
randallc Posted September 16, 2006 Posted September 16, 2006 (edited) Hi, You still have not done "fileclose" when trying to write the new line, so file is locked in fileopen at the time.; move the "fileclose " up in the script. Best, Randall Edited September 16, 2006 by randallc ExcelCOM... AccessCom.. Word2... FileListToArrayNew...SearchMiner... Regexps...SQL...Explorer...Array2D.. _GUIListView...array problem...APITailRW
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now