Powershell - 多个文件的多个命令
Powershell - multiple commands to multiple files
希望有人能帮我完成这个脚本。我有一个文件夹,里面装满了我需要修改的 *.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 行,它可以工作,但添加其他两个则不行。
无论如何可以帮助我吗?
谢谢
卢克
正如 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
}
希望有人能帮我完成这个脚本。我有一个文件夹,里面装满了我需要修改的 *.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 行,它可以工作,但添加其他两个则不行。 无论如何可以帮助我吗? 谢谢 卢克
正如 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
}