向 powershell 脚本添加进度指示器 - 拆分 CSV

问题描述 投票:0回答:1

我需要将巨大的 CSV 文件分割成更小的部分。 我想检查 PowerShell 脚本是否能够做到这一点。 我在网络上找到了一个脚本(https://www.spjeff.com/2017/06/02/powershell-split-csv-in-1000-line-batches/),但想添加进度指示器以确保它正在运行。它运行了几个小时,但没有生成输出文件。

请让我知道,我可以在脚本中的哪个位置添加进度指示器的逻辑,以读取文件的内容和分割进度。

powershell csv split
1个回答
0
投票

对于拆分

Csv
文件,我建议使用可步进管道(有关完整说明,请参阅:掌握(可步进)管道),这也可以让您轻松地集成
Write-Progress
栏。

请注意,

Write-Progress
cmdlet 本身就相当昂贵 (尤其是在 Windows PowerShell 5.1 中)。

Import-Csv .\Your.csv | Foreach-Object -Begin { $ExpectedBatches = 1000 $Index = 1 $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() }
    
© www.soinside.com 2019 - 2024. All rights reserved.