[英]Powershell to delete lines in a txt file within a folder
好的。 我已經在測試 ChatGPT 以幫助我創建一個 PowerShell 腳本來刪除文件夾中少於 48 個字符的 txt 文件中的行,以及 append 文件名末尾的 A:例如,sys20221224.txt - > sys20221224A.txt。
我知道我很接近。 這是我到目前為止所擁有的:
Get-ChildItem -Path "C:\temp\powershellTest" -Filter "*.txt" | ForEach-Object {
$file = $_.FullName
$newFile = "$($file.substring(0,$file.lastindexof('.')))A.txt"
Get-Content -Path $file | Where-Object {$_.length -ge 48} | Out-File -FilePath $newFile
}
它有點工作,但 50MB 的文件現在是 100MB。 少了 20k 行,但文件更大。
$list = Get-ChildItem -Path "C:\temp\powershellTest" -Filter "*.txt" Set-Location "C:\temp\powershellTest"
ForEach ($l in $list) {
if ($l.length -lt 48){
Clear-Content $l -Verbose
Rename-Item $l ((($l.Name)).ToString() + "A")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.