Advertisement
costi0n

bonifica_csv

May 23rd, 2023
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. $invocation = (Get-Variable MyInvocation).Value
  3. $directorypath = Split-Path $invocation.MyCommand.Path
  4. $path = $directorypath + "\"
  5.  
  6.  
  7. # Definisci il nome del file di input e output
  8. $inputFile = $path + "creazione_nuovi_utenti.csv"
  9. $outputFile = $path + "Creazione_Nuovi_Utenti_BONIFICATO.csv"
  10.  
  11.  
  12. # Leggi il file CSV usando ';' come delimitatore
  13. $data = Import-Csv -Path $inputFile -Delimiter ';'
  14.  
  15. # Crea un oggetto DataTable per manipolare i dati
  16. $table = New-Object System.Data.DataTable
  17.  
  18. # Aggiungi le colonne al DataTable
  19. $data[0].PSObject.Properties | ForEach-Object {
  20.     $table.Columns.Add($_.Name) | Out-Null
  21. }
  22.  
  23. # Aggiungi le righe al DataTable
  24. $data | ForEach-Object {
  25.     $row = $table.NewRow()
  26.     $_.PSObject.Properties | ForEach-Object {
  27.         $row[$_.Name] = $_.Value
  28.     }
  29.     $table.Rows.Add($row) | Out-Null
  30. }
  31.  
  32. # Elenco delle colonne da rimuovere
  33. $columnsToRemove = @()
  34.  
  35. # Identifica le colonne da rimuovere
  36. $table.Columns | ForEach-Object {
  37.     $column = $_
  38.     $isEmpty = $true
  39.     foreach ($row in $table.Rows) {
  40.         if (![string]::IsNullOrEmpty($row[$column.ColumnName])) {
  41.             $isEmpty = $false
  42.             break
  43.         }
  44.     }
  45.     if ($isEmpty) {
  46.         $columnsToRemove += $column.ColumnName
  47.     }
  48. }
  49.  
  50. # Rimuovi le colonne identificate
  51. foreach ($column in $columnsToRemove) {
  52.     $table.Columns.Remove($column) | Out-Null
  53. }
  54.  
  55. # Rimuovi le righe vuote
  56. $data = $data | Where-Object { $_.PSObject.Properties.Value -ne $null -and $_.PSObject.Properties.Value -ne '' }
  57.  
  58. # Scrivi i dati puliti nel file di output
  59. $data | Export-Csv -Path $outputFile -Delimiter ';' -NoTypeInformation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement