Powershell 返回包含特定文件但不完全递归的目录

标签 powershell

使用以下 Powershell 代码,我试图在根目录中找到不包含 robots.txt 的文件夹。通常,我可以递归地执行此操作,但是递归这个庞大的文件夹结构需要 FOREVER。我真正需要的只是第一级,也就是只搜索 C:\Projects 中的文件夹。

基本上我需要从每组 child 中获取 child ,然后才返回没有 robots.txt 文件的 parent 。我在这里遇到的问题是嵌套 for 循环中的 $_ 给了我 CWD,而不是我正在搜索的目录的子级。我知道我可能必须在这里使用 -Where ,但我有点不知所措,而且对 powershell 还很陌生。任何帮助表示赞赏!

$drv = gci C:\Projects | %{
    $parent = $_; gci -exclude "robots.txt" | %{$_.parent}} | gu

最佳答案

这个单行(为了清楚起见,分散在几个)应该为你做的伎俩:

# look directly in projects, not recursively
dir c:\projects | where {
    # returns true if $_ is a container (i.e. a folder)
    $_.psiscontainer
} | where {
    # test for existence of a file called "robots.txt"
    !(test-path (join-path $_.fullname "robots.txt"))
} | foreach {
    # do what you want to do here with $_
    "$($_.fullname) does not have robots.txt"
}

关于Powershell 返回包含特定文件但不完全递归的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11658073/

相关文章:

powershell - invoke-expression 不会在 powershell 中抛出错误

Powershell 脚本 : combine two array in execution command

powershell - powershell中的op_Division和数据类型

windows - 在 Windows 上,如何终止所有在目录中打开文件的进程?

powershell - 如何在 PowerShell 中正确/全局地转换 UTF-8(无 BOM)文件? (到另一个文件)

json - 如何在 PowerShell 中解析来自 Invoke-WebRequest 的 JSON?

powershell - 我缺少什么来取代我想要的一切?

Powershell 随机选择和计数

windows - 有什么办法可以使用Powershell在文件夹中找到 'metadata'

android - 从 PowerShell 使用 System.IO 访问 MTP 存储