2

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

1
  • 1
    Remove the dot from .-notmatch (replace with space) Commented Mar 2, 2018 at 6:17

1 Answer 1

2

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Lieven and Mathias. I used the tidier version you supplied Mathias, and it works wonders. Thank you for your gracious help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.