2016-07-22 42 views
0

我目前正试图从多个文件中删除部分匹配的完整字符串。从部分字符串匹配中删除完整字符串并且不会创建新文件

例如,我有一个页眉和页脚,其中的字符顺序是0*5。我想完全删除特定文件夹中的文件中的页眉和页脚,而不更改文件名。我已经找到并尝试切断Get-Content myFile.txtSet-Content newFile.txt

我的文件位于一个文件夹中,所以我从“*”打电话给他们。他们都有不同的名字。我正试图通过PowerShell一次性删除所有的页眉和页脚。

回答

0

我不是失去了原件的粉丝,所以我用了很长的方法:

$ListofFiles = Get-ChildItem c:\windows\logs\* -include *.txt 

ForEach ($item in $ListOfFiles){ 
    $FileContent = Get-Content "$item.txt" 
    Rename-Item -path "c:\windows\logs\$item.txt" -newname "$item.old" 
    ForEach ($line in $FileContent) { 
     If ($Line -like "*whatever i am looking for*") { 
      Write-host "Found the entry i was looking for" 
     } 
     else { 
      $Line | Out-File "c:\windows\logs\$item.txt" 
     } 
    } 
} 
+0

你在你的'$ item'字符串中的错误 - 单引号字符串不插变量,所以你总是读写'$ item.txt'作为文字文件名,而不是循环变量content.txt – TessellatingHeckler

+0

感谢您指出这一点......当我粘贴它们时,所有双引号.... –