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

Ric*_*fen 4 powershell

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

基本上我需要从每组孩子中获取孩子,然后只返回没有robots.txt文件的父母.我在这里遇到的问题是我在嵌套for循环中的$ _给了我CWD,而不是我正在搜索的目录的子代.我知道我可能不得不使用 - 在这里,但我有点过头了,对PowerShell很新.任何帮助表示赞赏!

$drv = gci C:\Projects | %{
    $parent = $_; gci -exclude "robots.txt" | %{$_.parent}} | gu
Run Code Online (Sandbox Code Playgroud)

x0n*_*x0n 6

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

# 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"
}
Run Code Online (Sandbox Code Playgroud)