chewy Posted May 20, 2009 Share Posted May 20, 2009 I know there has been question asked about editing the host file before. I have been able to successfully open the host file and add in a new entry with no problem. I am also able to do a find and replace in the host file with no problem. What I want to accomplish is to be able to open a host file and search for a string of text and replace the entire line. For example: in my host file if I may or may not have a line like the following 10.0.0.1 zenwsimport However the ip address could be different, so what I would like to have happen is check for the word "zenwsimport" (that will be constant, if the line is there at all) and if it is found, edit the entire line to say: 10.0.0.2 zenwsimport Also if zenwsimport is not found I would add in the line above. So basically I know I need to read everything into an array, but that is where I am stuck. I get confused trying to delete out the array line I want removed and the new line added in. Any direction and help would be appreciated! Link to comment Share on other sites More sharing options...
Inverted Posted May 20, 2009 Share Posted May 20, 2009 (edited) There is a function to create an array with each line of the file. FileToArray or something like that Hmm, also check FileReadLine Edited May 20, 2009 by Inverted Link to comment Share on other sites More sharing options...
chewy Posted May 20, 2009 Author Share Posted May 20, 2009 There is a function to create an array with each line of the file. FileToArray or something like thatHmm, also check FileReadLinei got the array to read in, but I dont know how to find and delete out a line within the array and save the results to the original file Link to comment Share on other sites More sharing options...
Inverted Posted May 20, 2009 Share Posted May 20, 2009 (edited) i got the array to read in, but I dont know how to find and delete out a line within the array and save the results to the original fileCome on, it should be kinda easy. You need a loop to go through each line to check for the string or replace it if you want (StringReplace maybe ?)You can fill a line with empty string "" or ";" used for comments. Or you can put a ";" in front of the old line for backup.If you're stuck post some code. Edited May 20, 2009 by Inverted Link to comment Share on other sites More sharing options...
chewy Posted May 20, 2009 Author Share Posted May 20, 2009 here is what I have #include <file.au3> #include <Array.au3> Dim $zenfind Dim $aRecords If Not _FileReadToArray(@SystemDir & "\Drivers\etc\hosts",$aRecords) Then MsgBox(4096,"Error", " Error reading log to Array error:" & @error) Exit EndIf ;_ArrayDisplay($aRecords, "b") For $x = 0 to $aRecords[0] Msgbox(0,'Record:' & $x, $aRecords[$x]) ;;;;;xxxxx;;;;;msgbox (0, "test", $aRecords[0]) $zenfind = _ArraySearch ($aRecords[$x], "zenwsimport", 0, 0) msgbox (0,"zenfind", $zenfind) if $zenfind <> -1 then $aRecords[$x] = "172.20.96.2 zenwsimport" msgbox (0,"after 96.2 check",$aRecords[$x]) EndIf Next Im getting -1 everytime for the $zenfind from the array search even though I know it is in the host file Link to comment Share on other sites More sharing options...
Developers Jos Posted May 20, 2009 Developers Share Posted May 20, 2009 (edited) Parameter 1 should be just the array name ? : For $x = 0 to $aRecords[0] Msgbox(0,'Record:' & $x, $aRecords[$x]) ;;;;;xxxxx;;;;;msgbox (0, "test", $aRecords[0]) $zenfind = _ArraySearch ($aRecords[$x], "zenwsimport", 0, 0) msgbox (0,"zenfind", $zenfind) if $zenfind <> -1 then $aRecords[$x] = "172.20.96.2 zenwsimport" msgbox (0,"after 96.2 check",$aRecords[$x]) EndIf Next should be?: $zenfind = _ArraySearch ($aRecords, "zenwsimport", 0, 0) if $zenfind <> -1 then $aRecords[$x] = "172.20.96.2 zenwsimport" msgbox (0,"after 96.2 check",$aRecords[$x]) EndIf Edited May 20, 2009 by Jos SciTE4AutoIt3 Full installer Download page  - Beta files    Read before posting   How to post scriptsource   Forum etiquette Forum Rules  Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Inverted Posted May 20, 2009 Share Posted May 20, 2009 (edited) If you want a line-by-line search, look at this approach : (I used the string "example" for searching, cause I don't have that zen thingy) #include <file.au3> #include <Array.au3> Dim $zenfind Dim $aRecords If Not _FileReadToArray(@SystemDir & "\Drivers\etc\hosts",$aRecords) Then MsgBox(4096,"Error", " Error reading log to Array error:" & @error) Exit EndIf ;_ArrayDisplay($aRecords, "b") For $x = 1 to $aRecords[0] ; Msgbox(0,'Record:' & $x, $aRecords[$x]) ;;;;;xxxxx;;;;;msgbox (0, "test", $aRecords[0]) If StringInStr ($aRecords[$x], "example") Then msgbox (4096,"zenfind", "String found in line : " & $x) EndIf Next Edited May 20, 2009 by Inverted Link to comment Share on other sites More sharing options...
chewy Posted May 21, 2009 Author Share Posted May 21, 2009 thanks for the help guys, I was overthinking this. I reworked it and got it to work. Here is the code in case anyone is interested. expandcollapse popup#cs ---------------------------------------------------------------------------- AutoIt Version: 3.2.10.0 Author: Matt Majewski Date: 5.20.09 Script Function: Edits the host file to append zenwsimport IP this will add the specified IP address regardless of whether zenwsimport is already in the HOSTS File This script will either add the IP in or edit the existing. It will NOT leave the ZEN IP in the file if one exists already for some reason it adds an extra line break at the beginning of the HOSTS file when it saves. Feel free to figure out why and fix if you want! #ce ---------------------------------------------------------------------------- ; Script Start - Add your code below here #include <file.au3> #include <Array.au3> ;DEFINES ;EDIT THIS IP TO THE IP ADDRESS OF YOUR ZEN SERVER YOU WANT MACHINES TO IMPORT TO $ZENIP = "172.20.96.2" ;HOSTS file path $sFile = @SystemDir & "\Drivers\etc\hosts" ;used to define the index in the array when zenwsimport was found Dim $zenfind ;the array used to import the HOSTS file to Dim $aRecords ;Open the HOSTS file for reading into the array If Not _FileReadToArray(@SystemDir & "\Drivers\etc\hosts",$aRecords) Then MsgBox(4096,"Error", " Error reading log to Array error:" & @error) Exit EndIf ;searches the imported array to find the word "zenwsimport" ;then set the arrayindex it found it into $zenfind $zenfind = _ArraySearch ($aRecords, "zenwsimport", 0 , 0, 0, True) ;IF it found zenwsimport IF ($zenfind <> -1) Then ;change the array to include the new zen ip Number ;it changes the specific array index based on the earlier search $aRecords[$zenfind] = $ZENIP & " zenwsimport" ;open the host file for editing ;the 2 parameter will erase the contents of the file $hFile = FileOpen($sFile, 2); 2 = erase ;write the array to the open File _FileWriteFromArray($hfile, $aRecords, 1) ;close the HOSTS file that was opened FileClose($hFile) ; did not find zenwsimport Else ;open the host file for editing ;the 1 parameter will append to the end of the file $hFile = FileOpen($sFile, 1); 1 = append ;write the new zen ip to the end of the file FileWriteLine($hFile, $ZENIP & " zenwsimport") ;close the HOSTS file that was opened Fileclose($hFile) ;END IF found ZENWSImport of not EndIf Link to comment Share on other sites More sharing options...
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