Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何在带有通配符的子目录中查找文件_Powershell_Batch File_Appveyor - Fatal编程技术网

Powershell 如何在带有通配符的子目录中查找文件

Powershell 如何在带有通配符的子目录中查找文件,powershell,batch-file,appveyor,Powershell,Batch File,Appveyor,我希望在子目录中找到给定文件的所有位置,子目录中可以包含通配符 在un*x上,我会使用如下内容: find ba* -type f -name "foo" -exec dirname {} bash$ MINGW_DIR=$(find /c/mingw-w64/${pattern} -type f -name "mingw64-make.exe" -exec dirname {} | tail -1) 不幸的是,我需要在Windows上使用BAT或PowerShell来实现这一点(我真的不在

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

在un*x上,我会使用如下内容:

find ba* -type f -name "foo" -exec dirname {}
bash$ MINGW_DIR=$(find /c/mingw-w64/${pattern} -type f -name "mingw64-make.exe" -exec dirname {} | tail -1)
不幸的是,我需要在Windows上使用BAT或PowerShell来实现这一点(我真的不在乎,但我对W32中的脚本编写知之甚少的是BAT)

到目前为止,我已经想出了

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

所以我尝试了嵌套循环:

FOR /D %d IN (ba*) DO FOR /R bar %f %d IN (foo*) DO @echo %~pf
但这只给了我一个错误,%f不能在这里用语法(德语:“%f”kann syntaktisch nicht an dieser Stelle verwendet werden)

背景 我试图解决的实际问题是:

在MinGW中,安装被隐藏在一些神秘的目录中,如
C:\MinGW-w64\x86\u 64-6.3.0-posix-seh-rt\u v5-rev1
。 我没有逐字指定这些神秘的路径(在appveyor映像的升级方面,这些路径很可能很脆弱),而是考虑在我的CI配置中只指定类似于
*x86_64-6.3.*
的内容,然后让CI脚本搜索所需二进制文件的路径(例如
mingw64 make.exe
)并自动将其添加到我的
路径中。
在多个匹配的情况下,我想我只会使用找到的路径中的任何一个(例如,第一个或最后一个)

e、 g.类似于:

find ba* -type f -name "foo" -exec dirname {}
bash$ MINGW_DIR=$(find /c/mingw-w64/${pattern} -type f -name "mingw64-make.exe" -exec dirname {} | tail -1)
响应:


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


使用批处理可以做到这一点的一种方法是使用findstr命令

批处理文件中的示例:

@echo
findstr /i /s /C:"random" *.*
pause
/i
使搜索不区分大小写

/s
表示在子目录中搜索

C/:
表示搜索此特定字符串

输出将采用以下形式:

路径
包含字符串[可能是整个文件]的文本

您还可以组合
dir
findstr
,如下所示:

for /f "delims=." %%a in ('dir /B /A-D ^| findstr /i /s /C:"random" *.bat') do (
    echo "%%a"
    pause
    )
上面的内容将递归地遍历您的根目录,并打印出每个文件的位置[点击空格继续,因为pause按照tin上的说明执行]

您可以将
echo%%a
替换为
SET PATH=%PATH%%;%%a
将在
findstr
中找到的路径添加到环境路径

“delims=。”
将使用延长期作为打印/匹配的截止日期。如果没有它,您将获得部分文件名

dir/b/a-d
意味着它将以裸格式打印当前目录,但不会打印文件夹

[这些示例是为批处理文件设计的]


希望有帮助

“这个问题与PowerShell有什么关系?”Bill_Stewart我的微弱尝试处于BAT状态,但如果有人能给我指出正确的方向,我很乐意使用PS解决方案。(我怀疑PS在执行此类任务时会有比普通BAT更好的工具)是的,我绝对推荐PowerShell,因为它在几乎所有可以想象的方面都优于cmd.exe。请参阅我的答案以获取示例。