使用以下 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/