Home > database >  How can get output of command like this to a variable
How can get output of command like this to a variable

Time:02-05

how can get output of this command to a variable.

wevtutil qe system "/q:*[System [(EventID=1074)]]" /rd:true /f:text /c:1 | findstr /i "date"

my solution dose not working

FOR /F %i IN ('wevtutil qe system "/q:*[System [(EventID=1074)]]" /rd:true /f:text /c:1 | findstr /i "date"') DO set VARIABLE=%i
echo %VARIABLE%

CodePudding user response:

You have to "escape" the | to tell the parser that it's part of the inner command (for ... in ('command') ...). Without escaping, it ends the forcommand too early, leading to a script-breaking error.

Also in batchfiles, you have to double the % for the metavariables.

Third, for gets the first token ("word") only without specification which token(s) to use and which delimiters to use to separate them.

@echo off
FOR /F "delims=" %%i IN ('wevtutil qe system "/q:*[System [(EventID=1074)]]" /rd:true /f:text /c:1 ^| findstr /i "date"') DO set VARIABLE=%%i
echo %VARIABLE%

To get the date only, use "tokens=2" instead of "delims=" (to take the second token ("word"), using standard delimiters space, tab, comma)

CodePudding user response:

operators need to be escaped in paranthesis. | becomes (^|), also ( ^& ^> ^^ )

  •  Tags:  
  • Related