0

im using adb shell commands to check if my devices' screen is on or not

adb shell dumpsys power | find "mWakefulness="

if the screen is off this command returns mWakefulness=Asleep

what i want to do is assign the output of that command to a variable and run it through an if statement to check if mWakefulness=Asleep

ive tried this

@echo off
for /f "delims=" %%a in ('adb shell dumpsys power | find "mWakefulness="') do (
    Set "Output=%%a"
)
Echo Output Result = "%OutPut%"

but this returns | was unexpected at this time.

so i tried to surround adb shell dumpsys power | find "mWakefulness=" with double quotes

@echo off
for /f "delims=" %%a in ('"adb shell dumpsys power | find "mWakefulness=""') do (
    Set "Output=%%a"
)
Echo Output Result = "%OutPut%"

but this returns Output Result = ""

can anyone help?

dam1ne
  • 181
  • 1
  • 2
  • 17
  • Does this answer your question? [Assign command output to variable in batch file](https://stackoverflow.com/questions/16203629/assign-command-output-to-variable-in-batch-file) – T3RR0R Oct 25 '20 at 20:18
  • With the additional double-quotes the `=`-sign appears unquoted to the parser, hence you need to escape it like `^=`… – aschipfl Oct 25 '20 at 22:57

1 Answers1

1

In your original code, you need to "escape" the | by preceding it with a caret (^).

This tells batch that the pipe is part of the command-to-be-executed, not the FOR statement

Magoo
  • 77,302
  • 8
  • 62
  • 84