schilbiz Posted February 11, 2008 Share Posted February 11, 2008 What could I add to this script so that it would not add the line to the HOSTS file more than once? Since I am planning on implementing it in a logon script and need to leave it active for a couple weeks. If users were to login multiple times I do not want it to add the line multiple times. #include <array.au3> #include <file.au3> #RequireAdmin Dim $s_FileInputPath = @SystemDir & "\drivers\etc\hosts" Dim $s_FileOutputPath = @SystemDir & "\drivers\etc\hosts" Dim $i_InserLineNumber = 23 Dim $s_InserLineContent = "1.1.1.1 www.msn.com" ;A return value 1 indicates success. A return value of 0 or less indicates failure. WriteLineToFile($s_FileInputPath ,$s_FileOutputPath ,$i_InserLineNumber,$s_InserLineContent) Func WriteLineToFile($s_FileName,$s_FileOutputName,$i_LineNumber,$s_Line) Local $as_HostsContent If Not _FileReadToArray($s_FileName,$as_HostsContent) Then Return 0 ;~ _ArrayDisplay($as_HostsContent) ;This if statement extends the number of lines in the file if the line that is being inserted to is currently larger than the total number of lines in the file If UBound($as_HostsContent) < $i_LineNumber Then ReDim $as_HostsContent[$i_LineNumber+1] EndIf If Not _ArrayInsert($as_HostsContent,$i_LineNumber,$s_Line) Then Return -1 ;~ _ArrayDisplay($as_HostsContent) If Not _FileWriteFromArray($s_FileOutputName,$as_HostsContent,1) Then Return -2 Return 1 EndFunc Link to comment Share on other sites More sharing options...
Zedna Posted February 11, 2008 Share Posted February 11, 2008 Look at _ArraySearch() Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
Danny35d Posted February 13, 2008 Share Posted February 13, 2008 (edited) Try this code is using FileRead(), FileGetSize(), StringInStr() and FileWriteLine(). #RequireAdmin Dim $s_FileInputPath = @SystemDir & "\drivers\etc\hosts" Dim $s_InserLineContent = "1.1.1.1 www.msn.com" $HostFile = FileRead($s_FileInputPath, FileGetSize($s_FileInputPath)) If StringInStr($HostFile, $s_InserLineContent) = 0 Then FileWriteLine($s_FileInputPath, $s_InserLineContent) 1) Read the entire Host file into memory 2) Use StringInStr() to check is the line is already added 3) If StringInStr() return 0 "meaning string not found" then FileWriteLine() add the line Edited February 13, 2008 by Danny35d AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line Link to comment Share on other sites More sharing options...
schilbiz Posted February 13, 2008 Author Share Posted February 13, 2008 Try this code is using FileRead(), FileGetSize(), StringInStr() and FileWriteLine(). #RequireAdmin Dim $s_FileInputPath = @SystemDir & "\drivers\etc\hosts" Dim $s_InserLineContent = "1.1.1.1 www.msn.com" $HostFile = FileRead($s_FileInputPath, FileGetSize($s_FileInputPath)) If StringInStr($HostFile, $s_InserLineContent) = 0 Then FileWriteLine($s_FileInputPath, $s_InserLineContent) 1) Read the entire Host file into memory 2) Use StringInStr() to check is the line is already added 3) If StringInStr() return 0 "meaning string not found" then FileWriteLine() add the line @ Danny35d, thanks @ Zedna, I fumbled around with _ArraySearch() and learned how to use it better, but was having a tough time getting it work properly, it was adding spaces and doing odd things. Dannys is a nice clean script I will use that but thanks for the suggestion. 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