SRL Archive - Other Scripts

Hosts file updater


Tootoot222

2009-02-27 04:37:53

http://www.mvps.org/winhelp2002/hosts.htm

read first ^

this will update your hosts file, which in turn blocks almost all adverts (you must restart your pc first i think)

also, for hosts files that are above ~60KB in size, you must disable the DSN client first:


To resolve this issue (manually) open the "Services Editor"

* Start | Run (type) "services.msc" (no quotes)
* Scroll down to "DNS Client", Right-click and select: Properties
* Click the drop-down arrow for "Startup type"
* Select: Manual, or Disabled (recommended) click Apply/Ok and restart.


(more indepth instructions in the link)

program HostsUpdater;

const
BackupOldHosts = True;

var
NewInfo, OldInfo, HostPath: String;
I: Integer;

MainWindow: TForm;
UpdateButton, BrowseButton: TButton;
RBttns: array [0..3] of TRadioButton;
CustEdit: TEdit;

procedure AccessFile(Read: Boolean; var Info: String; Path: String);
var
F: Integer;
begin
try
F:= OpenFile(Path, True);
if(F >= 0) then
if(Read) then
ReadFileString(F, Info, FileSize(F))
else
WriteFileString(F, Info)
else
RaiseException(erCustomError, 'Unable to open file');
except
Writeln(ExceptionToString(ExceptionType, ExceptionParam));
finally
try
if(F >= 0) then
CloseFile(F)
else
RaiseException(erCustomError, 'Unable to close file');
except
Writeln(ExceptionToString(ExceptionType, ExceptionParam));
TerminateScript;
end;
end;
end;

procedure UpdateHosts;
begin
if(FileExists(HostPath + 'HOSTS')) then
begin
NewInfo:= GetPage('http://www.mvps.org/winhelp2002/hosts.txt');
if(NewInfo = '') then
Writeln('Unable to reach webpage!')
else
begin
if(BackupOldHosts) then
begin
AccessFile(True, OldInfo, HostPath + 'HOSTS');
I:= 1;
while(FileExists(HostPath + 'HOSTS' + IntToStr(I))) do
I:= I + 1;
CloseFile(RewriteFile(HostPath + 'HOSTS' + IntToStr(I), False));
AccessFile(False, OldInfo, HostPath + 'HOSTS' + IntToStr(I));
end;
AccessFile(False, NewInfo, HostPath + 'HOSTS');
Writeln('Updated successfully!');
end;
end;
end;

procedure LoadMainWindow;
begin
MainWindow:= CreateForm;
with MainWindow do
begin
Position:= poDesktopCenter;
Width:= 250;
Height:= 220;
Caption:= 'Hosts file updater - by Tootoot222';
BorderStyle:= bsToolWindow;
end;
end;

procedure UpdateButtonClick(Sender: TObject);
var
I: Integer;
begin
for I:= Low(RBttns) to High(RBttns) do
if(RBttns[I].Checked) then
case I of
0: HostPath:= 'C:\WINDOWS\SYSTEM32\DRIVERS\ETC\';
1: HostPath:= 'C:\WINNT\SYSTEM32\DRIVERS\ETC';
2: HostPath:= 'C:\WINDOWS';
3: HostPath:= CustEdit.Text;
end;
UpdateHosts;
end;

procedure LoadUpdateButton;
begin
UpdateButton:= TButton.Create(MainWIndow);
with UpdateButton do
begin
Parent:= MainWindow;
Height:= 30;
Width:= 100;
Left:= (Parent.ClientWidth - Width) / 2;
Top:= Parent.ClientHeight - 4 - Height;
Font.Size:= 13;
Caption:= 'Update!';
OnClick:= @UpdateButtonClick;
end;
end;

procedure BrowseButtonOnClick(Sender: TObject);
var
OD: TOpenDialog;
begin
OD:= TOpenDialog.Create(MainWindow);
OD.InitialDir:= ExtractFileDrive(AppPath);
OD.Filter:= 'Hosts file (hosts) | hosts |All files (*.*) | *.*';
if(OD.Execute) then
CustEdit.Text:= ExtractFilePath(OD.FileName);
OD.Free;
end;

procedure LoadBrowseButton;
begin
BrowseButton:= TButton.Create(MainWindow);
with BrowseButton do
begin
Parent:= MainWindow;
Width:= 50;
Caption:= 'Browse';
Visible:= False;
OnClick:= @BrowseButtonOnClick;
end;
end;

procedure RBttnsOnClick(Sender: TObject);
var
I: Integer;
begin
I:= High(RBttns);
if(Sender = RBttns[I]) then
begin
CustEdit.Top:= RBttns[I].Top + RBttns[I].Height + 12;
CustEdit.Visible:= True;
BrowseButton.Height:= RBttns[I].Height;
BrowseButton.Left:= RBttns[I].Left + RBttns[I].Width + 12;
BrowseButton.Top:= RBttns[I].Top;
BrowseButton.Visible:= True;
end else
begin
CustEdit.Visible:= False;
BrowseButton.Visible:= False;
end;
end;

procedure LoadRBttns;
var
I: Integer;
C: TCanvas;
Cap: TStringArray;
begin
Cap:= ['Windows Vista / Windows XP', 'Windows 2K', 'Win 98/ME', 'Custom path'];
C:= GetBitmapCanvas(BitmapFromString(1, 1, ''));
for I:= Low(RBttns) to High(RBttns) do
with RBttns[I] do
begin
RBttns[I]:= TRadioButton.Create(MainWindow);
Parent:= MainWindow;
if(I = Low(RBttns)) then
Top:= 10
else
Top:= RBttns[I - 1].Top + RBttns[I - 1].Height + 12;
C.Font.Size:= Font.Size;
C.Font.Height:= Font.Height;
C.Font.Style:= Font.Style;
Caption:= Cap[I];
Width:= C.TextWidth(Caption) + 16;
if(I = Low(RBttns)) then
Left:= (Parent.Width - Width) / 2
else
Left:= RBttns[I - 1].Left;
OnClick:= @RBttnsOnClick;
end;
RBttns[0].Checked:= True;
C.Font.Size:= CustEdit.Font.Size;
C.Font.Height:= CustEdit.Font.Height;
C.Font.Style:= CustEdit.Font.Style;
CustEdit.Width:= C.TextWidth(CustEdit.Text) + 10;
CustEdit.Left:= (CustEdit.Parent.ClientWidth - CustEdit.Width) / 2;
end;

procedure LoadCustEdit;
begin
CustEdit:= TEdit.Create(MainWindow);
with CustEdit do
begin
Parent:= MainWindow;
Visible:= False;
Text:= 'C:\WINDOWS\SYSTEM32\DRIVERS\ETC\';
Height:= 30;
end;
end;

procedure InitForm;
begin
LoadMainWindow;
LoadUpdateButton;
LoadCustEdit;
LoadBrowseButton;
LoadRBttns;
end;

procedure SafeInitForm;
var
V: TVariantArray;
begin
ThreadSafeCall('InitForm', V);
end;

procedure ShowForm;
begin
MainWindow.ShowModal;
end;

procedure SafeShowForm;
var
V: TVariantArray;
begin
ThreadSafeCall('ShowForm', V);
end;

begin
SafeInitForm;
SafeShowForm;
end.

ShowerThoughts

2009-02-27 04:40:12

Interesting, GJ!

Harry

2009-02-27 09:12:56

I hate those hugs host file lists. Any list over 50kbs will make your internet on your PC go a lot slower. Just get adblock.
Nice work though!

senrath

2009-02-27 09:15:28

From: Hy71194 aka Harry
I hate those hugs host file lists. Any list over 50kbs will make your internet on your PC go a lot slower. Just get adblock.
Nice work though!


Debatable. There is no noticeable difference between my internet connection with my 286kb hosts file than with a blank hosts file.

Tootoot222

2009-02-27 10:37:19

you MUST disable the DSN client (it's all in the link i posted on the first post)

From: DaLink
To resolve this issue (manually) open the "Services Editor"

* Start | Run (type) "services.msc" (no quotes)
* Scroll down to "DNS Client", Right-click and select: Properties
* Click the drop-down arrow for "Startup type"
* Select: Manual, or Disabled (recommended) click Apply/Ok and restart.

Harry

2009-02-27 13:50:22

From: senrath
Debatable. There is no noticeable difference between my internet connection with my 286kb hosts file than with a blank hosts file.

Anything is debatable, but even Microsoft has conformed a larger host file will make a slower DNS load time, as it must scan the entire hosts file each DNS lookup attempt.

Smartzkid

2009-03-01 11:55:10

Neat, but I wouldn't do it. Use adblock, then you can still click SRL's ads every now and then.