拆分通过Powershell导出大型CSV文件

时间:2016-08-29 18:17:47

标签: excel windows powershell csv filesystems

像所有人都知道excel有最大行1048574.我的问题是:当达到最大行时,然后移动到新的CSV文件我尝试了一些没有运气的东西我无法弄明白。

$RootFolder = Get-Content "c:\DRIVERS\myfile.txt"

foreach ($arrayOfPaths in $RootFolder){

  $csv  = $arrayofPaths -replace '^\\\\[^\\]+\\([^\\]+)\\([^\\]+).*', 'C:\output\Company_name_${1}_${2}.csv'
  Get-ChildItem $arrayOfPaths -Recurse | Where-Object {$_.mode -match "d"} | ForEach-Object {
$path = $_.FullName
  Get-Acl $path | Select-Object -Expand Access |
    Select-Object @{n='Path';e={$path}}, IdentityReference, AccessControlType,
                  FileSystemRights |
    Export-Csv $csv -Append -NoType
  }

}

最后更新:

$RootFolder = Get-Content "c:\DRIVERS\myfile.txt"

foreach ($arrayOfPaths in $RootFolder){

  $csv  = $arrayofPaths -replace '^\\\\[^\\]+\\([^\\]+)\\([^\\]+).*', 'C:\output\Company_name_${1}_${2}.csv'
  $csvIndex = 1
  $maxRows = 1000000
  $rowsLeft = $maxRows

  Get-ChildItem $arrayOfPaths -Recurse | Where-Object {$_.mode -match "d"} | ForEach-Object {
  #$csv  = $_.FullName -replace '^\\\\[^\\]+\\([^\\]+)\\([^\\]+).*', 'C:\output\Company_name_${1}_${2}.csv'# <- construct CSV path here
  $path = $_.FullName
  $thisCSV = Get-Acl $path | Select-Object -Expand Access |
    Select-Object @{n='Path';e={$path}}, IdentityReference, AccessControlType,
                  FileSystemRights |
    ConvertTo-Csv
if ($thisCSV.count -lt $rowsLeft) {
    $thisCSV | Export-Csv $csv -append -noType
    $rowsLeft -= $thisCSV.count
} else {
    $thisCSV[0..($rowsLeft - 1)] | Export-Csv $csv -append -noType
    $csvIndex++
    $csv = $csv -replace '\.csv$', "$csvIndex.csv"
    if ($thisCSV.count -gt $rowsLeft) {
        $thisCSV[$rowsLeft..($thisCSV.count - 1)] | Export-Csv $csv -append -noType
    }
    $rowsLeft = $maxRows - ($thisCSV.count - $rowsLeft)
}


  }

}

1 个答案:

答案 0 :(得分:0)

在临时变量中执行ConvertTo-Csv,将其.Count属性相加,直到达到分割点,然后将CSV数组拆分为新文件,该文件将使用从此处开始使用的新名称上。

以下是一个简化示例,假设没有一个CSV可以超过1000000行。

$csv = .............
$csvIndex = 1
$maxRows = 1000000
$rowsLeft = $maxRows

...........
$thisCSV = Select-Object ........ |
           ConvertTo-Csv
if ($thisCSV.count -lt $rowsLeft) {
    $thisCSV | Export-Csv $csv -append -noType
    $rowsLeft -= $thisCSV.count
} else {
    $thisCSV[0..($rowsLeft - 1)] | Export-Csv $csv -append -noType
    $csvIndex++
    $csv = $csv -replace '\.csv$', "$csvIndex.csv"
    if ($thisCSV.count -gt $rowsLeft) {
        $thisCSV[$rowsLeft..($thisCSV.count - 1)] | Export-Csv $csv -append -noType
    }
    $rowsLeft = $maxRows - ($thisCSV.count - $rowsLeft)
}