Reputation: 75
Hoping someone can help me finish off this script. I have a folder full of *.imp (plain text - csv exports) files that I need to modify. These files are updated on a schedule, so this Powershell script will be updated on a schedule too, but I can't seem to get it to work.
Here is where I've gotten so far...
$InputFiles = Get-Item "C:\scripts\*.imp"
$OldString = '@##'
$NewString = '"'
$InputFiles | ForEach {
(Get-Content -Path $_.FullName).Replace($OldString,$NewString) | Set-Content -Path $_.FullName
(Get-Content -Path $_.FullName).-notmatch '(^[\s,-]*$)|(rows\s*affected)' | Set-Content -Path $_.FullName
(Get-Content -Path $_.FullName).-notmatch 'HeaderRow' | Set-Content -Path $_.FullName
}
If I leave the first Get-Content line, it works, but adding the other two don't. Can anyway help me with this? Thanks Luke
Upvotes: 2
Views: 411
Reputation: 174435
As Lieven points out you need to remove the .
reference operator in the second and third statement:
$InputFiles = Get-Item "C:\scripts\*.imp"
$OldString = '@##'
$NewString = '"'
$InputFiles | ForEach {
(Get-Content -Path $_.FullName).Replace($OldString,$NewString) | Set-Content -Path $_.FullName
(Get-Content -Path $_.FullName) -notmatch '(^[\s,-]*$)|(rows\s*affected)' | Set-Content -Path $_.FullName
(Get-Content -Path $_.FullName) -notmatch 'HeaderRow' | Set-Content -Path $_.FullName
}
Instead of reading and writing the file from disk three times, you could chain all the operations:
$InputFiles = Get-Item "C:\scripts\*.imp"
$OldString = '@##'
$NewString = '"'
$InputFiles | ForEach {
(Get-Content -Path $_.FullName).Replace($OldString,$NewString) -notmatch '(^[\s,-]*$)|(rows\s*affected)' -notmatch 'HeaderRow' | Set-Content -Path $_.FullName
}
Upvotes: 2