From 20c3ec425e468108142e5650837031c520630e09 Mon Sep 17 00:00:00 2001 From: kneutron <50146127+kneutron@users.noreply.github.com> Date: Wed, 5 Apr 2023 10:03:47 -0500 Subject: [PATCH] Add files via upload --- powershell/DVDs.csv | 3 + .../create-output-from-variable-csv-input.ps1 | 64 +++++++++++++++++++ powershell/hats.csv | 4 ++ powershell/pants.csv | 6 ++ powershell/shirts.csv | 5 ++ 5 files changed, 82 insertions(+) create mode 100644 powershell/DVDs.csv create mode 100644 powershell/create-output-from-variable-csv-input.ps1 create mode 100644 powershell/hats.csv create mode 100644 powershell/pants.csv create mode 100644 powershell/shirts.csv diff --git a/powershell/DVDs.csv b/powershell/DVDs.csv new file mode 100644 index 0000000..55d222a --- /dev/null +++ b/powershell/DVDs.csv @@ -0,0 +1,3 @@ +Peter +Paul +Mary \ No newline at end of file diff --git a/powershell/create-output-from-variable-csv-input.ps1 b/powershell/create-output-from-variable-csv-input.ps1 new file mode 100644 index 0000000..3df6953 --- /dev/null +++ b/powershell/create-output-from-variable-csv-input.ps1 @@ -0,0 +1,64 @@ +<# Create an output csv based on multiple variable input CSV files +REF: https://www.reddit.com/r/PowerShell/comments/12bxnmq/multiple_csv_filenames_used_as_columns_of_new_csv/ + +Mod for powershell on OSX, should also run on Linux +#> + +# Define the path to the directory containing the text lists +$directoryPath = "$HOME/csv-var-inputs" # "C:\path\to\directory" + +# Get a list of all the csv files in the directory +$listPaths = Get-ChildItem $directoryPath -Filter *.csv | Select-Object -ExpandProperty FullName + +# Create an empty hashtable to store the names +$names = @{} + +# Loop through each list and add the names to the hashtable +foreach ($listPath in $listPaths) { + +# Get the names from the current list + $listNames = Get-Content $listPath + +# Add each name to the hashtable + foreach ($name in $listNames) { + + if ($names.ContainsKey($name)) { +# If the name already exists in the hashtable, add the list name to the existing entry + $names[$name] += ", $($listPath |Split-Path -Leaf)" + } else { +# If the name doesn't exist in the hashtable, create a new entry with the list name + $names[$name] = $($listPath |Split-Path -Leaf) + } # if containskey + } # foreach name +} # foreach input csv + +# Convert the hashtable to a table and output to a CSV file +$table = New-Object System.Data.DataTable + +# Add columns for the "Name" and each list +$table.Columns.Add("Name", [string]) + +foreach ($listPath in $listPaths) { + $columnName = $($listPath |Split-Path -Leaf) + $column = New-Object System.Data.DataColumn($columnName, [string]) + $table.Columns.Add($column) +} + +# Add rows to the table for each name +foreach ($name in $names.Keys) { + $row = $table.NewRow() + $row["Name"] = $name + + foreach ($listPath in $listPaths) { + $columnName = $($listPath |Split-Path -Leaf) + + if ($names[$name] -like "*$columnName*") { + $row[$columnName] = "X" + } + } + $table.Rows.Add($row) +} + +$tablesort = $table |Sort-Object Name +$tablesort |Export-Csv "$HOME/variable-table-output.csv" -NoTypeInformation +ls -lh "$HOME/variable-table-output.csv" \ No newline at end of file diff --git a/powershell/hats.csv b/powershell/hats.csv new file mode 100644 index 0000000..d08b454 --- /dev/null +++ b/powershell/hats.csv @@ -0,0 +1,4 @@ +Sam +Mary +Trevor +Ted diff --git a/powershell/pants.csv b/powershell/pants.csv new file mode 100644 index 0000000..8fa2f56 --- /dev/null +++ b/powershell/pants.csv @@ -0,0 +1,6 @@ +Bob +Mary +Jane +Ted +Paul +Peter diff --git a/powershell/shirts.csv b/powershell/shirts.csv new file mode 100644 index 0000000..b23fa40 --- /dev/null +++ b/powershell/shirts.csv @@ -0,0 +1,5 @@ +Bob +Mary +Jane +Ted +Peter \ No newline at end of file