1

So I have

Array = [testfile001_TR1,
testfile001_TR2,
testfile001_TR3,
testfile002_TR1,
testfile002_TR2,
testfile002_TR3]

I would like to split the above Array into multiple arrays

Array[0] = testfile001_tr1,testfile001_tr2,testfile001_tr3 

Array[1] = testfile002_tr1,testfile002_tr2,testfile002_tr3 
4
  • 2
    What have you tried so far? Please show your code and explain what is not working as expected. Commented Nov 12, 2021 at 0:44
  • 2
    ... and you might explain what your actual goal is ... there might be better options than you think of at the moment. ;-) Commented Nov 12, 2021 at 0:56
  • @Olaf Thanks for the response, but I'm so new at this that I'm not sure where to begin. I've been searching the web, but haven't really found what I was looking for. I have a folder containing 10-20 files that are named in the similar format as above. Currently when the script is executed, it opens a Windows dialog allowing me to select files that I want to process. I select the files and the script processes the audio files. What I'd like to do is select all the files in the folder > split the files into their ownseparate Arrays and then process each array consisting of 3 files each. Commented Nov 16, 2021 at 22:00
  • If I got you right it should be enogh to sort the files according to their filename and process them in chunks of 3. Commented Nov 16, 2021 at 23:01

2 Answers 2

1

Splitting an array into two arrays (collections) by a given criterion can be achieved with the .Where() array method:

$array = 
  'testfile001_TR1',
  'testfile001_TR2',
  'testfile001_TR3',
  'testfile002_TR1',
  'testfile002_TR2',
  'testfile002_TR3'

# Note: $coll1 and $coll2 aren't technically *arrays*, but for
#       most purposes behave like ones.
$coll1, $coll2 = $array.Where({ $_ -like 'testfile001*' }, 'Split')

Efficiently splitting into multiple collections requires extra work, using a switch statement and an efficiently extensible collection (list) type, [System.Collections.Generic[T]]:

$array = 
  'testfile001_TR1',
  'testfile001_TR2',
  'testfile001_TR3',
  'testfile002_TR1',
  'testfile002_TR2',
  'testfile002_TR3',
  'testfile003_TR1',
  'testfile003_TR2',
  'testfile003_TR3'

$lists = 
 [System.Collections.Generic.List[string]]::new(),
 [System.Collections.Generic.List[string]]::new(),
 [System.Collections.Generic.List[string]]::new()

switch -Wildcard ($array) {
  'testfile001*' { $lists[0].Add($_) }
  'testfile002*' { $lists[1].Add($_) }
  default        { $lists[2].Add($_) }
}
Sign up to request clarification or add additional context in comments.

Comments

0

If I got it right something like this should work actually.

First you get all desired files from the directory you specify sorted by their file names:

$FileList = 
    Get-ChildItem -Path 'D:\sample' -Filter 'testfile*.mp3' | 
        Sort-Object -Property BaseName 

The result would be something similar to this:

$FileList = 
    'testfile001_TR1',
    'testfile001_TR2',
    'testfile001_TR3',
    'testfile002_TR1',
    'testfile002_TR2',
    'testfile002_TR3',
    'testfile256_TR1',
    'testfile256_TR2',
    'testfile256_TR3'

Now you can use a for loop to process the files in chunks of 3 as you like:

for ($i = 0; $i -lt $FileList.Count; $i += 3) {
    "Chunk Number '$(($i/3) + 1 )'"
    "Do something meaningful with the file #1 '$($FileList[$i])'"
    "Do something meaningful with the file #2 '$($FileList[$i + 1])'"
    "Do something meaningful with the file #3 '$($FileList[$i + 2])'"
}

The sample output would look like this:

Chunk Number '1'
Do something meaningful with the file #1 'testfile001_TR1'
Do something meaningful with the file #2 'testfile001_TR2'
Do something meaningful with the file #3 'testfile001_TR3'
Chunk Number '2'
Do something meaningful with the file #1 'testfile002_TR1'
Do something meaningful with the file #2 'testfile002_TR2'
Do something meaningful with the file #3 'testfile002_TR3'
Chunk Number '3'
Do something meaningful with the file #1 'testfile256_TR1'
Do something meaningful with the file #2 'testfile256_TR2'
Do something meaningful with the file #3 'testfile256_TR3'

Comments

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.