The humble for command has accumulated some interesting capabilities over the years:
D:> FOR /F "delims=" %i IN ('date /t') DO set today=%i
D:> echo %today%
Sat 20/09/2008
Note that "delims="
overwrites the default space and tab delimiters so that the output of the date command gets gobbled all at once.
To capture multi-line output, it can still essentially be a one-liner (using the variable lf as the delimiter in the resulting variable):
REM NB:in a batch file, need to use %%i not %i
setlocal EnableDelayedExpansion
SET lf=-
FOR /F "delims=" %%i IN ('dir /b') DO if ("!out!"=="") (set out=%%i) else (set out=!out!%lf%%%i)
ECHO %out%
To capture a piped expression, use ^|
:
FOR /F "delims=" %%i IN ('svn info . ^| findstr "Root:"') DO set "URL=%%i"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…