RadFox
RadFox

Reputation: 419

Search a directory structure and report folder present or missing?

I am looking for some help with writing the code in PowerShell to check for and report back.

I want to search C:\MyData\MyCustomerNames for folders MyTests, MyProducts, and MyReturns, and then report back C:\MyData\MyCustomerNames\MyTests is Present or C:\MyData\MyCustomerNames\MyProducts is Missing.

I know that the code Test-Path C:\MyData\MyCustomerNames\MyProductscould work but I also want to test for *.xml, *.docx, etc. Plus, the only part of the path that is a variable is MyCustomerNames.

If you can point me to something that would work, that would be awesome, or provide an example more so than what I provided.

Please and Thank you!

Upvotes: 1

Views: 120

Answers (1)

BenH
BenH

Reputation: 10044

To expand on what sodawillow recommended. Save your locations in an array, then use the foreach statement to run the loop for each value in the array. You can use Get-ChildItem in an if statement to search for those directories and evaluate if they exist.

$folders = "MyTests", "MyProducts", "MyReturns"
foreach ($folder in $folders) {
    if (Get-ChildItem -Path "C:\MyData\MyCustomerNames" -Filter $folder -Directory) {
        "$folder exists"
    } else {
        "$folder does not exist"
    }
}

Upvotes: 3

Related Questions