Get a list of directories without a given subdirectory

Suppose I have a directory tree like the following:

 |-- Store1
 |   |-- Clients
 |   |-- Orders
 |-- Store2
 |   |-- Clients
 |   |-- Orders
 |-- Store3
 |   |-- Clients
 |-- Store4
 |   |-- Clients
 
i.e., some directories have an "Orders" subdirectory and others don't. I would like a Windows Powershell script to get a list of all the directories which have no "Orders" subdirectory, and output the results to a file. This is similar to my previous question "Get list of nonempty directories".

I am using Windows 7.
RothbardAsked:
Who is Participating?
 
Brent ChallisPrincipal: ITCommented:
The following script assumes that you are only looking one folder down from the base folder.

$folderBase = "C:\Stores"
$reportFileName = "c:\Stores\report.txt"
$subFolderNames = Get-ChildItem $folderBase -Directory | Select-Object -ExpandProperty FullName

#Initialise file with a time stamp
("Report on folders without the Orders subfolder run at {0}"-f (Get-Date)) | Out-File $reportFileName

foreach ($subfolderName in $subFolderNames)
{
    if (Test-Path "$subfolderName\Orders")
    {
        Write-Verbose "Orders found in $subfolderName"
    }
    else
    {
        $subfolderName | Out-File $reportFileName -Append
    }
}
1
 
SubsunCommented:
Try..
GCI "C:\Folder" | ?{$_.PSIsContainer} |%{
	If (!(GCI $_.FullName | ?{$_.PSIsContainer -and $_.Name -eq "Orders"})){
	$_.FullName
	}
}| Out-file C:\Temp\result.txt

Open in new window

result.txt file will have the full path of the folders which doesn't have Orders folder..
1
 
oBdACommented:
Get-ChildItem -Path D:\AllStores -Directory | ? {!(Get-ChildItem -Path $_.FullName -Filter "Orders")}

Open in new window

You can pipe the result through additional Select-Object and finally to Set-Content as needed, for example like this:
Get-ChildItem -Path D:\AllStores -Directory | ? {!(Get-ChildItem -Path $_.FullName -Filter "Orders")} | Select-Object -ExpandProperty FullName | Set-Content -Path D:\Temp\test.log

Open in new window

1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.