Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何替换文本文件中的多个字符串并同时生成日志文件?_Powershell - Fatal编程技术网

Powershell 如何替换文本文件中的多个字符串并同时生成日志文件?

Powershell 如何替换文本文件中的多个字符串并同时生成日志文件?,powershell,Powershell,文件夹中有多个文本文件,每个文本文件中都有多个受影响的ID。我需要用新的ID替换它们。另外,我想生成一个文本日志文件,列出文件名oldID和newID。交叉检查和验证需要此日志文件。 我有一个csv文件,我正在从中创建一个数组IdList。我在下面列出了替换字符串的代码部分 foreach ($f in gci -r -include "*.txt") { Set-Variable -Name filename -value ($f.name) for( $i=0; $i -le $elem

文件夹中有多个文本文件,每个文本文件中都有多个受影响的ID。我需要用新的ID替换它们。另外,我想生成一个文本日志文件,列出文件名oldID和newID。交叉检查和验证需要此日志文件。 我有一个csv文件,我正在从中创建一个数组IdList。我在下面列出了替换字符串的代码部分

foreach ($f in gci -r -include "*.txt")
{
Set-Variable -Name filename -value ($f.name)


for( $i=0; $i -le $elementCount; $i++)
 {
Write-Host $i
$oldString= $IdList[$i].OLD_ID+','+$IdList[$i].OLD_ID_TYPE
$newString= $IdList[$i].NEW_ID+','+$IdList[$i].NEW_ID_TYPE

 gc $f.fullname|ForEach-Object {if($_.contains($oldString)){ "$filename,$oldString,$newString" >> $logFile; $_ -replace $oldString,$newString}}| sc $f.fullname

 } 

}
我得到一个错误:

Set-Content : The process cannot access the file 'U:\testScript\rapg6785.txt' because it is being used by another process.
At line:22 char:152
+      gc $f.fullname|ForEach-Object {if($_.contains($oldString)){ "$filename,$oldString,$newString" >> $logFile; $_ -replace $oldString,$newString}}| sc <<<<  $f.fullname
+ CategoryInfo          : NotSpecified: (:) [Set-Content], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.SetContentCommand
Set Content:进程无法访问文件“U:\testScript\rapg6785.txt”,因为另一个进程正在使用该文件。
第22行字符:152

+gc$f.fullname | ForEach对象{if($\.contains($oldString)){“$filename,$oldString,$newString”>$logFile;$\-替换$oldString,$newString}}}}sc尝试将gc
$f.fullname
放在括号中:
(gc$f.fullname)


这样,管道在
gc
结束时开始读取文件并释放文件句柄供另一个进程使用。

尝试将
gc$f.fullname
放在括号中:
(gc$f.fullname)
谢谢。它正在使用(gc$f.fullname)。你能告诉我有括号和没有括号有什么区别吗