Skip to content

Instantly share code, notes, and snippets.

View tnhung2011's full-sized avatar

tnhung2011

  • Vietnam
View GitHub Profile
@if defined eratosSort goto sort
@echo off
setlocal enabledelayedexpansion enableextensions
%= Input is either first argument or stdin =%
if "%~1"=="" (
set /p n=|| goto :eof
set /a n=n
) else (set /a n=%~1)
(
if %n% lss 2 goto help
@tnhung2011
tnhung2011 / webpage.bat
Last active December 24, 2025 07:53
Displays http://example.com using cURL and MSHTA (no temporary file)
@echo off
setlocal enableextensions
:: curl should be available by default in Windows 10 Insider build 17063 and later
:: Source: https://devblogs.microsoft.com/commandline/tar-and-curl-come-to-windows/
for /f "tokens=*" %%a in ('curl -s -L http://example.com') do (
echo(%%a
set "str=%%a"
)
mshta "about:%str:"=""%"
@tnhung2011
tnhung2011 / spinner.bat
Created December 19, 2025 11:43
A spinner, demonstrating carriage return in Batch (CR thanks to Jeb - http://ss64.org/viewtopic.php?id=1344)
@if (@X)==(@Y) @end /*
@echo off
setlocal enabledelayedexpansion
::http://ss64.org/viewtopic.php?id=1344
for /F "usebackq" %%a in (`copy /Z "%~f0" nul`) do set "cr=%%a"
set /a idx=0
for %%i in (/ - \ ^|) do (
set spin.!idx!=%%i
set /a idx+=1
)
@tnhung2011
tnhung2011 / math.bat
Last active December 19, 2025 15:34
A CLI for arithmetic operations, with floating-point support via JScript.
@if (@X == @Y) @end /*
@echo off
setlocal enableextensions enabledelayedexpansion
for /f %%A in ('prompt $H ^&echo on ^&for %%B in ^(1^) do rem') do set bs=%%A
set ansi1=
set ansi2=
2>nul >nul (reg query HKCU\Console /v VirtualTerminalLevel /t REG_DWORD | find "0x1") && (
set ansi1=
set ansi2=
)
@tnhung2011
tnhung2011 / elevate.bat
Created December 18, 2025 10:58
I don't know if anyone has posted this. This script elevates your program without using temporary VBScript file or PowerShell.
@if (@X == @Y) @end /*
@echo off
setlocal enableextensions enabledelayedexpansion
set rargs=
for /f usebackq^ tokens^=1*^ eol^= %%a in ('%*') do set rargs=%%b
cscript //nologo //e:jscript "%~f0" %1
exit /b
*/
WScript.CreateObject("Shell.Application").ShellExecute(WScript.Arguments(0), WScript.CreateObject("WScript.Shell").Environment("Process").Item("rargs"), "", "runas");
@tnhung2011
tnhung2011 / timezone.bat
Created December 17, 2025 13:05
Get your current timezone. More useful output than "tzutil /g".
@echo off
setlocal enableextensions enabledelayedexpansion
for /f "delims=" %%i in ('tzutil /g') do (
for /f "delims=:" %%j in ('tzutil /l ^| findstr /N /C:"%%i"') do (
set /a "toSkip=%%j-1"
goto print
)
)
:print
for /f "delims=() tokens=2" %%i in ('^(tzutil /l ^| findstr /N $^) ^| findstr /R /C:"^%toSkip%"') do echo %%i
@tnhung2011
tnhung2011 / asinvoker.bat
Last active December 17, 2025 12:17
Run a program as invoker.
@echo off
setlocal enableextensions enabledelayedexpansion
if "%~1"=="/?" goto help
set args=%*
set prefix=
if "%~1"=="/nowait" (
::%* strips spaces
set args=!args:~7!
set prefix=start ""
shift
@tnhung2011
tnhung2011 / hoststree.bat
Last active December 22, 2025 11:16
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
@tnhung2011
tnhung2011 / hosts.bat
Created December 15, 2025 14:27
Aggregates local hosts files to the global one, rejecting any duplicates
@echo off
:: detect wrapping
:: writePublic defined = jump to :writePublic
:: this evades CTRL-C canceling all operations
:: https://stackoverflow.com/a/27131024
if "%~1"=="" if defined writePublic (goto writePublic) else (goto noargs)
set writePublic=
setlocal enableextensions enabledelayedexpansion
if "%~1"=="/?" goto help
@tnhung2011
tnhung2011 / regread.bat
Last active December 14, 2025 01:57
Read a value from the registry using a single path (HKXX or HKEY_ABC\SubKey\Value). Useful for lazy copy-pasting.
@echo off
setlocal enableextensions enabledelayedexpansion
for /f "usebackq eol= delims=" %%a in ('%*') do set sArgs=%%~a
::Split at last backslash ("\")
for /f "delims==" %%a in ('set _a 2^>nul') do set "%%a="
(set lf=^
)
for %%l in ("!lf!") do set "_a=!sArgs:\=%%~l!"