1

I need to populate a powershell array with the filenames of files stored in a directory based on their names. I need a different array for each set of files (see Example folder structure) to be used as part of another process.

I would like this to be automatic and create the arrays on the fly, so I would have arrays containing all the filenames starting with processor123, another for processorabc and processorxyz. The names of these arrays can be stored in another array named $arrynames. It needs to be dynamic in case a new processor is introduced, I do not want to rely on users to enter the file names.

Example folder structure;

c:\directory\processor123\processor123.log.20150604
c:\directory\processor123\processor123.log.20150603
c:\directory\processor123\processor123.log.20150602
c:\directory\processor123\processor123.log.20150601
c:\directory\processor123\processor123.log.20150531
c:\directory\processorabc.log.20150604
c:\directory\processorabc.log.20150603
c:\directory\processorabc.log.20150602
c:\directory\processorabc.log.20150601
c:\directory\processorabc.log.20150531
c:\directory\processorxyz.log.20150604
c:\directory\processorxyz.log.20150603
c:\directory\processorxyz.log.20150602
c:\directory\processorxyz.log.20150601
c:\directory\processorxyz.log.20150531

$ArryNames

processor123
processorabc
processorxyz

$processor123

c:\directory\processor123\processor123.log.20150604
c:\directory\processor123\processor123.log.20150603
c:\directory\processor123\processor123.log.20150602
c:\directory\processor123\processor123.log.20150601
c:\directory\processor123\processor123.log.20150531

$processorabc

c:\directory\processorabc.log.20150604
c:\directory\processorabc.log.20150603
c:\directory\processorabc.log.20150602
c:\directory\processorabc.log.20150601
c:\directory\processorabc.log.20150531
5
  • 1
    Have you tried anything yourself? Commented Jun 4, 2015 at 12:43
  • Ive tried a couple of things but nothing works, I'm pretty new to the scripting world Commented Jun 4, 2015 at 13:00
  • 2
    Even if you're "new", you should have posted some code in your question indicating that you have tried something. What you ask for is doable, although I say you'd better not go with a variable named $processorabc, as it's pretty hard to refer a variable by its name. Instead, you might want to use $sometable["processor123"]. If there would be a "processor" named ArryNames, you'll be screwed, but using a hash table will still work, you'll just get a `$sometable["ArryNames"] and no breaking of the script. Commented Jun 4, 2015 at 13:12
  • Ok, apologies, thanks for the heads up on hash tables, I will work on this, looks promising. Commented Jun 4, 2015 at 13:15
  • 3
    @Vesper Not only to indicate that he tried something, but to help the rest of us a) avoid wasting time on things he already tried and/or b) point out potential mistakes in his previous attempts. Commented Jun 4, 2015 at 13:15

1 Answer 1

1

So, what you'll want to do for each string:

  1. Extract first part of file name
  2. Put entire string into array matching the first part

As already mentioned in comments, you can use a hashtable to hold the arrays, with the file prefix being the key:

# Set up a hashtable
$HashTable = @{}

# Pipe your list of file paths to ForEach-Object
Get-Content C:\filenames.txt |ForEach-Object {
    # Extract the filename with [System.IO.Path]::GetFileName(), split it by dots and grab only the part before the first dot
    $Prefix = [System.IO.Path]::GetFileName($_) -split "\." |Select-Object -First 1
    # Use the extracted prefix to place the string in the correct hashtable entry
    $HashTable[$Prefix] += $_
}

Now, you can reference the first 5 paths with: $HashTable["processor123"]

Sign up to request clarification or add additional context in comments.

1 Comment

you sir are a genius, That works exactly as desired. Having never used hash tables before this is all new to me, thankyou for adding all the comments as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.