suistrop
XLDnaute Impliqué
Bonjour,
J ai demandé à chatgpt et il me donne le code suivant mais je ne sais pas comment le faire fonctionner ..
# ================== CONFIG ==================
$Root = "C:\chemin\vers\vos\egp" # Dossier racine (parcours récursif)
$Out = "C:\chemin\sortie\egp_code_inventory.csv" # Fichier CSV de sortie
$Delimiter = ';' # ';' conseillé pour Excel FR (',' sinon)
# ============================================
# Assembly .NET zip (utile si Expand-Archive indisponible)
try { Add-Type -AssemblyName System.IO.Compression.FileSystem } catch {}
# -------- Helpers ----------
function New-TempDir {
$d = Join-Path $env:TEMP ([System.IO.Path]::GetRandomFileName())
New-Item -ItemType Directory -Force -Path $d | Out-Null
return (Get-Item $d)
}
function Extract-Egp {
param([string]$EgpPath, [string]$Dest)
try {
Expand-Archive -LiteralPath $EgpPath -DestinationPath $Dest -Force
} catch {
[System.IO.Compression.ZipFile]::ExtractToDirectory($EgpPath, $Dest)
}
}
function Normalize([string]$s) {
if ($null -eq $s) { return "" }
# Normalise les fins de ligne en LF (\n) pour un split cohérent
$s = $s -replace "`r`n","`n"
$s = $s -replace "`r","`n"
return $s
}
function Get-PgmName {
param($XmlNode, [string]$FallbackPath)
$pgm = ""
if ($XmlNode -and $XmlNode.ParentNode -and $XmlNode.ParentNode.Attributes) {
$a = $XmlNode.ParentNode.Attributes.GetNamedItem("Name")
if ($a) { $pgm = $a.Value }
}
if (-not $pgm -and $XmlNode -and $XmlNode.Attributes) {
$a = $XmlNode.Attributes.GetNamedItem("Name")
if ($a) { $pgm = $a.Value }
}
if (-not $pgm) { $pgm = [System.IO.Path]::GetFileNameWithoutExtension($FallbackPath) }
return $pgm
}
# ---------------------------
$rows = New-Object System.Collections.Generic.List[object]
$found = 0
$egpCnt = 0
Get-ChildItem -Path $Root -Recurse -Filter *.egp | ForEach-Object {
$egp = $_.FullName
$egpName = [System.IO.Path]::GetFileName($egp)
$egpCnt++
try {
$tmp = New-TempDir
Extract-Egp -EgpPath $egp -Dest $tmp.FullName
Get-ChildItem -Path $tmp.FullName -Recurse | ForEach-Object {
$p = $_.FullName
if ($_.Extension -ieq ".xml") {
try {
$xml = [xml](Get-Content -LiteralPath $p -Raw -ErrorAction Stop)
# Balises probables contenant du SAS (namespace-agnostic)
$nodes = $xml.SelectNodes("//*[local-name()='Code' or local-name()='Program' or local-name()='SourceCode' or local-name()='SasCode' or local-name()='Text']")
if ($nodes) {
foreach ($n in $nodes) {
# 1) CDATA via InnerText ; 2) fallback InnerXml -> strip tags
$code = $n.InnerText
if ([string]::IsNullOrWhiteSpace($code)) {
$raw = $n.InnerXml
if (-not [string]::IsNullOrWhiteSpace($raw)) {
$code = [System.Text.RegularExpressions.Regex]::Replace($raw, '<.+?>', '')
}
}
$code = Normalize $code
if ($null -eq $code) { continue }
$pgmName = Get-PgmName -XmlNode $n -FallbackPath $p
# ------ OPTION 2: une ligne CSV par ligne SAS ------
$lines = $code -split "`n"
foreach ($line in $lines) {
# On garde l'indentation; on retire seulement les fins d'espaces
$codeLine = if ($null -ne $line) { $line.TrimEnd() } else { "" }
$rows.Add([pscustomobject]@{
EGP = $egpName
PGM = $pgmName
Code = $codeLine
})
$found++
}
}
}
} catch {
Write-Verbose "XML non lisible: $p — $_"
}
}
elseif ($_.Extension -ieq ".sas") {
try {
$code = Get-Content -LiteralPath $p -Raw -ErrorAction Stop
$code = Normalize $code
$pgmName = [System.IO.Path]::GetFileNameWithoutExtension($p)
$lines = $code -split "`n"
foreach ($line in $lines) {
$codeLine = if ($null -ne $line) { $line.TrimEnd() } else { "" }
$rows.Add([pscustomobject]@{
EGP = $egpName
PGM = $pgmName
Code = $codeLine
})
$found++
}
} catch {
Write-Verbose "SAS non lisible: $p — $_"
}
}
}
} catch {
Write-Warning "Erreur sur $egp : $_"
} finally {
if ($tmp -and (Test-Path $tmp.FullName)) { Remove-Item $tmp.FullName -Recurse -Force }
}
}
if ($rows.Count -eq 0) {
Write-Warning "Aucune ligne de code extraite."
}
# Export CSV en 3 colonnes (EGP | PGM | Code), une ligne = une ligne SAS
$rows | Select EGP, PGM, Code | Export-Csv -NoTypeInformation -Encoding UTF8 -Delimiter $Delimiter -Path $Out
Write-Host "EGP analysés : $egpCnt | Lignes exportées : $found"
Write-Host "Sortie : $Out (colonnes EGP$Delimiter PGM$Delimiter Code — 1 ligne SAS par ligne CSV)"
J ai demandé à chatgpt et il me donne le code suivant mais je ne sais pas comment le faire fonctionner ..
# ================== CONFIG ==================
$Root = "C:\chemin\vers\vos\egp" # Dossier racine (parcours récursif)
$Out = "C:\chemin\sortie\egp_code_inventory.csv" # Fichier CSV de sortie
$Delimiter = ';' # ';' conseillé pour Excel FR (',' sinon)
# ============================================
# Assembly .NET zip (utile si Expand-Archive indisponible)
try { Add-Type -AssemblyName System.IO.Compression.FileSystem } catch {}
# -------- Helpers ----------
function New-TempDir {
$d = Join-Path $env:TEMP ([System.IO.Path]::GetRandomFileName())
New-Item -ItemType Directory -Force -Path $d | Out-Null
return (Get-Item $d)
}
function Extract-Egp {
param([string]$EgpPath, [string]$Dest)
try {
Expand-Archive -LiteralPath $EgpPath -DestinationPath $Dest -Force
} catch {
[System.IO.Compression.ZipFile]::ExtractToDirectory($EgpPath, $Dest)
}
}
function Normalize([string]$s) {
if ($null -eq $s) { return "" }
# Normalise les fins de ligne en LF (\n) pour un split cohérent
$s = $s -replace "`r`n","`n"
$s = $s -replace "`r","`n"
return $s
}
function Get-PgmName {
param($XmlNode, [string]$FallbackPath)
$pgm = ""
if ($XmlNode -and $XmlNode.ParentNode -and $XmlNode.ParentNode.Attributes) {
$a = $XmlNode.ParentNode.Attributes.GetNamedItem("Name")
if ($a) { $pgm = $a.Value }
}
if (-not $pgm -and $XmlNode -and $XmlNode.Attributes) {
$a = $XmlNode.Attributes.GetNamedItem("Name")
if ($a) { $pgm = $a.Value }
}
if (-not $pgm) { $pgm = [System.IO.Path]::GetFileNameWithoutExtension($FallbackPath) }
return $pgm
}
# ---------------------------
$rows = New-Object System.Collections.Generic.List[object]
$found = 0
$egpCnt = 0
Get-ChildItem -Path $Root -Recurse -Filter *.egp | ForEach-Object {
$egp = $_.FullName
$egpName = [System.IO.Path]::GetFileName($egp)
$egpCnt++
try {
$tmp = New-TempDir
Extract-Egp -EgpPath $egp -Dest $tmp.FullName
Get-ChildItem -Path $tmp.FullName -Recurse | ForEach-Object {
$p = $_.FullName
if ($_.Extension -ieq ".xml") {
try {
$xml = [xml](Get-Content -LiteralPath $p -Raw -ErrorAction Stop)
# Balises probables contenant du SAS (namespace-agnostic)
$nodes = $xml.SelectNodes("//*[local-name()='Code' or local-name()='Program' or local-name()='SourceCode' or local-name()='SasCode' or local-name()='Text']")
if ($nodes) {
foreach ($n in $nodes) {
# 1) CDATA via InnerText ; 2) fallback InnerXml -> strip tags
$code = $n.InnerText
if ([string]::IsNullOrWhiteSpace($code)) {
$raw = $n.InnerXml
if (-not [string]::IsNullOrWhiteSpace($raw)) {
$code = [System.Text.RegularExpressions.Regex]::Replace($raw, '<.+?>', '')
}
}
$code = Normalize $code
if ($null -eq $code) { continue }
$pgmName = Get-PgmName -XmlNode $n -FallbackPath $p
# ------ OPTION 2: une ligne CSV par ligne SAS ------
$lines = $code -split "`n"
foreach ($line in $lines) {
# On garde l'indentation; on retire seulement les fins d'espaces
$codeLine = if ($null -ne $line) { $line.TrimEnd() } else { "" }
$rows.Add([pscustomobject]@{
EGP = $egpName
PGM = $pgmName
Code = $codeLine
})
$found++
}
}
}
} catch {
Write-Verbose "XML non lisible: $p — $_"
}
}
elseif ($_.Extension -ieq ".sas") {
try {
$code = Get-Content -LiteralPath $p -Raw -ErrorAction Stop
$code = Normalize $code
$pgmName = [System.IO.Path]::GetFileNameWithoutExtension($p)
$lines = $code -split "`n"
foreach ($line in $lines) {
$codeLine = if ($null -ne $line) { $line.TrimEnd() } else { "" }
$rows.Add([pscustomobject]@{
EGP = $egpName
PGM = $pgmName
Code = $codeLine
})
$found++
}
} catch {
Write-Verbose "SAS non lisible: $p — $_"
}
}
}
} catch {
Write-Warning "Erreur sur $egp : $_"
} finally {
if ($tmp -and (Test-Path $tmp.FullName)) { Remove-Item $tmp.FullName -Recurse -Force }
}
}
if ($rows.Count -eq 0) {
Write-Warning "Aucune ligne de code extraite."
}
# Export CSV en 3 colonnes (EGP | PGM | Code), une ligne = une ligne SAS
$rows | Select EGP, PGM, Code | Export-Csv -NoTypeInformation -Encoding UTF8 -Delimiter $Delimiter -Path $Out
Write-Host "EGP analysés : $egpCnt | Lignes exportées : $found"
Write-Host "Sortie : $Out (colonnes EGP$Delimiter PGM$Delimiter Code — 1 ligne SAS par ligne CSV)"
Dernière édition: