# Pour avoir les accents, choisir l'encodage UTF-8-BOM # 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..." $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", "X", "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*" Write-host "Création du .dat avec les opérations suivantes:" # Pour chaque opération ForEach ($operation in $planches){ # Operation $ope = $($operation.planche).Split(" ")[1] #$ope # ZEP/Groupement #CCTT 8948 "ZEP DJ20948" (702+712+714+720+722+724+726+728) $zep = $($operation.zep).Split(" ")[1] #$zep # Debut $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 # Fin $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 = "" # Rptx $rptx = $($operation.rptx).Split(":")[1] #$rptx # N° CP $cp = $($operation.ncp) #$cp # Tel $tel = "0000000000" #$tel # Terminaison $term = "||||||||||||||||||||||||||" $ligne = "$($ope)|1|$($zep)|$($voie)|$($debut)|$($fin)|$($rptx)|$($cp)|$($tel)$term" Write-host $ligne # Chaque opération est écrite dans le fichier MGPT_.dat #echo $ligne >> $env:USERPROFILE/Desktop/MGPT_$dd.dat $dat_file = "$env:USERPROFILE\Desktop\MGPT_$dd.dat" Add-Content $dat_file $ligne } Write-host "Le fichier ajt $dat_file a été crée." [string]$Export = Read-Host "Voulez-vous exporter le fichier ajt ? [o/n]" IF ($Export -eq "o") { Write-host "Insérer une clé USB..." $d = Get-Item $dat_file $dat = $d.Basename $dest_dat_file = Save-FileName -initialDirectory "%USERDATA%\desktop\" -name $dat $dest_dat_file <# $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..." Copy-Item -Path $dat_file -Destination $dest_dat_file }