Locate file, and copy its path. Batch script

余生长醉 提交于 2019-12-13 18:33:37

问题


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%


来源:https://stackoverflow.com/questions/31575817/locate-file-and-copy-its-path-batch-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!