0

Me again, I've just update the code with errors outfiles Still have two issues, the bad shape of my xml output (looks like crap) And also in my xml output if You execute the code you'll see that it generates files in the wrong way . Let me explain, if there is an error in the second line for example the xml will contain line 1 & 3, like he's skipping line 2 (which is normal because line 2 is in error) but not including line 4. At the end I want 3 elements per XML!!! and not 1 or 2 in case of error. If you do not understand just play the code.

@Theo : you were right about calling the counter variables in the for each loop

Here is the csv model (including errors for tests)

UCB63_DATENUM;U6618_FILENAME;UF6E8_CANAL;U65B8_IDRP
9/11/19 23:03;457E6659_ZN_LIQRLVPR_A_V_ML.pdf;ML;1
9/11/19 23:03;49453878_ZN_LIQRLVPR_A_V_ML.pdf;ML;2
9/11/19 23:03;497E585B_ZN_LIQRLVPR_A_V_CS.pdf;CS;3
2/12/20 22:12;58453B75_ZN_LIQRLVPR_A_V_ML.pdf;ML;4
7/8/19 22:27;457E6659_ZN_LIQRLVPR_A_V_ML.pdf;ML;5
;49453878_ZN_LIQRLVPR_A_V_ML.pdf;ML;6
9/11/19 23:03;497E585B_ZN_LIQRLVPR_A_V_CS.pdf;CS;7
2/12/20 22:12;58453B75_ZN_LIQRLVPR_A_V_ML.pdf;ML;8
2/12/20 22:12;58453B75_ZN_LIQRLVPR_A_V_ML.pdf;ML;







#vARIABLES EN DUR
    $FREQUENCE_DECOMPTE     = 'Nom="FREQUENCE_DECOMPTE" Valeur="MENS"'
    $LIBELLE_ORGANISME      = 'Nom="LIBELLE_ORGANISME" Valeur="HUMANIS CCN OG"'
    $MONTANT_TOTAL          = 'Nom="MONTANT_TOTAL" Valeur="0"'
    $POLE                   = 'Nom="POLE" Valeur="1ADP"'
    $CODE_ORGANISME         = 'Nom="CODE_ORGANISME" Valeur="1ADP"'
    
    # Paramètre nombre item par xml VALEUR A MODIFIER A 5000
    $TailleLot = 3
    
    # Nombre de process restants
    $CountItems = 0
    $countXml = 1
    
    # Tests ligne OK/KO
    $ligneOK = 0
    $ligneKO = 0
    $TotalLigne = 0
    
    
    
    #Sortie ok/ko CSV
        
    $FichierSortie = [pscustomobject]@{
                    "NumAssure" = $item.U65B8_IDRP
                    "Date" = $item.UCB63_DATENUM
                    "NomFichier" = $item.U6618_FILENAME
                    "Canal" = $item.UF6E8_CANAL
                    "Etat" = ""}   
    
          
    
    #Import du csv et création des différentes collections 
    $liste = Import-Csv -path 'c:\temp\testH.csv' -Delimiter ';'
    [System.Collections.ArrayList] $DateErrors = @()
    [System.Collections.ArrayList] $FileNameErrors = @()
    [System.Collections.ArrayList] $CanalErrors = @()
    [System.Collections.ArrayList] $NumAssureErrors = @()
    
    
    #création fichier
    $xmlFile = "C:\Temp\MIG_ERELEVE_MM_$(get-date -f dd-MM-yyyy)_$countXml.xml"
    
    
      #En-tête
    $output = @"
    <?xml version="1.0" encoding="utf-8"?>
    <Documents Origine="ERELEVE_HUM">
    
    "@
    
    
    
    foreach($item in $liste)
    {
       
       $FichierSortie = [pscustomobject]@{
                    "NumAssure" = $item.U65B8_IDRP
                    "Date" = $item.UCB63_DATENUM
                    "NomFichier" = $item.U6618_FILENAME
                    "Canal" = $item.UF6E8_CANAL
                    "Etat" = ""}   
    
        Write-Output "test for each"
    
        #Initiation variables booléennes 
        $MyDateIsCorrect = $true
        $MyFileNameIsCorrect = $true
        $MyCanalIsCorrect = $true
        $MyNumAssureIsCorrect = $true
         
    
    
        $CountItems ++
        $TotalLigne ++
        
        
      
      
        #Transformations données
        $date = $($item.UCB63_DATENUM -split " ")[0]
        $renommage = [System.IO.Path]::GetFileNameWithoutExtension($item.U6618_FILENAME)
    
        if([System.String]::IsNullOrEmpty($item.UCB63_DATENUM)) 
            { 
            $MyDateIsCorrect = $false
            $DateErrors.Add($item)
            $ligneKO ++
            $FichierSortie.Etat = "KO" 
            
            }
    
        else   {
                if($date -notmatch "[0-9]{1,2}[\/][0-9]{1,2}[\/][0-9]{1,2}") 
                    { 
                    $MyDateIsCorrect = $false
                    $DateErrors.Add($item)
                    $ligneKO ++ 
                    $FichierSortie.Etat = "KO"
                                   
                    } 
               }
        
        if([System.String]::IsNullOrEmpty($item.U6618_FILENAME)) 
            { 
            $MyFileNameIsCorrect = $false
            $FileNameErrors.Add($item)
            $ligneKO ++ 
            $FichierSortie.Etat = "KO" 
                 
            } 
               
        else   { 
                 if($item.U6618_FILENAME -notmatch ".+[\.]pdf") 
                    { 
                    $MyFileNameIsCorrect = $false
                    $FileNameErrors.Add($item)
                    $ligneKO ++ 
                    $FichierSortie.Etat = "KO"
                                    
                    }
               }      
        
        if([System.String]::IsNullOrEmpty($item.UF6E8_CANAL)) 
            { 
            $MyCanalIsCorrect = $false
            $CanalErrors.Add($item)
            $ligneKO ++ 
            $FichierSortie.Etat = "KO" 
                   
            }
        
        if([System.String]::IsNullOrEmpty($item.U65B8_IDRP)) 
            { 
            $MyNumAssureIsCorrect = $false
            $NumAssureErrors.Add($item)
            $ligneKO ++   
            $FichierSortie.Etat = "KO" 
                 
            }
    
           
            #Génération output XML
    
    if($MyDateIsCorrect -and $MyFileNameIsCorrect -and $MyCanalIsCorrect -and $MyNumAssureIsCorrect)
                            {
                            $ligneOK ++
                            $FichierSortie.Etat = "OK" 
                            
                                      
           
    $output += @"
              <Document>
                    <Index Nom="TITLE" Valeur="$renommage"/>
                    <Index Nom="NO_ASSURE" Valeur="$($item.U65B8_IDRP)"/>
                    <Index Nom="DEBUT_PERIODE" Valeur="$RecupDateFinTraitement"/>
                    <Index Nom="FIN_PERIODE" Valeur="$RecupDateFin30"/>
                    <Index $FREQUENCE_DECOMPTE/>
                    <Index $LIBELLE_ORGANISME/>
                    <Index $MONTANT_TOTAL/>
                    <Index Nom="DATE_GENERATION_DECOMPTE"$RecupDateFinTraitement/>
                    <Index $POLE/>
                    <Index $CODE_ORGANISME/>
                    <Index Nom="ALERTE_MAIL" Valeur="$fin"/>
                    <Fichier Nom="$($item.U6618_FILENAME)"/>
              </Document>`r`n              
    "@        
                              
                            }
                           
                          
    
    if ($CountItems -eq $TailleLot)
      {
    
      Write-Output "test premier if"
      
    
      #Pied de page
    
        $output += "`r`n </Documents>"
        $output | Set-Content -Path $xmlFile -Encoding UTF8 
    
      $CountItems = 0
      $countXml += 1
      
      $xmlFile = "C:\Temp\MIG_ERELEVE_MH_$(get-date -f dd-MM-yyyy)_$countXml.xml"
    
    
       #En-tête
    $output = 
    @"
    <?xml version="1.0" encoding="utf-8"?>
    <Documents Origine="ERELEVE_HUM">
    
    "@
    
      } 
     
      $FichierSortie | export-csv -Path C:\Temp\SortieGlobaleOK_KO.csv -NoTypeInformation -Encoding UTF8 -Delimiter ';' -Append         
    }
    
    
    
    $FichierSortieNbreLignes = [pscustomobject]@{
                    "LignesOK" = $ligneOK
                    "LignesKO" = $ligneKO
                    "TotalLignes" = $TotalLigne}
    
    
     
    
    
    Write-Output "ligne OK $ligneOK"
    Write-Output "ligne en erreur $ligneKO"
    Write-Output "Total ligne $TotalLigne"
    
    
    #Fichiers de sortie d'erreur et de comptage
    
    $FichierSortieNbreLignes | Export-Csv -NoTypeInformation -Path c:\temp\ComptageLigneOK_KO.csv -Delimiter ';' -Encoding UTF8    
    $DateErrors.ToArray() | Export-Csv -Path c:\temp\DateErrors.csv -NoTypeInformation
    $FileNameErrors.ToArray() | Export-Csv -Path c:\temp\FileNameErrors.csv -NoTypeInformation
    $CanalErrors.ToArray() | Export-Csv -Path c:\temp\CanalErrors.csv -NoTypeInformation
    $NumAssureErrors.ToArray() | Export-Csv -Path c:\temp\NumAssureErrors.csv -NoTypeInformation 
  • I don't understand why you changed the [code in this answer](https://stackoverflow.com/a/62831246/9898643) to totally ignore the file counter called `$xmlFileCount` and for that matter all other counters (`$itemCount`, `$restant`, `$currentItem`) and decided to save the XML **outside** the loop.. Also, I fail to understand why you went back to using `$output +=` constructions. As I said earlier, changing the `while` into a `foreach` loop takes more work to have the counters do their job of keeping track of the number of items in the file, the file number, the items you want to skip etc. – Theo Jul 15 '20 at 08:38
  • Your code is great and I kept it but I'd like to understand how to build this counter in my for each loop. The thing is I could use your work but if one day I need to debug it, I'm not sure I could be able to. – Hadrien Beaujean Jul 15 '20 at 13:28
  • Oh, but I'm sure you can. Please check my [earlier answer](https://stackoverflow.com/a/62831246/9898643) again. I have added quite a lot of explaining to it, so you can follow the logic. – Theo Jul 15 '20 at 14:02
  • I know my solution is much more dirty than yours but I had to find a solution which looks like me, a little messy maybe. Thanx a lot for your help Theo, I keep yours for future – Hadrien Beaujean Jul 15 '20 at 14:56

0 Answers0