vendredi 27 mai 2016

powershell replace data in files sub directory (ok) only if line begins with some specific string (ko)

it s ok to replace a string in all files sub directory with this :

initial in line : 02#APPLICATION/PDF;name="P31F9B000A00001.pdf"* replace by : # P31F9B000A00001 can be anything

with this pattern :

$array = @("D:\cebi")
$myRegExp1 = [regex]"#APPLICATION/PDF;name=""(.*).pdf""\*"
$myRegToReplace1 = "#"
$myRegExp2 = [regex]"\*ClasseAutomatique\*2016\*"
$myRegToReplace2 = "*ClasseAutomatique*2016*Faux*"
foreach ($element in $array) {
    Get-ChildItem $element  -Recurse -Include "*.DAT" | foreach-object {Copy-Item $_.fullname ($_.fullname+".bak") -recurse } 
    gci $element -r -include "*.DAT"| foreach-object { $a = $_.fullname; ( get-content $a ) |  foreach-object { $_ -replace $myRegExp1,$myRegToReplace1 -replace $myRegExp2,$myRegToReplace2  }  | set-content $a } 

}

But now, i would like do replace a string only for the lines begins by a specific string in bold, it can be anything

i want to replace string "ClasseAutomatique" by "ok_man" only if line begins by "02#APPLICATION/PDF;name=" intial string :

02#APPLICATION.PDF;name="P31F9B000A00001.pdf"-anything_data.2016.##_ClasseAutomatique_2016__##P31F9B000A00001****.pdf 01#APPLICATION.PDF;name="**P31F9B123D02651.pdf"-anything_data.2016.##_ClasseAutomatique_2016__##**P31CKB000D00002****.pdf

Result would be :

02#APPLICATION.PDF;name="P31F9B000A00001.pdf"-anything_data.2016.##_ok_man_2016__##P31F9B000A00001.pdf 01#APPLICATION.PDF;name="P31F9B123D02651.pdf"-anything_data.2016.##_ClasseAutomatique_2016__##P31CKB000D00002.pdf

i try this :

$myRegExp3 = [regex]"^(02#APPLICATION/PDF;name=(.).pdf)-(.).2016.##_(ClasseAutomatique)_2016__##(.*).pdf"

$myRegToReplace3 = "$1-$2.2016.##_ok_man_2016__##$4.pdf"

but nothing

Aucun commentaire:

Enregistrer un commentaire