Sign in to follow this  
Followers 0
Mark01

Replacing Host entries in a host file

2 posts in this topic

My issues is as follows. When the user pulls up the GUI and clicks on Internet button it should search the hosts file for the entry of (172.24.240.10 www.google.com) and place a # and the beginning of the line to comment it out but if the users accidentally runs the program again it and chooses internet it will just place 1 # instead of 2. Right now everytime I click on the Internet button it continues to place a # at the beginning. So if I open the program 3 times and click on Internet 3 times the line shows (###172.24.240.10 www.google.com) instead of just 1. Please help me!!

#Begin

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****

#AutoIt3Wrapper_outfile=..\..\..\..\WEBICE\Webice.exe

#AutoIt3Wrapper_UseX64=n

#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <ButtonConstants.au3>

#include <GUIConstantsEx.au3>

#include <StaticConstants.au3>

#include <WindowsConstants.au3>

#Region

#Region Variable Declarations, etc

Dim $line, $HostFileName, $FileContent

Global $EntryTag = "NotFound"

$HostFileName = @SystemDir & "\Drivers\etc\hosts"

$FileContent = ""

#EndRegion Variable Declarations, etc

_Main()

Func _Main()

$Form1 = GUICreate("The ICE 1.0", 316, 238, 381, 189)

GUISetIcon("D:\003.ico")

$GroupBox1 = GUICtrlCreateGroup("", 8, 1, 297, 193)

$InternetButt = GUICtrlCreateButton("&Internet", 17, 50, 131, 89, $WS_GROUP)

GUICtrlSetFont(-1, 12, 800, 0, "Perpetua")

$DedicatedButt = GUICtrlCreateButton("&Dedicated", 161, 50, 131, 89, $WS_GROUP)

GUICtrlSetFont(-1, 12, 800, 0, "Perpetua")

GUICtrlCreateLabel("Web ICE Host Switcher", 35, 16, 244, 28)

GUICtrlSetFont(-1, 14, 800, 2, "Univers 55")

GUICtrlSetColor(-1, 0x008000)

GUICtrlCreateGroup("", -99, -99, 1, 1)

$CancelButt = GUICtrlCreateButton("&Cancel", 110, 203, 75, 25, $WS_GROUP)

GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###

While 1

$nMsg = GUIGetMsg()

Select

Case $nMsg = $CancelButt

Exit

Case $nMsg = $InternetButt

InternetConfig()

Case $nMsg = $DedicatedButt

ModDedicated()

Case $nMsg = $GUI_EVENT_CLOSE

Exit

EndSelect

WEnd

EndFunc ;==>_Main

Func ModDedicated()

$TheICEMod = "#172.24.240.10 theice.com"

$ReplaceText = "172.24.240.10 theice.com"

$FileContents = FileRead($HostFileName)

$FileContents = StringReplace($FileContents, $TheICEMod, $ReplaceText)

FileDelete($HostFileName)

FileWrite($HostFileName, $FileContents)

$JARMod = "#172.24.240.10 test.google.com"

$ReplaceText = "172.24.240.10 test.google.com"

$FileContents = FileRead($HostFileName)

$FileContents = StringReplace($FileContents, $JARMod, $ReplaceText)

FileDelete($HostFileName)

FileWrite($HostFileName, $FileContents)

$InternetButt = GUICtrlCreateButton("&Internet", 17, 50, 131, 89, $WS_GROUP)

GUICtrlSetFont(-1, 12, 800, 0, "Perpetua")

GUICtrlSetBkColor(-1, 0x00FF00)

$DedicatedButt = GUICtrlCreateButton("&Dedicated", 161, 50, 131, 89, $WS_GROUP)

GUICtrlSetFont(-1, 12, 800, 0, "Perpetua")

GUICtrlSetBkColor(-1, 0xFFFFFF)

Exit

EndFunc ;==>ModDed

Func InternetConfig()

$TheICE = "172.24.240.10 www.google.com"

$ReplaceText = "#172.24.240.10 www.google.com"

$FileContents = FileRead($HostFileName)

$FileContents = StringReplace($FileContents, $TheICE, $ReplaceText)

FileDelete($HostFileName)

FileWrite($HostFileName, $FileContents)

$JAR = "172.24.240.10 test.google.com"

$ReplaceText = "#172.24.240.10 test.google.com"

$FileContents = FileRead($HostFileName)

$FileContents = StringReplace($FileContents, $JAR, $ReplaceText)

FileDelete($HostFileName)

FileWrite($HostFileName, $FileContents)

$InternetButt = GUICtrlCreateButton("&Internet", 17, 50, 131, 89, $WS_GROUP)

GUICtrlSetFont(-1, 12, 800, 0, "Perpetua")

GUICtrlSetBkColor(-1, 0x00FF00)

$DedicatedButt = GUICtrlCreateButton("&Dedicated", 161, 50, 131, 89, $WS_GROUP)

GUICtrlSetFont(-1, 12, 800, 0, "Perpetua")

GUICtrlSetBkColor(-1, 0xFFFFFF)

Exit

EndFunc ;==>InternetConfig

#End

Share this post


Link to post
Share on other sites



The reason it keeps adding the # symbol is because you're checking for the string "172.24.240.10 test.google.com" and replacing that with "#172.24.240.10 test.google.com". So, what happens is, the next time it looks for the string, it replaces just the string text with your $ReplaceText, and because you never look for the $ReplaceText first, it keeps adding #s to the front of it. What you should be doing is looking for the $ReplaceText string in the file first, if it sees it there already, skip it and move on, if it doesn't see $ReplaceText, it should look for the $JAR text next.

Something like this (pseudo code):

$FileRead = FileReadline($hostfilename)
If StringinStr($Fileread, "#172.24.240.10 test.google.com")> 0 then
     do nothing
elseif stringinstr($fileread, "172.24.240.10 test.google.com") >0  then
    $FileContents = StringReplace($FileContents, $JAR, $ReplaceText)
Endif

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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