Reputation: 2696
I am grabbing the last line of a file and outputing it in another file with the same name with a "t" prefix but an errors comes up.
$path = 'D:\files\'
$inc = @("*txt_*")
$exc = @("*csv_*")
$List = Get-ChildItem -Path $path -recurse -include $inc -exclude $exc
foreach ($item in $List) {
Get-Content $item.Fullname -Tail 1 | Out-File -Encoding Ascii $path"t-"$item
}
However, if I dont prepend the file name it works fine.
Get-Content $item.Fullname -Tail 1 | Out-File -Encoding Ascii $path"t-"
What is wrong with this?
Upvotes: 0
Views: 609
Reputation: 200463
Prepend the filename with T-
and build the destination path from the folder name and the modified filename:
Get-ChildItem -Path $path -Recurse -Include $inc -Exclude $exc | ForEach-Object {
$dst = Join-Path $_.Directory.FullName ($_.Name -replace '^', 'T-')
Get-Content $_.FullName -Tail 1 | Set-Content $dst -Encoding ascii
}
Upvotes: 2