Home > front end >  Create and Compare Arrays From List of Files
Create and Compare Arrays From List of Files

Time:02-23

I have a list of teams, and a folder with multiple files. What I'm trying to accomplish is to only send the files, together, that correspond to the correct team.

Example:

$TeamA: UserA,UserB,UserC
$TeamB: UserD,UserE,UserF

Files in the folder follow the following naming convention:

[directory]\UserA Errors Report 2022-02-15.xlsx
[directory]\UserB Errors Report 2022-02-15.xlsx
[directory]\UserC Errors Report 2022-02-15.xlsx

So I want to send the files that contain UserA/B/C in the name, to X, the files that contain UserD/E/F to Y, etc.

I imagine the best method is to save the file locations to an array, then check if the directories contain the username of members of Team A, attach those files. I am having trouble completing this specific task.

File Locations:

$FileNames = $LatestFiles.Name

My attempt at saving the "matching" results to a variable:

$Comparison = foreach ($member in $FileNames) {
    if ($TeamA | Where-Object { $_ -contains $FileNames}) {
        Write-Output $member
       #I only want to save the file names that match members of TeamA to the "$Comparison" variable.
    }
}

However the code above returns blank, so there's something I am doing wrong.

Please let me know if there's a much better approach.

I have a solution but it's not very elegant:

$test1 = $LatestFiles |
ForEach-Object {
    $fileName = $_.BaseName -split ' '
    Write-Output @{
        User = $fileName[0]
        Tester = $fileName[1]
        Date = $fileName[2]
        File = $_
    }
} 

$ArrayTest = $test1 | Where-Object User -In $TeamA 

$Target = $ArrayTest.File.Name | ForEach-Object { @{ Name = "C:\X\X\Documents\Error Reporting\$($LatestFolder.Name)\$($_)" } }

But this means for all other teams, I'll have to create variables to further store their locations as well. IT seems inefficient.

CodePudding user response:

What you're currently doing seems OK, below is just a different way of doing it using a hash table where the Keys are the User's IDs and the Values are the Team they belong to. Once we have this we can use Group-Object to group all files by Team ID and then enumerate the groups. This is mainly an example for you to play with and see how it works.

$TeamA = 'UserA', 'UserB', 'UserC'
$TeamB = 'UserD', 'UserE', 'UserF'

<#
Here we create a map that would look like this:

Name                           Value
----                           -----
UserD                          TeamB
UserC                          TeamA
UserB                          TeamA
UserA                          TeamA
UserE                          TeamB
UserF                          TeamB
#>

$teamMap = @{}
$TeamA.ForEach({ $teamMap[$_] = 'TeamA' })
$TeamB.ForEach({ $teamMap[$_] = 'TeamB' })

# This is just for testing, yours would be like:
# $files = Get-ChildItem lastestfolder -Filter *.xlsx
$files = [System.IO.FileInfo[]](@'
UserA Errors Report 2022-02-15.xlsx
UserB Errors Report 2022-02-15.xlsx
UserC Errors Report 2022-02-15.xlsx
UserD Errors Report 2022-02-15.xlsx
UserE Errors Report 2022-02-15.xlsx
UserF Errors Report 2022-02-15.xlsx
'@ -split '\r?\n')

# Split the BaseName of the files and get the first token, then we can do
# something like `$teamMap['UserA']` which would give us `TeamA` as a Result
# This is our "grouping condition"
$files | Group-Object { $teamMap[$_.BaseName.Split(' ')[0]] } | ForEach-Object {
    foreach($file in $_.Group) {
        [pscustomobject]@{
            Team = $_.Name
            File = $file.FullName
        }
    }
}

Above example for me results in something like this, since I'm not sure what your expected output is, I would expect you to handle it as you please.

Team  File
----  ----
TeamA /home/user/Documents/UserA Errors Report 2022-02-15.xlsx
TeamA /home/user/Documents/UserB Errors Report 2022-02-15.xlsx
TeamA /home/user/Documents/UserC Errors Report 2022-02-15.xlsx
TeamB /home/user/Documents/UserD Errors Report 2022-02-15.xlsx
TeamB /home/user/Documents/UserE Errors Report 2022-02-15.xlsx
TeamB /home/user/Documents/UserF Errors Report 2022-02-15.xlsx
  • Related