############################# # # # MGPT Ajt Créateur # # # ############################# # Pour avoir les accents, choisir l'encodage UTF-8-BOM pour le fichier du script PS # Ouvrir ajt_creator en passant le fichier .xlsx en paramètre. # .\ajt_creator "C:\Users\bruno\Desktop\xls\essai ve 15-05.xlsx" param([string]$ajt_file) Write-host "`n ###################" Write-host " MGPT Ajt Créateur" Write-host " ###################" Function Save-FileName($initialDirectory, $name){ [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null $SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog $SaveFileDialog.initialDirectory = $initialDirectory $SaveFileDialog.FileName = $name; $SaveFileDialog.filter = “DAT files (*.dat)|*.dat|All files (*.*)|*.*” $SaveFileDialog.ShowDialog() | Out-Null $SaveFileDialog.filename } Function Get-FileName($initialDirectory) { [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog $OpenFileDialog.initialDirectory = $initialDirectory $OpenFileDialog.filter = "Excel Files|*.xls;*.xlsx" $OpenFileDialog.ShowDialog() | Out-Null $OpenFileDialog.filename } # Mettre le fichier .xlsx dans un dossier xls sur le bureau #$ajt_file = "$env:USERPROFILE\Desktop\xls\essai ve 15-05.xlsx" <# # Entrer le nom de fichier au prompt $inputfile = read-host "Entrer le chemin du fichier .xlsx" # xls\essai ve 15-05.xlsx $inputdata = get-content $inputfile #> # Choisir le fichier .xlsx par une boite de dialogue (sur le bureau par défaut) if ([string]::IsNullOrEmpty($ajt_file)) { Write-host "`nChoisir le fichier .xlsx ..." $ajt_file = Get-FileName "$env:USERPROFILE\Desktop" } # Si aucun fichier .xlsx fourni, on quitte le script. if ([string]::IsNullOrWhitespace($ajt_file)) { #[Environment]::Exit(1) # quit Powershell Write-host "Aucun fichier choisi. On quitte le script ..." Break } Write-host "On importe le .xlsx ..." $ajt = Get-Item $ajt_file # Nom long du fichier .xlsx $ExcelFileName = $ajt.fullname # Nom court du fichier .csv $CSVBaseName = $ajt.Basename # Chemin du .xlsx $CSVpath = Split-Path -Path $ajt_file # Nom long du fichier .csv $CSVFileName = $CSVpath + "\" + $CSVBaseName + ".csv" <# Write-host "ExcelFileName: $ExcelFileName" Write-host "CSVBaseName: $CSVBaseName" Write-host "CSVpath: $CSVpath" Write-host "CSVFileName: $CSVFileName" #> # Export to .csv (6) $xlCSV=6 # Conversion .xlsx vers .csv Write-host "Conversion du fichier .xlsx en .csv ..." $Excel = New-Object -comobject Excel.Application $Excel.Visible = $False $Excel.displayalerts=$False $Workbook = $Excel.Workbooks.Open($ExcelFileName) $Workbook.SaveAs($CSVFileName,$xlCSV) $Excel.Quit() If(ps excel){kill -name excel} # On efface / backup tous les fichiers MGPT*.dat existants Write-host "On efface / backup tous les fichiers MGPT*.dat existants dans le dossier MGPT_Ajt_Archives ..." $fileToCheck = "$env:USERPROFILE\Desktop\MGPT*.dat" $archivePath = "$env:USERPROFILE\Desktop\MGPT_Ajt_Archives" if (Test-Path $fileToCheck -PathType leaf) { # leaf: file, container: folder, any: tous #Remove-Item $fileToCheck if (!(Test-Path $archivePath -PathType container)) { mkdir $archivePath } Move-Item -Path $fileToCheck -Destination $archivePath -Force } # On importe le .csv selon $header Write-host "On importe le .csv ..." $header = "planche", "d_deb", "jour", "h_deb", "h_fin", "J", "d_fin", "lieu", "VC", "gare", "zep", "encadrant", "rptx", "ncp", "get" #$A = import-csv -Path "$env:USERPROFILE\Desktop\xls\essai ve 15-05.csv" -Delimiter ";" -Header $header $A = import-csv -Path $CSVFileName -Delimiter ";" -Header $header # On sélectionne uniquement les lignes commençant par AK ou BF #$planches = $A | Where-Object planche -Match "^AK*|^BF*" # On sélectionne uniquement les lignes dont le champ Voie/Catenaire est "V" $planches = $A | Where-Object VC -eq "V" $long = $planches.length Write-host "$long planches trouvées ..." if (![string]::IsNullOrWhitespace($planches)) { Write-host "Aucune planche S9 MGPTable" } Write-host "On supprime les planches incomplètes..." Write-host "Création du .dat avec les opérations suivantes:" $i = 0 $filename = "" # Pour chaque opération ForEach ($operation in $planches){ # Operation# BF 123456 $ope = $($operation.planche).Split(" ")[1] # - si 123456 #$ope = $($operation.planche) #$ope # ZEP/Groupement # ZEP 305 / GROUPEMENT 8704 #CCTT 8948 "ZEP DJ20948" (702+712+714+720+722+724+726+728) $zep = $($operation.zep).Split(" ")[1] #$zep # Debut # 15/05/2020 / 8:50 $dd = $($operation.d_deb) $y = $($dd.Split("/")[2]) $m = $($dd.Split("/")[1]) $d = $($dd.Split("/")[0]) $dd = "$($y)$($m)$($d)" $hd = $($operation.h_deb).Replace(":", "") $debut = "$($dd)$($hd)" #$debut # Nom du fichier .dat à créer # MGPT_BF_0000131428_20200511_1504_U.dat if ([string]::IsNullOrWhitespace($filename)) { $a = Get-Date -Format "HHmm" $filename = "MGPT_BF_0000131428_$($dd)_$($a)_U.dat" #$filename $dat_file = "$env:USERPROFILE\Desktop\$($filename).dat" } # Fin # 15/05/2020 / 8:50 $df = $($operation.d_fin) $y = $($df.Split("/")[2]) $m = $($df.Split("/")[1]) $d = $($df.Split("/")[0]) $df = "$($y)$($m)$($d)" $hf = $($operation.h_fin).Replace(":", "") $fin = "$($df)$($hf)" #$fin # Voie $voie = "" # Encadrant # Train:7899 / Début:220 $encadrant = $($operation.encadrant).Split(":")[1] $encadrant # Dernière ZEP if (![string]::IsNullOrWhitespace($encadrant)) { $derniere_zep = $($zep).Split("+")[1] $derniere_zep } # Rptx # RPTx:VILPOUX $rptx = $($operation.rptx).Split(":")[1] #$rptx # N° CP # 7607869Y $cp = $($operation.ncp) #$cp # Tel $tel = "000000000$i" #$tel # Terminaison (26 barres) $term = "||||||||||||||||||||||||||" # Construction de la ligne $ligne = "$($ope)|1|$($zep)|$($voie)|$($debut)|$($fin)|$($rptx)|$($cp)|$($tel)$term" #$dat_file = "$env:USERPROFILE\Desktop\$($filename).dat" # On supprime les lignes dont 1 des champs est nul: rptx, cp, ope if (![string]::IsNullOrWhitespace($rptx) -And ![string]::IsNullOrWhitespace($cp) -And ![string]::IsNullOrWhitespace($ope)) { Write-host $ligne Add-Content $dat_file $ligne } $i++ } Write-host "Le fichier ajt $dat_file a été crée sur le bureau." [string]$Export = Read-Host "Voulez-vous exporter le fichier ajt ? [o/n]" IF ($Export -eq "o") { Write-host "Insérer une clé USB..." # Routine attente clé à faire # On choisit la destination $d = Get-Item $dat_file $dat = $d.Basename $dest_dat_file = Save-FileName -initialDirectory "%USERDATA%\desktop\" -name $dat <# $e = Get-Item $dest_dat_file $e.Basename $e.fullname $destination = Split-Path -Path $e $destination Copy-Item -Path $dat_file -Destination $destination #> Write-host "Copie du fichier ajt sur la destination..." Write-host " => $dat_file vers $dest_dat_file" Copy-Item -Path $dat_file -Destination $dest_dat_file Write-host "Pensez à copier le fichier ajt sur le transit et à le récupérer sur le PC Ajt." }