PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 15, 2022

[FIXED] How can get output of command like this to a variable

 August 15, 2022     batch-file, output, variables     No comments   

Issue

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%

Solution

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)



Answered By - Stephan
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing