Author Posts

January 1, 2012 at 12:00 am

by ChrisL at 2012-10-17 10:26:02

This seems like such a simple issue but I haven't been able to find anything on it. I'm using Get-ChildItem to recursively search through folder sets for particular text files, modify them, and then save the file. The problem I've been running into is that for files that are located in subfolders, instead of overwriting them, it saves them in the root folder instead. Is there a way to retreive the full path using GCI or is there a better way to do this?


ForEach ($sFolder in $aSearchFolders) {
If (Test-Path "$sFolder") {
Set-Location $sFolder
Get-ChildItem -Path $sFolder -Filter name*.xxx -Recurse | ForEach-Object { (Get-Content $_) | ForEach-Object {$_ -replace "text1", "text2"} | Set-Content $_ }
}
}

by DonJ at 2012-10-17 10:36:18

The File object does contain the full path. The problem is that you're using $_, which is the whole file object, so it's defaulting to the Name property. You probably want to use something like $_.FullName instead.

by ChrisL at 2012-10-17 11:53:32

That fixed it; thank you so much!!