Locate file, and copy its path. Batch script

2019-08-14 19:22发布

问题:

I am working on a batch script to automate building a Qt project.

One issue i am having is the fact that the install directory path of Qt may not be the same for every user.

For example, on my system the path of my mingw48_32 is: c:\Qt\Qt5.2.0\5.2.0\mingw48_32 but on someone elses system it may be c:\Qt\5.2.0\mingw48_32 depending on how they chose to set it up.

So when i am specifying the path for the qmake.exe, i need to know that the path to qmake.exe is.

How can i search for a file, and copy its path from a batch script?

回答1:

whence.bat

Not really like the real whence, but it might be helpful.

@SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
@SET EXITCODE=1

:: Needs an argument.

@IF "x%1"=="x" (
    @ECHO Usage: %0 ^<progName^>
    GOTO TheEnd
)

@set newline=^


@REM Previous two (2) blank lines are required. Do not change!

@REM Ensure that the current working directory is first
@REM because that is where DOS looks first.
@PATH=.;!PATH!

@FOR /F "tokens=*" %%i in ("%PATH:;=!newline!%") DO @(
    @IF EXIST %%i\%1 (
        @ECHO %%i\%1
        @SET EXITCODE=0
    )

    @FOR /F "tokens=*" %%x in ("%PATHEXT:;=!newline!%") DO @(
        @IF EXIST %%i\%1%%x (
            @ECHO %%i\%1%%x
            @SET EXITCODE=0
        )
    )
)

:TheEnd
@EXIT /B %EXITCODE%