1

I am working with two data sets. One is used to filter the other. The base dataset is a set of folders using a naming scheme like this:

003
003 rev 1
003 rev 2
004
005
etc...

I have a second set of numbers that signify which folders I want to pull from the first set. I import them using

$numSet = Import-Csv $csvLoc | Where {$_.ItemCategory -eq "CatagoryName"} | Select-Object -ExpandProperty "Folder ID"

and they looks something like this:

C:\>$numSet
003
005

So far this is exactly how I want it. Now what I want to do is, using the Get-ChildItem command, import the locations of the folders so that I can work with them.

# This is about how I believe I should go about this.
$folderLocationArray = Get-ChildItem -Path $sourceDir | Where {$numSet.Contains($_.Name)}

Because I need to check against an array the obvious choice would be -contains, except that doesn't allow me to use wildcards, and the rev # that comes after some of the file names means that those files don't show up. This is what happens:

C:\>$folderLocationArray
003
005

I have also tried -match and -like with the same results. Instead of an error, the script either sits and acts like it is running, or returns a blank like this:

C:\>script.ps1 #This is set up to output $folderLocationArray

C:>

I know that the basics are sound, because I can plug in a single number and it works, and even with an array as the input it still returns files, just not the ones with rev # in them.

I'm at a loss. How can I use Get-ChildItem and filter the array against an array?

Edit: To clarify.

I have have a list of numbers. I have a bunch of folders. Every folder starts with a number. Some share the same number. I want to know which folders match the list.

7
  • Im not sure what you are asking. Are you just trying to compare 2 arrays and find the same or unique? Commented Jun 22, 2018 at 19:29
  • I want filter the folder names by the numbers, so that only the folders that have the number in their name remain. Commented Jun 22, 2018 at 19:31
  • So you want all folders that start with a number correct? Commented Jun 22, 2018 at 19:32
  • Clarification: If 003 is in the array, do you want the folders 003, 003 Rev 1 and 003 Rev 2 to be considered "hits"? Commented Jun 22, 2018 at 19:34
  • @JeffZeitlin That's right. All the folders will start with a number. I want only the folders that start with the numbers in the array that I create. Commented Jun 22, 2018 at 19:34

2 Answers 2

3

Filter Results based on array

@("001","002","023","006") | foreach{Get-ChildItem -Path $path -filter "$_*" -Recurse -Directory}
Sign up to request clarification or add additional context in comments.

Comments

3

This will use a filter based on an item in the $numset array

$numset = @('003', '005')

foreach ($base in $numset) {
    Get-ChildItem -Directory -Recurse -Filter $($base + "*")
}

2 Comments

Unfortunately I wasn't able to get this one working, and the one ArcSet put forward has. I will still try this on Monday to double check, thanks!
@HairyDresden - Ok. The two (2) solutions are basically the same. I added a -Recurse to this one which might be why it did not see the directories.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.