添加进度指示器到powershell脚本-分割CSV

ecbunoof  于 5个月前  发布在  Shell
关注(0)|答案(1)|浏览(59)

我需要分割成小块巨大的CSV文件.我想检查PowerShell脚本是否能够做到这一点.我已经在网络上找到了一个脚本(https://www.spjeff.com/2017/06/02/powershell-split-csv-in-1000-line-batches/),但想添加进度指示器,以确保它正在运行.它运行了几个小时,但没有生成输出文件.
请让我知道,在脚本中,我可以添加一个逻辑进度指示器的阅读文件的内容和分割的进展。

eni9jsuy

eni9jsuy1#

对于拆分Csv文件,我建议使用可步进管道(完整的解释,请参阅:掌握(可步进)管道),这也允许您轻松地整合Write-Progress条。
注意the Write-Progress cmdlet is pretty expensive本身
(特别是在Windows PowerShell 5.1中)。

Import-Csv .\Your.csv | Foreach-Object -Begin {
    $ExpectedBatches = 1000
    $Index = 0
    $BatchSize = 1000
} -Process {
    if ($Index % $BatchSize -eq 0) {
        $BatchNr = [math]::Floor($Index++/$BatchSize)
        Write-Progress -Activity "Processing" -Status "Batch number: $BatchNr" -PercentComplete ($BatchNr * 100 / $ExpectedBatches)
        $Pipeline = { Export-Csv -notype -Path .\Batch_$BatchNr.csv }.GetSteppablePipeline()
        $Pipeline.Begin($True)
    }
    $Pipeline.Process($_)
    if ($Index++ % $BatchSize -eq 0) { $Pipeline.End() }
} -End {
    $Pipeline.End()
}

字符串

相关问题