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?

2
  • Does this answer your question? Assign command output to variable in batch file Commented Oct 25, 2020 at 20:18
  • With the additional double-quotes the =-sign appears unquoted to the parser, hence you need to escape it like ^= Commented Oct 25, 2020 at 22:57

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.