Skip to content

Commit a40d869

Browse files
committed
add :print, :capture
1 parent 7313b75 commit a40d869

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

modules/miscs.cmd

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,34 @@ goto :eof
4343
)
4444
endlocal & set "%result_var%=%win_major_ver%"
4545
goto :eof
46+
47+
48+
:print <MESSAGE>
49+
:: outputs the message to the console without trailing new line characters
50+
echo|set /p="%~1"
51+
goto :eof
52+
53+
54+
:capture <CMD> [<RESULT_VAR>] [<LINE_SEPARATOR>]
55+
:: captures the output of a command in a variable
56+
setlocal EnableDelayedExpansion
57+
set cmd=%~1
58+
set result_var=%~2
59+
set line_separator=%~3
60+
if not defined line_separator (
61+
set line_separator=^& echo.
62+
)
63+
set stdout=
64+
for /F "delims=" %%f in ('%cmd%') do (
65+
if defined stdout (
66+
set "stdout=!stdout!!line_separator!%%f"
67+
) else (
68+
set "stdout=%%f"
69+
)
70+
)
71+
if not defined result_var (
72+
echo %stdout%
73+
exit /B 0
74+
)
75+
endlocal & set "%result_var%=%stdout%"
76+
goto :eof

0 commit comments

Comments
 (0)