Skip to content

Instantly share code, notes, and snippets.

@x5lcfd
Created February 16, 2017 03:43
Show Gist options
  • Save x5lcfd/1acc4e4ca252967de8aa1b6f78d25fd5 to your computer and use it in GitHub Desktop.
Save x5lcfd/1acc4e4ca252967de8aa1b6f78d25fd5 to your computer and use it in GitHub Desktop.
:: setvar varname cmd
:: Set VARNAME to the output of CMD
:: Triple escape pipes, eg:
:: setvar x dir c:\ ^^^| sort
:: -----------------------------
:: http://stackoverflow.com/a/25954264/6181205
@echo off
SETLOCAL
:: Get command from argument
for /F "tokens=1,*" %%a in ("%*") do set cmd=%%b
:: Get output and set var
for /F "usebackq delims=" %%a in (`%cmd%`) do (
ENDLOCAL
set %1=%%a
)
:: Show results
SETLOCAL EnableDelayedExpansion
echo %1=!%1!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment