Foreach File in Files extract last line in file

Hello Partners,

Need to go through a list of files and save the last line of each file in another file

 all my attempts failed

TRY 1

[pre] foreach ($file in Get-ChildItem "C:\Users\Marcos\Documents\Adobe" -Tail 1 ) { $file+ $file.fullname }

[/pre]

TRY 2

[pre]

foreach ($file in Get-ChildItem "C:\Users\Marcos\Documents\Adobe") {
if ($arquivo -eq true) {
Get-Content -Tail 1
$file + $file.fullname }

[/pre]

Almost … :wink: … try this:

$ListOfLastLines = Get-ChildItem -Path 'C:\Users\Marcos\Documents\Adobe'  -File |
ForEach-Object {
Get-Content -Path $_.FullName -Tail 1
}
$ListOfLastLines | Out-File -FilePath ‘C:\Users\Marcos\Documents\Result.txt’

bro, its , very well, thanks so much.

I’ve been trying to learn how to work with powershell but I’ve had a lot of trouble

Thank you for your help

more one simply question ,

 

How can I add the filename before the last line?

I tried concatenating the variable + $ ListOfLastLines.FullName, but I returned some errors.

The proper way would be to create a custom object and export it as CSV file … like this:

$ListOfLastLines = Get-ChildItem -Path 'C:\Users\Marcos\Documents\Adobe' -File |
ForEach-Object {
[PSCustomObject]@{
FileName = $.FullName
LastLine = Get-Content -Path $
.FullName -Tail 1
}
}
$ListOfLastLines | Export-Csv -Path ‘C:\Users\Marcos\Documents\Result.csv’ -NoTypeInformation