4

Using the following Powershell code, I'm trying to locate folders which do NOT contain robots.txt in the root. Normally, I could do this recursively but it's taking FOREVER to recurse this massive folder structure. What I really need is only the first level, AKA search only the folders found in C:\Projects.

Basically I need to get children from each set of children, and only then return parents where there was no robots.txt file. The problem I'm having here is my $_ in the nested for loop is giving me the CWD, not the children of the directory I'm searching. I know I might have to use a -Where here but I'm a little over my head and pretty new to powershell. Any help is appreciated!

$drv = gci C:\Projects | %{
    $parent = $_; gci -exclude "robots.txt" | %{$_.parent}} | gu
x0n
  • 51,312
  • 7
  • 89
  • 111
Richthofen
  • 2,076
  • 19
  • 39

1 Answers1

6

this one-liner (spread over several for clarity) should do the trick for you:

# 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"
}
John Conde
  • 217,595
  • 99
  • 455
  • 496
x0n
  • 51,312
  • 7
  • 89
  • 111
  • Thanks. Test-Path, while not as elegant as recursing the child directories again, is much faster and easier to read. – Richthofen Jul 25 '12 at 21:21
  • 1
    You think multiple recursion with depth limiting hacks is elegant? Really? :) – x0n Jul 27 '12 at 00:01
  • Well, I was trying to write it in Pseudo-code first, and the multiple recursions was the easiest way to 'picture' what I wanted. But knowing there's a test-path method out there, well, I guess that makes things much easiser. – Richthofen Jul 27 '12 at 18:15
  • Not sure from which version, but doing Get-ChildItem c:\Projects -Directory does the filtering of directories for you. – Rashack Oct 27 '17 at 10:02
  • @Rashack Yeah, as does `dir -ad` in newer versions of powershell. I'm not sure if it was supported back in 2012 when I wrote this answer initiallly. – x0n Oct 27 '17 at 14:44