JScript Posted August 14, 2009 Share Posted August 14, 2009 (edited) Hello all,This functions is used for manipulate Host file located in %Systemroot%\System32\Drivers\Etc\Host.Current functions:; =============================================================================================================================== _HostRead( [FilePath] ) _HostReadLine( LineNumber [, FilePath ] ) _HostWrite( IP, HostName [, OverWrite [, FilePath ]]) _HostDelete( IP, HostName [, FilePath ]) ; ===============================================================================================================================Send comments...expandcollapse popup#include-once #include <Array.au3> ; #INDEX# ======================================================================================================================= ; Title .........: _HostFile.au3 ; AutoIt Version.: 3.2.12++ ; Language.......: English ; Description ...: Functions that manipulate Host file located in %Systemroot%\System32\Drivers\Etc\Host. ; Author ........: João Carlos (jscript) ; =============================================================================================================================== ; #CURRENT# ===================================================================================================================== ;_HostRead ;_HostReadLine ;_HostWrite ;_HostDelete ; =============================================================================================================================== ; #INTERNAL_USE_ONLY#============================================================================================================ ;============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _HostRead ; Description ...: Read all contents in the Host file. ; Syntax.........: _HostRead( [FilePath] ) ; Parameters ....: FilePath - [optional] The default location and name is %Systemroot%\System32\Drivers\Etc\Host. ; Return values .: Success - Sets @error = 0 and returns an two-dimensional array: ; $array[0][0] - Number of lines returned ; $array[1][0] - Line number ; $array[1][1] - IP number ; $array[1][2] - Host name ; ... ; $array[n][0] = nth Line number ; $array[n][1] = nth IP number ; $array[n][2] = nth Host name ; Failure - Returns the same two-dimensional array above with all values = 0 and: ; Sets @error = 1 if can not be open file ; Author ........: jscript (João Carlos) ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........; ; Example .......; $aHost = _HostRead() ; =============================================================================================================================== Func _HostRead($FilePath = "") Local $FileOpen, $aFileLines, $nCount, $Line = "", $RedirectIP[1][3] = [[0, 0, 0]] If $FilePath = "" Then $FilePath = @SystemDir & "\Drivers\Etc\hosts" ;Read the file into an array $FileOpen = FileOpen($FilePath, 0) If $FileOpen = -1 Then Return SetError(1, 0, $RedirectIP) Local $FileRead = FileRead($FileOpen, FileGetSize($FilePath)) & @LF $aFileLines = StringSplit(StringStripCR($FileRead), @LF) For $nCount = 1 To $aFileLines[0] $Line = StringStripWS($aFileLines[$nCount], 7) ; Skip comment lines proceeded by: # If StringLen($Line) < 10 Or StringRegExp(StringStripWS($Line, 8), "\A#") Then ContinueLoop ; $Line = StringSplit($Line, " " & Chr(9)) ;If @error Then $Line = StringSplit($Line, Chr(9)) If Not @error Then ReDim $RedirectIP[UBound($RedirectIP, 1) + 1][3] $RedirectIP[0][0] += 1 $RedirectIP[$RedirectIP[0][0]][0] = $nCount $RedirectIP[$RedirectIP[0][0]][1] = $Line[1] $RedirectIP[$RedirectIP[0][0]][2] = $Line[2] EndIf Next FileClose($FileOpen) Return $RedirectIP EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: _HostReadLine ; Description ...: Read the specified line number in Host file. ; Syntax.........: _HostReadLine( LineNumber [, FilePath ] ) ; Parameters ....: LineNumber - Specified line number to be read. ; FilePath - [optional] The default location and name is %Systemroot%\System32\Drivers\Etc\Host. ; Return values .: Success - Sets @error = 0 and returns an one-dimensional with 2 elements: ; $array[0] - IP number ; $array[1] - Host name ; Failure - Sets @error = 1 and returns the same one-dimensional array above with 0 value. ; Author ........: jscript (João Carlos) ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........; ; Example .......; $aHost = _HostReadLine($array[1][0]) ; =============================================================================================================================== Func _HostReadLine($LineNumber, $FilePath = "") Local $FileOpen, $aFileLines, $nCount, $Line = "", $RedirectIP[2] = [0, 0] If $FilePath = "" Then $FilePath = @SystemDir & "\Drivers\Etc\hosts" ;Read the file into an array $FileOpen = FileOpen($FilePath, 0) If $FileOpen = -1 Then Return SetError(1, 0, $RedirectIP) Local $FileRead = FileRead($FileOpen, FileGetSize($FilePath)) & @LF $aFileLines = StringSplit(StringStripCR($FileRead), @LF) For $nCount = 1 To $aFileLines[0] $Line = StringStripWS($aFileLines[$nCount], 7) ; Skip comment lines proceeded by: # If StringLen($Line) < 10 Or StringRegExp(StringStripWS($Line, 8), "\A#") Then ContinueLoop $Line = StringSplit($Line, " ") If Not @error And $nCount = $LineNumber Then $RedirectIP[0] = $Line[1] $RedirectIP[1] = $Line[2] ExitLoop EndIf Next FileClose($FileOpen) Return $RedirectIP EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: _HostWrite ; Description ...: Write redirections IP and HostName in the Host file. ; Syntax.........: _HostWrite( IP, HostName [, OverWrite [, FilePath ]]) ; Parameters ....: IP - IP addres. ; HostName - Host name. ; OvewrWrite - [optional] If set to 1, over write an existe value, the defaul is 0 ; FilePath - [optional] The default location and name is %Systemroot%\System32\Drivers\Etc\Host. ; Return values .: Success - Returns 1 ; Failure - Returns 0 and sets @error to: ; 1 - if file is readonly ; 2 - if file can not be open ; 3 - if not output file in write mode ; 4 - if file cannot be written ; Author ........: jscript (João Carlos) ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........; ; Example .......; _HostWrite($IP, $HostName, 1) ; =============================================================================================================================== Func _HostWrite($IP, $HostName, $OverWrite = 0, $FilePath = "") Local $FileOpen, $aFileLines, $hWriteHandle, $nCount, $Line = "", $Flag = True, $Value0, $Value1 If $FilePath = "" Then $FilePath = @SystemDir & "\Drivers\Etc\hosts" ; Check if file is readonly... If StringInstr(FileGetAttrib($FilePath), "R") Then Return SetError(1, 0, 0) ;Read the file into an array $FileOpen = FileOpen($FilePath, 0) If $FileOpen = -1 Then Return SetError(2, 0, 0) Local $FileRead = FileRead($FileOpen, FileGetSize($FilePath)) & @LF $aFileLines = StringSplit(StringStripCR($FileRead), @LF) ;Open the output file in write mode $hWriteHandle = FileOpen($FilePath, 2) If $hWriteHandle = -1 Then Return SetError(3, 0, 0) ;Loop through the array For $nCount = 1 To $aFileLines[0] $Line = StringStripWS($aFileLines[$nCount], 7) ; Skip comment lines proceeded by: # If StringLen($Line) < 10 Or StringRegExp(StringStripWS($Line, 8), "\A#") Then ContinueLoop $Value0 = StringRegExp($Line, "\b" & $IP & "\b") $Value1 = StringRegExp($Line, "\b" & $HostName & "\b") If ($Value0 And $Value1) Or ($OverWrite And ($Value0 Or $Value1)) Then $aFileLines[$nCount] = $IP & " " & $HostName $Flag = False ExitLoop EndIf Next If $Flag Then _ArrayInsert($aFileLines, $aFileLines[0], $IP & " " & $HostName) ;Write the lines back to original file. For $nCount = 1 To UBound($aFileLines) - 1 $Line = StringStripWS($aFileLines[$nCount], 7) If $Line = "" Then ContinueLoop ; If FileWriteLine($hWriteHandle, $aFileLines[$nCount]) = 0 Then FileClose($hWriteHandle) Return SetError(4, 0, 0) EndIf Next FileClose($hWriteHandle) Return SetError(0, 0, 1) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: _HostDelete ; Description ...: Delete entries in the Host file. ; Syntax.........: _HostDelete( IP, HostName [, FilePath ]) ; Parameters ....: IP - IP addres. ; HostName - Host name. ; FilePath - [optional] The default location and name is %Systemroot%\System32\Drivers\Etc\Host. ; Return values .: Success - Returns 1 ; Failure - Returns 0 and sets @error to: ; 1 - if file is readonly ; 2 - if file can not be open ; 3 - if not output file in write mode ; 4 - if file cannot be written ; Author ........: jscript (João Carlos) ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........; ; Example .......; _HostDelete($IP, $HostName, 1) ; =============================================================================================================================== Func _HostDelete($IP = "", $HostName = "", $FilePath = "") Local $FileOpen, $aFileLines, $hWriteHandle, $nCount, $Line = "", $Flag = True If $FilePath = "" Then $FilePath = @SystemDir & "\Drivers\Etc\hosts" ; Check if file is readonly... If StringInstr(FileGetAttrib($FilePath), "R") Then Return SetError(1, 0, 0) ;Read the file into an array $FileOpen = FileOpen($FilePath, 0) If $FileOpen = -1 Then Return SetError(2, 0, 0) Local $FileRead = FileRead($FileOpen, FileGetSize($FilePath)) & @LF If ($IP <> "" And Not StringInStr($FileRead, $IP)) Or ($HostName <> "" And Not StringInStr($FileRead, $HostName)) Then _ Return SetError(3, 0, 0) $aFileLines = StringSplit(StringStripCR($FileRead), @LF) ;Open the output file in write mode $hWriteHandle = FileOpen($FilePath, 2) If $hWriteHandle = -1 Then Return SetError(4, 0, 0) ;Write the lines back to original file. For $nCount = 1 To UBound($aFileLines) - 1 $Line = StringStripWS($aFileLines[$nCount], 7) If $Line = "" Then ContinueLoop If $IP = "" And StringRegExp($Line, "\b" & $HostName & "\b") Then ContinueLoop If StringRegExp($Line, "\b" & $IP & "\b") And $HostName = "" Then ContinueLoop If StringRegExp($Line, "\b" & $IP & "\b") And StringRegExp($Line, "\b" & $HostName & "\b") Then ContinueLoop ; If FileWriteLine($hWriteHandle, $aFileLines[$nCount]) = 0 Then FileClose($hWriteHandle) Return SetError(5, 0, 0) EndIf Next FileClose($hWriteHandle) Return SetError(0, 0, 1) EndFunc@shai, Thank you for having found a mistake!Regards,João Carlos. Edited January 8, 2012 by jscript holley and MrVietA2 2 http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!) Somewhere Out ThereJames Ingram Download Dropbox - Simplify your life!Your virtual HD wherever you go, anywhere! Link to comment Share on other sites More sharing options...
viky909 Posted January 8, 2012 Share Posted January 8, 2012 Nice one , thanks for sharing Link to comment Share on other sites More sharing options...
shai Posted January 8, 2012 Share Posted January 8, 2012 1 mistakein _HostWrite funcreplace:ReturnReturnSetError(4, 0, 0)to:ReturnSetError(4, 0, 0) Link to comment Share on other sites More sharing options...
JScript Posted January 8, 2012 Author Share Posted January 8, 2012 Nice one , thanks for sharing Wow, thanks!I posted this UDF on 14 August 2009 - 01:35 AM and only now (almost 3 years) is that someone commented, ual!Regards,João Carlos. http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!) Somewhere Out ThereJames Ingram Download Dropbox - Simplify your life!Your virtual HD wherever you go, anywhere! Link to comment Share on other sites More sharing options...
JScript Posted January 8, 2012 Author Share Posted January 8, 2012 @Shai Thank you for having found a mistake! It has been fixed! Regards, João Carlos. http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!) Somewhere Out ThereJames Ingram Download Dropbox - Simplify your life!Your virtual HD wherever you go, anywhere! 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