0

I want to create a batch file to retrieve a password from an EXE on the computer. It is a password that changes everyday.

I don't get any results with the code below.

set /p input="C:\Program Files\Password\PasswordGenerator
if %input%==q3Ym9fb goto YES

if not %input%==q3Ym9fb goto NO

:YES

echo correct password
pause

:NO

echo incorrect password 
pause
5
  • set /p input is for getting keyboard input, not the output from running a command.
    – DavidPostill
    Jun 12, 2019 at 20:12
  • You need to use For - Loop through command output - Windows CMD - SS64.com to parse the output from C:\Program Files\Password\PasswordGenerator
    – DavidPostill
    Jun 12, 2019 at 20:13
  • @DavidPostill: There are other problems with his script, if you are minded to give an answer.
    – harrymc
    Jun 12, 2019 at 20:16
  • @harrymc I'm not, primarily because I'm not sure what he is actually trying to achieve. Feel free to answer if you want.
    – DavidPostill
    Jun 12, 2019 at 20:18
  • I am trying to get the password from a password generator tool and save it where I can use the password to schedule a task. Also the password generator will only generate letters and number no special characters.
    – drive
    Jun 12, 2019 at 21:08

1 Answer 1

0

The correct way to set a variable to the result of a command is:

FOR /F "tokens=* USEBACKQ" %%F IN (`command`) DO SET var=%%F
ECHO %var%

In case of any error your IF command might have a syntax error, and also if the password generator returns a string with non-alphanumeric characters.

You should be comparing strings like this:

if "%input%"=="q3Ym9fb" goto YES

I also note that by reversing the order of the YES and NO labels, your second IF becomes redundant.

In addition, after returning from the PAUSE of the first label, execution will continue to the second label. If that is not your intention, you should add an EXIT command.

1
  • Where "tokens=*" will strip leading spaces from command output (what may be desired). To get unmodified output use "delims="
    – LotPings
    Jun 12, 2019 at 20:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .