Skip to content

Instantly share code, notes, and snippets.

@mataha
Created September 24, 2023 17:41
Show Gist options
  • Save mataha/312db2f0238ba1ff739e8b1a5c4b5af1 to your computer and use it in GitHub Desktop.
Save mataha/312db2f0238ba1ff739e8b1a5c4b5af1 to your computer and use it in GitHub Desktop.
:parse
@(verify "" || setlocal DisableDelayedExpansion EnableExtensions) 2>nul || (
(echo(Command Extensions could not be enabled; terminating the program.)
) >&2 && goto :EOF "Command Extensions could not be enabled"
@if not "-h" == "%~1" if not "-?" == "%~1" (goto :main)
@(
(echo(Language-independent ACP ^(active code page^) retrieval)
(echo()
) & call && goto :usage
:main
@for /f delims^= %%o in ('"%SystemRoot%\System32\chcp.com 2>nul"') do @(
for %%c in (%%o) do @set "__codepage=%%~nc" >nul 2>nul || goto :error
)
@for /f delims^= %%v in ("%~1") do @(
endlocal & call && (set %%v=%__codepage%) && goto :EOF || goto :error
)
:error
@(
(echo(%[red]%[%~n0 error]%[/red]%: invalid variable name)
(echo()
) >&2 & call
:usage
@(
(echo(%[b]%%[u]%Usage:%[/b]%%[/u]% %[b]%%~n0%[/b]% [OPTIONS] [variable])
(echo()
(echo(%[b]%%[u]%Arguments:%[/b]%%[/u]%)
(echo( [variable] Variable to assign the active code page number to)
(echo()
(echo(%[b]%%[u]%Options:%[/b]%%[/u]%)
(echo( %[b]%-h%[/b]% or %[b]%-?%[/b]% Print help)
) && goto :EOF
:EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment