我正在嘗試撰寫一個腳本來將檔案夾中的多個 txt 檔案組合到一個輸出檔案中,但我需要洗掉標題行。我嘗試了以下方法:
$outFile = new-item -path 'combined\combined.txt' -itemtype file -force
$inFiles = @(Get-ChildItem -Filter *.txt)
Get-Content $inFiles[0] -First 1 | Set-Content -Encoding Utf8 $outFile
Get-ChildItem -Recurse -Include "*.txt" | get-content | Select -Skip 1 |
Out-File -Append -encoding ASCII $outFile
我也嘗試過回圈,但會遇到有關正在使用的檔案或 powershell 腳本只是停止而不是合并所有檔案的錯誤
foreach ($file in $inFiles) {
$from = Get-Content -path $file |
Select-Object -Skip 1
Add-Content $outFile -value $from }
任何幫助將不勝感激!
uj5u.com熱心網友回復:
我認為你過于復雜了......這應該足夠了:
Get-ChildItem -Filter *.txt |
Select-Object -First 1 |
ForEach-Object {
Get-Content -Path $_.FullName |
Select-Object -First 1
} |
Out-File -FilePath 'combined\combined.txt'
Get-ChildItem -Filter *.txt |
ForEach-Object {
Get-Content -Path $_.FullName |
Select-Object -Skip 1
} |
Out-File -FilePath 'combined\combined.txt' -Append
第一個片段創建輸出檔案,僅包含從第一個輸入檔案中獲取的標題。第二個代碼段從所有輸入檔案中獲取除第一行之外的所有行。
你確定它是關于 txt 檔案的嗎?如果是關于具有相同標題的 CSV 檔案,那就更容易了。;-)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/381808.html
標籤:电源外壳
