Sign in to follow this  
Followers 0
schilbiz

Adjusting a script so it does not add the same line multiple times.

4 posts in this topic

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

Share this post


Link to post
Share on other sites



#3 ·  Posted (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 by Danny35d

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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
Sign in to follow this  
Followers 0