-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateGithubSectionOnly.bat
More file actions
62 lines (51 loc) · 1.55 KB
/
UpdateGithubSectionOnly.bat
File metadata and controls
62 lines (51 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
@echo off
setlocal enabledelayedexpansion
set "url=https://raw.hellogithub.com/hosts"
set "tempfile=%temp%\remote-hosts"
set "hostsfile=C:\Windows\System32\drivers\etc\hosts"
set "string1=### Github Start"
set "string2=### Github End"
set "found1=0"
set "found2=0"
set "point1=0"
set "point2=0"
rem Download the remote-hosts file
powershell -Command "Invoke-WebRequest -Uri %url% -OutFile %tempfile%"
rem Read the hosts file and find the positions of the strings
for /f "tokens=1,* delims=:" %%a in ('findstr /n "^" %hostsfile%') do (
if "!found1!"=="0" if "%%b"=="%string1%" (
set "found1=1"
set "point1=%%a"
)
if "!found2!"=="0" if "%%b"=="%string2%" (
set "found2=1"
set "point2=%%a"
)
)
if "!found1!"=="0" (
echo String "%string1%" not found in %hostsfile%
copy /b %hostsfile% + %tempfile% C.txt
goto :movefile
)
if "!found2!"=="0" (
echo String "%string2%" not found in %hostsfile%
copy /b %hostsfile% + %tempfile% C.txt
goto :movefile
)
rem Split the hosts file into A1.txt and A2.txt
(for /f "tokens=1,* delims=:" %%a in ('findstr /n "^" %hostsfile%') do (
if %%a leq !point1! echo(%%b
)) > A1.txt
(for /f "tokens=1,* delims=:" %%a in ('findstr /n "^" %hostsfile%') do (
if %%a geq !point2! echo(%%b
)) > A2.txt
rem Concatenate A1.txt, remote-hosts, and A2.txt into C.txt
copy /b A1.txt + %tempfile% + A2.txt C.txt
:movefile
rem Move C.txt to overwrite the hosts file
move /y C.txt %hostsfile%
rem Refresh the DNS
ipconfig /flushdns
echo Hosts file has been updated successfully
endlocal
pause