powershell - 如何使用通配符在子目录中查找文件

标签 powershell batch-file appveyor

我想在子目录中找到给定文件的所有位置,该子目录中可以包含通配符。

在un * x上,我将使用类似以下内容:

find ba* -type f -name "foo" -exec dirname {}

不幸的是,我需要在Windows上使用BAT或PowerShell来执行此操作(我并不在乎,但是我对W32中的脚本的了解很少是关于BAT的)。

到目前为止,我已经提出了
FOR /R bar %f IN (foo*) DO @echo %~pf

但这不允许在目录参数中使用通配符,例如FOR /R ba* %f IN (foo*) DO @echo %~pf无效。

所以我尝试了嵌套循环:
FOR /D %d IN (ba*) DO FOR /R bar %f %d IN (foo*) DO @echo %~pf

但这只是给我一个错误,%f在此位置不能被语法使用(德语:“%f” kann syntaktisch nicht and dyers Stelle verwendet werden)。

背景

我要解决的实际问题是:

AppVeyor中,MinGW装置被埋在诸如C:\mingw-w64\x86_64-6.3.0-posix-seh-rt_v5-rev1之类的一些神秘目录中。
我想到没有逐字指定这些 secret 路径(就升级传送者图像而言,它们很可能是脆弱的),我想到的是仅在我的CI配置中指定诸如*x86_64-6.3.*之类的内容,然后让CI脚本搜索所需路径二进制文件(例如mingw64-make.exe)并将其自动添加到我的PATH中。
在多次匹配的情况下,我想我只会使用找到的路径中的任何一个(例如第一个或最后一个)。

例如就像是:
bash$ MINGW_DIR=$(find /c/mingw-w64/${pattern} -type f -name "mingw64-make.exe" -exec dirname {} | tail -1)

响应:
cmd> FOR /D %d IN (%pattern%) DO FOR /R bar %f %d IN (mingw64-make.*) DO @set %MINGW_DIR%=%~pf

最佳答案

如何在子目录中找到文件并将该子目录添加到当前 session 的路径(PowerShell)的示例:

$executablePath = Get-ChildItem -Filter mingw64-make.exe -File -Recurse |
  Select-Object -ExpandProperty DirectoryName
if ( $executablePath ) {
  if ( $env:Path[$env:Path.Length - 1] -ne ";" ) {
    $env:Path += ";"
  }
  $env:Path += $executablePath
}

关于powershell - 如何使用通配符在子目录中查找文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48671600/

相关文章:

batch-file - 为什么在Windows 7上无法访问名为__CD__的变量?

由于缺少 GSL,R 包构建在 Windows 机器 (AppVeyor) 上失败 - GNU Scientific Library

python - “Choco install python”在 AppVeyor 上失败,出现 1603

inno-setup - Inno Setup 无法在 AppVeyor 上使用掩码找到图像文件

powershell - 使用 Powershell DSC 包资源安装 exe 会获得返回代码 1619

powershell - 如何在命名空间中组织类?

arrays - 在 Powershell 中避免不可知的锯齿状阵列展平

windows cmd批处理文件计算

windows - 用于检查系统中已安装程序及其版本的脚本

java - 如何使用PowerShell脚本在Windows上启动本地neo4j服务器?