且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 PowerShell 将多个 CSV 文件合并为一个

更新时间:2023-11-23 11:53:34

这会将所有文件附加在一起,一次读取一个:

This will append all the files together reading them one at a time:

get-childItem "YOUR_DIRECTORY*.txt" 
| foreach {[System.IO.File]::AppendAllText
 ("YOUR_DESTINATION_FILE", [System.IO.File]::ReadAllText($_.FullName))}

# Placed on seperate lines for readability

如果您需要,这将在每个文件条目的末尾放置一个新行:

This one will place a new line at the end of each file entry if you need it:

get-childItem "YOUR_DIRECTORY*.txt" | foreach
{[System.IO.File]::AppendAllText("YOUR_DESTINATION_FILE", 
[System.IO.File]::ReadAllText($_.FullName) + [System.Environment]::NewLine)}

跳过第一行:

$getFirstLine = $true

get-childItem "YOUR_DIRECTORY*.txt" | foreach {
    $filePath = $_

    $lines =  $lines = Get-Content $filePath  
    $linesToWrite = switch($getFirstLine) {
           $true  {$lines}
           $false {$lines | Select -Skip 1}

    }

    $getFirstLine = $false
    Add-Content "YOUR_DESTINATION_FILE" $linesToWrite
    }