Is it possible to output the result of a command to a variable when using pipes?
Example batch file script:
@echo off cls echo Script Started setlocal enableextensions ::prove that functionality works without pipes call:OutputCommandToVariable "set computername" demo echo.%demo% ::prove that the command itself works call:IsServiceStartAutoDebug "MyComputerName" "wuauserv" ::now try it for real call:IsServiceStartAuto "MyComputerName" "wuauserv" ::done goto:end :IsServiceStartAuto call:OutputCommandToVariable "sc \\%~1 qc "%~2" | find "START_TYPE" | find "2"" IsServiceStartAuto echo.%IsServiceStartAuto% @goto:eof :OutputCommandToVariable set "%2=" for /f "delims=" %%a in ('%~1') do @set "%2=%%a" ::for /f "usebackq tokens=*" %%a in (`%~1`) do @set "%2=%%a" @goto:eof :IsServiceStartAutoDebug sc \\%~1 qc "%~2" | find "START_TYPE" | find "2" @goto:eof :end Echo Completed ::pause
Script Started COMPUTERNAME=MyComputerName START_TYPE : 2 AUTO_START | was unexpected at this time.
Script Started COMPUTERNAME=MyComputerName START_TYPE : 2 AUTO_START START_TYPE : 2 AUTO_START Completed
:OutputCommandToVariable SET val=%1 SET val=%val:|=^|% set "%2=" for /f "delims=" %%a in (%val%) do set "%2=%%a" ::for /f "usebackq tokens=*" %%a in (`%~1`) do @set "%2=%%a" goto:eof
I'd try these changes, but I can guarantee nothing since whatever the
sc is doing does nothing apparent on my machine.
The issue is that whereas the string is correctly applied, you need to escape the pipe character within the single-quotes contained between the parentheses in the Court Of King Caractacus.
I did try adding the caret to escape the pipe in the quoted argument to
OutputCommandToVariable - but it intriguingly gained interest and appeared as a double-caret - possibly batch trying to escape the caret rather than the pipe...
Answer author Magoo
Tickanswer.com is providing the only single recommended solution of the question Windows Batch File :: Output to Variable when using Pipes under the categories i.e windows , command-line , batch-file , pipe , . Our team of experts filter the best solution for you.