## Q:\Test17\SU_1244148.ps1
Set-Location "C:\Data\Files\"
$Files = Get-ChildItem File*.txt
$Final = ForEach ($File in $Files){
$Content = Get-Content $File
$Prefix = $Content[0].Substring(1,3)
For ($i = 1;$i -lt $Content.Length-1;$i++){
"{0}{1}" -f $Prefix,$Content[$i]
}
}
$Final | Set-Content Final.txt
> gc .\Final.txt
bc1data1
bc1data2
fg4data3
fg4data4
ij7data5
ij7data6
Versão 2 lida com arquivos grandes
## Q:\Test17\SU_1244148_2.ps1
#Set-Location "C:\Data\Files\"
$Files = (Get-ChildItem File*.txt|Sort)
$Final = '.\Final.txt'
## As we append to $Final initially clear
If (Test-Path $Final){Remove-Item $Final}
ForEach ($File in $Files){
$Reader = [IO.File]::OpenText($File)
$Header = $Reader.ReadLine()
$Prefix = $Header.SubString(1,3)
$Line = $Reader.ReadLine()
while ($Reader.Peek() -ge 0) {
$Prefix+$Line| Out-File $Final -Append
$Line = $Reader.ReadLine()
}
$Reader.Dispose()
}
Código otimizado para não ter que testar o cabeçalho à direita.
O loop while imprime a linha anterior e lê a próxima dessa maneira, soltando a última linha.