@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "mysqldatapath="
FOR %%a IN (wamp wamp64) DO (
FOR /f %%d IN ('dir /ad /b "%sourcedir%\%%a\bin\mysql\mysql*" 2^>nul') DO (
IF exist "%sourcedir%\%%a\bin\mysql\%%d\data" (
SET "candidate=%sourcedir%\%%a\bin\mysql\%%d"
CALL :latest %%d
)
)
)
ECHO mysqldatapath=%mysqldatapath%
GOTO :EOF
:: latest version required? Format %1 may be mysqlA.C.E or mysqlAB.CD.EF
:latest
IF NOT defined mysqldatapath GOTO newver
:: is newly-detected version in %1 preferred to %sql_ver%?
SET "new_ver=%1"
:: remove "mysql"
SET "new_ver=%new_ver:~5%"
:: replace '.' with ' '
SET "new_ver=%new_ver:.= %"
CALL :vcompare %sql_ver% %new_ver%
:: :vcompare will clear 'new_ver' if new version is preferred
IF NOT DEFINED new_ver GOTO :eof
:newver
SET "mysqldatapath=%candidate%"
SET "sql_ver=%1"
:: remove "mysql"
SET "sql_ver=%sql_ver:~5%"
:: replace '.' with ' '
SET "sql_ver=%sql_ver:.= %"
GOTO :eof
:: version comparison. old old old new new new as 1 or 2-digit
:vcompare
IF "%4"=="" GOTO :EOF
SET "v_1=0000%1"
SET "v_4=0000%4"
SET /a v_1=1%v_1:~-3%
SET /a v_4=1%v_4:~-3%
IF %v_1%==%v_4% shift&GOTO vcompare
IF %v_1% gtr %v_4% SET "new_ver="
GOTO :eof
You would need to change the setting of sourcedir to suit your circumstances.
Not as easy as one may at first expect. The tricky part is in selecting the required version from the candidates.
The first block searches wamp and then wamp64 for a directory named mysql* and then nominates a candidate if the mysql* directory contains data. The :latest routine then checks the version numbers and selects the latest to be assigned to mysqldatapath.
:latest sets new_ver to the version number found, eg. 5.7.19 and converts this to 5 7 19. The :vcompare routine then does the mechanics of comparing the three levels, taking care of the potential leading-zero on each level. Note that attempting to compare using literals here will fail, as 5.10.01 would be less than 5.9.99 if compared as a literal, but is actually a later (and one would expect, greater) version.