Powershell - 多个文件的多个命令

问题描述 投票:2回答:1

希望有人可以帮我完成这个脚本。我有一个文件夹,里面有我需要修改的* .imp(纯文本 - csv导出)文件。这些文件按计划更新,因此这个Powershell脚本也会按计划更新,但我似乎无法让它工作。

这是我到目前为止的地方......

$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
}

如果我离开第一个Get-Content行,它可以工作,但添加其他两个不行。无论如何可以帮助我吗?谢谢卢克

powershell csv powershell-v2.0
1个回答
2
投票

正如Lieven所指出的,您需要在第二和第三个语句中删除.引用运算符:

$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
}

您可以链接所有操作,而不是从磁盘读取和写入文件三次:

$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
}
© www.soinside.com 2019 - 2024. All rights reserved.