Last active
October 30, 2023 17:24
-
-
Save boredwz/91969690ccb998f7167bf7af74375cc1 to your computer and use it in GitHub Desktop.
(CMD) Hosts Edit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| :: HostsEdit (CMD) | |
| :: | |
| :: [Author] | |
| :: wvzxn | https://github.com/wvzxn/ | |
| :: | |
| :: [Description] | |
| :: Edit strings in the Windows hosts file. | |
| :: /add - Add | |
| :: /del - Remove | |
| :: /q - Disable output (optionally) | |
| :: /backup - Create advanced backup file <hosts.Backup_%DateAndTime%> (optionally) | |
| :: | |
| :: [Examples] | |
| :: HostsEdit.cmd /add "1.2.3.4 www.example.com" | |
| :: HostsEdit.cmd /q "1.2.3.4 www.example.com" "/del" "4.3.2.1 api.example.com" /backup | |
| @echo off | |
| fsutil dirty query %SYSTEMDRIVE% >nul&if ERRORLEVEL 1 (echo Run as Administrator required&pause&exit) | |
| setlocal EnableDelayedExpansion | |
| :: Default variable value | |
| set "hosts=%WINDIR%\System32\drivers\etc\hosts" | |
| set "job=info" | |
| set "backup=Backup" | |
| set silent=0 | |
| :: Find /add, /del and options | |
| for %%Q in (%*) do ( | |
| set "q=%%~Q" | |
| if /i "!q!"=="/add" (set "job=add") | |
| if /i "!q!"=="/del" (set "job=del") | |
| if /i "!q!"=="/backup" (call :advancedBackup) | |
| if /i "!q!"=="/q" (set silent=1) | |
| ) | |
| :: If /add or /del not specified, do write info and exit | |
| if "%job%"=="info" ( | |
| for /f "usebackq delims=" %%Q in (`findstr /b /c:":: " "%~f0"`) do echo %%Q | |
| pause | |
| exit /b | |
| ) | |
| :: Set attributes + Create backup | |
| attrib -h -r "%hosts%" >nul 2>&1 | |
| if "%backup%"=="Backup" (del /f /q "%hosts%.Backup" >nul 2>&1) | |
| copy "%hosts%" "%hosts%.%backup%" >nul 2>&1 | |
| :: Iterate values and do /add or /del | |
| for %%Q in (%*) do set "q=%%~Q"& if not "!q:~0,1!"=="/" (call :%job% "!q!") | |
| :: If no change, delete the backup file | |
| fc "%hosts%" "%hosts%.%backup%" >nul 2>&1 && del /f /q "%hosts%.%backup%" | |
| exit /b | |
| :add | |
| :: If the line NOT FOUND, do APPEND to the hosts + echo if not /q | |
| findstr /i /c:%1 "%hosts%" >nul 2>&1 || ( | |
| echo %~1>>"%hosts%" | |
| if %silent%==0 (echo %1 added to the hosts file.) | |
| ) | |
| exit /b | |
| :del | |
| :: If the line FOUND, do REMOVE from the hosts + echo if not /q | |
| findstr /i /c:%1 "%hosts%" >nul 2>&1 && ( | |
| copy "%hosts%" "%hosts%_delete_this" >nul 2>&1 | |
| findstr /i /v /c:%1 "%hosts%_delete_this" > "%hosts%" | |
| del /f /q "%hosts%_delete_this" >nul 2>&1 | |
| if %silent%==0 (echo %1 removed from the hosts file.) | |
| ) | |
| exit /b | |
| :advancedBackup | |
| :: Add current date and time to the backup file name | |
| for /f "tokens=1-6 delims=/: " %%A in ('robocopy "|" . /njh ^| find ":"') do set "backup=%backup%_%%A%%B%%C%%D%%E%%F" | |
| exit /b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment