1

I have a little batch script that gets the temporary password from mysql.err file.

But I have a problem when a password contains : that is used in delimiter match resulting password will not get as is..

Is there any other approach to get correctly the password?

@echo off

setlocal enableextensions enabledelayedexpansion

set mysql_dir=C:\database\mysql

echo.
echo.
echo Search For Current MySQL Password
FOR /F "tokens=4 delims=:" %%G in ('find /I "A temporary password is generated for root@localhost:" ^<%mysql_dir%\data\%ComputerName%.err') DO set temp_mysql_password="%%G"

echo.
echo.
echo Current MySQL password: %temp_mysql_password%

The mysql.err file content is:

2017-08-31T07:38:36.195916Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-08-31T07:38:36.205943Z 22 [Note] A temporary password is generated for root@localhost: v>dqu;;&)7:Y
2017-08-31T07:38:42.510215Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

And the problem is with that the password
v>dqu;;&)7:Y** that contains : will be extracted as:
v>dqu;;&)7.

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
oriceon
  • 359
  • 2
  • 6
  • 17

2 Answers2

2

If you'd used "tokens=3* delims=:" and the next for variable %%H you'd get the unparsed password.
The asterisk representing the remainder of the input without further splitting at delims.

@echo off & setlocal enableextensions enabledelayedexpansion
set mysql_dir=C:\database\mysql
echo.
echo.
echo Search For Current MySQL Password
FOR /F "tokens=3* delims=:" %%G in (
    'find /I "A temporary password is generated for root@localhost:" ^<%mysql_dir%\data\%ComputerName%.err'
) DO set "temp_mysql_password=%%H"
set "temp_mysql_password=%temp_mysql_password:~1%"
echo.
echo.
echo Current MySQL password: %temp_mysql_password%
  • The password will be: `" v>dqu;;&)7:Y"` (with quotes). Test it with: `echo Current MySQL password: ^<%temp_mysql_password%^>`. But even if the set command (causing the quotes) is right: `set "temp_mysql_password=%%H"` it will be with a beginning whitespace. Okay this can easily be removed, so it's a nevertheless a good answer after the corrections. Note that it just works for: `root@localhost`. – Andre Kampling Aug 31 '17 at 11:38
  • @AndreKampling Agreed, I wasn't thorough enaugh revising the code. Changed. –  Sep 01 '17 at 18:22
  • works with every user, when changing the search string to just `"A temporary password is generated for "` – Stephan Sep 01 '17 at 19:25
1

This way without delimiters it will work:

...
for /F "delims=" %%G in ('find /I "A temporary password is generated for" ^<%mysql_dir%\data\%ComputerName%.err') do (
   set "temp_mysql_password=%%G"
   set "temp_mysql_password=!temp_mysql_password:*A temporary password is generated for root@localhost: =!"
)


echo.
echo.
rem Note the ! around the variable
echo Current MySQL password: !temp_mysql_password!

It uses string manipulation which is explained here: Remove - Remove a substring using string substitution.

Note that it will just work for root@localhost because that string is part of the string searched for substitution.

To make it work for any user@host combination you have to use 3 string substitutions instead of one:

set "temp_mysql_password=!temp_mysql_password:*A temporary password is generated for =!"
set "temp_mysql_password=!temp_mysql_password:*@=!"
set "temp_mysql_password=!temp_mysql_password:*: =!"

Output:

Current MySQL password: v>dqu;;&)7:Y

To use your solution anyway (using tokens and delims) you could concate all the delimited parts, see here on SO how it works.

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47