1

I'm writing a batch file to import power plans in windows 10. My script is mostly done but I need a way to alert the user if there is an error. Ideally, the script would echo the exact error and prompt the user to hit a key to acknowledge it. Here's my code so far:

powercfg -import "%UserProfile%\Desktop\Powercfg CMIT Defalut\CMIT Win10 Power Plan.pow"

******ERROR CODE PROBABLY SHOULD GO HERE?********

start "" cmd /c "echo CMIT Win10 PowerCFG Imported &echo(&pause"
FloatingKiwi
  • 4,408
  • 1
  • 17
  • 41
AsherV
  • 37
  • 1
  • 1
  • 8

2 Answers2

1

Redirect the error messages to a new file called error.txt

powercfg -import "%UserProfile%\Desktop\Powercfg CMIT Defalut\CMIT Win10 Power Plan.pow" 2> __error__.txt

Note: this assumes that the errors are output to stderr

Then you can check the error code , echo the response, and use pause to prompt for the key press

if %errorlevel% NEQ 0 (
  echo Failed 
  type __error__.txt
) else (
    echo Power plan successfully imported
)
pause
FloatingKiwi
  • 4,408
  • 1
  • 17
  • 41
  • By using that approach, is the error also echo'd in the cmd window or does the user have to open the txt file to read the error message? Thanks! – AsherV Sep 24 '16 at 01:56
  • `type __error__.txt` prints the error to the console. – FloatingKiwi Sep 24 '16 at 07:43
  • YES! This is what I'm looking for, Thank you! Any idea how to hide all but the actual error in the cmd response when there is an error? Currently, when there is an error, it outputs quite a bit of unnecessary info like the path to the __error__.txt when all I need is the actual error, such as "File not found" – AsherV Sep 26 '16 at 16:55
  • Add `@echo off` to the top of the file to disable the echoing of commands. – FloatingKiwi Sep 27 '16 at 01:38
  • That worked. Thanks! How do I display a success message IF there was no error? Ideally, it would say something like: "Power Plan Successfully Imported. Please hit any key to continue" – AsherV Sep 29 '16 at 20:34
1

if %errorlevel% NEQ 0

echo Script Failed!

J03L
  • 314
  • 3
  • 17