Skip to content

Instantly share code, notes, and snippets.

@tnhung2011
Last active December 22, 2025 11:16
Show Gist options
  • Select an option

  • Save tnhung2011/d301a30727b1e700ff4765b5c5cff008 to your computer and use it in GitHub Desktop.

Select an option

Save tnhung2011/d301a30727b1e700ff4765b5c5cff008 to your computer and use it in GitHub Desktop.
Creates a HOSTS tree view with hostnames branching out of their resolving IPs.
@echo off
setlocal enableextensions enabledelayedexpansion
if defined hoststreeMain goto main
if "%~1"=="/?" (
for /f %%A in ('prompt $H ^&echo on ^&for %%B in ^(1^) do rem') do set bs=%%A
echo Creates a HOSTS tree view with hostnames branching out of their resolving IPs.
echo:
echo HOSTSTREE file
echo:
echo !bs! file Defaults to %%SystemRoot%%\System32\drivers\etc\hosts
echo:
echo NOTES:
echo * Due to file system limitations, ":" will be replaced with ";" for IPv6 addresses.
echo * If the console window is closed while the script is running, you should
echo !bs! delete this folder: "%%temp%%\hoststree".
echo * If "notree" is defined, HOSTSTREE will recursively list folders instead of a tree.
goto :eof
)
(
for /f "delims==" %%a in ('set _ 2^>nul') do set "%%a="
set hoststreeMain=1
for /f "tokens=*" %%a in ("%~1") do set "hosts=%%~fa"
if not defined hosts set "hosts=%SystemRoot%\System32\drivers\etc\hosts"
cmd /c "%~f0"
::ctrlc will take you here
cmd /c rd /s /q "%%temp%%\hoststree"
exit /b
)
:main
md "%temp%\hoststree"
pushd "%temp%\hoststree"
for /f "tokens=1,2 eol=#" %%a in ('sort "%hosts%"') do (
set ip=%%a
set ip=!ip::=;!
if not defined __!ip! set /a __!ip!=i=0
for /f %%v in ("_!ip!_!i!") do (
set "%%v=!%%v! %%b"
if not "!%%v:~8000,1!"=="" set /a i+=1
)
)
for /f "delims=_= eol=#" %%a in ('set __ 2^>nul') do (
set __%%a=
md "%%a"
pushd "%%a"
2>nul (for /f "delims==" %%v in ('set _') do md !%%v!)
popd
)
if defined notree (
for /r %%a in (.) do (
set "str=%%~fa"
echo(!str:*%__CD__%=!
)
) else (tree)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment