0

I'm working on a portable development enviroment, so I need to find the route of the bins.I have tried with this:

for /f "tokens=*" %%a in ('dir mysqld.exe /b /s') do set mysql=%%a 

It returns A:\test\mysql-5.7.24-winx64\bin\mysqld.exe but I want to get only the folder, like this A:\test\mysql-5.7.24-winx64\bin\, how can I achieve this?.

1
  • Based upon the advice provided in the supplied answers, if you're searching on the same drive that your batch file is located, then be sure to include its drive letter, as the code provided is based upon the current directory, (which may not necessarily be the same). For /F "Delims=" %%A In ('Dir /B/S/A-D-L "%~d0\mysqld.exe" 2^>Nul')Do Set "mysqldir=%%~dpA" or For /R %~d0\ %%A In (mysqld.exe)Do Set "mysqldir=%%~dpA" Commented Jun 8, 2019 at 13:29

2 Answers 2

2

use modifiers (described in for /?):

for /f "tokens=*" %%a in ('dir mysqld.exe /b /s') do set "mysql=%%~dpa" echo %mysql% 

%%~dpa returns drive and path only.

Same without using dir (as for /r can recursively search for (a) file(s)):

for /r "c:\startfolder" %%a in (mysqld.exe) do set "mysql=%%~dpa" 

"c:\startfolder" is the start point for recursive search (you can omit it to search within the current folder and it's subfolders)

Note: if more there is than one matching file, the variable will hold the last finding.

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

1 Comment

Thank you, the second option It's a lot faster.
0

Use a subroutine with a parameter. read about parameters here

Something like this. Remove what I added for display.

@echo off for /f "tokens=*" %%a in ('dir mysqld.exe /b /s') do call :GetFolder "%%a" echo(%mysql% pause goto :eof :GetFolder REM 1=Filespec set "MySQL=%~dp1" goto :eof 

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.