2016-08-29 1 views
-1

Wie jeder weiß, Excel hat maximale Zeilen 1048574. Meine Frage ist: Wenn die maximale Zeile erreicht ist, dann verschieben Sie eine neue CSV-Datei Ich habe etwas versucht, kein Glück, ich kann es nicht fassen aus.Splitting Export großer CSV-Dateien über Powershell

$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 
    } 

} 

Letzte Aktualisierung:

$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) 
} 


    } 

} 
+0

@pnuts Ich habe meine Frage – Arbelac

+0

bearbeiten ich CSVs Dateien über Excel Anwendung aufgrund geschäftlicher Anforderungen öffnen, weil der, dass ich CSV-Datei über Powershell – Arbelac

Antwort

0

ConvertTo-Csv in einer temporären Variablen tun, addieren sich ihre .Count Eigentum, bis der Split-Punkt erreicht ist, spaltete dann die CSV-Array in eine neue Datei mit einem neuen Namen, wird von diesem Zeitpunkt an verwendet.

Unten ist ein vereinfachtes Beispiel, das davon ausgeht, dass keine einzelne CSV-Datei 1000000 Zeilen überschreiten darf.

$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) 
} 
+0

Zunächst einmal vielen Dank Ich verstehe teilen will nicht, dass Wie kann ich genau deinen Code anpassen? zB $ thisCSV = Select-Objekt ........ was muss ich hier machen? Also muss ich es nach dem Export-Csv $ csv -Append -NoType Befehl am Ende des Codes verwenden? – Arbelac

+0

AFAIK $ thisCSV = Get-Acl $ Pfad | Select-Object -Erweiterter Zugriff | Auswahlobjekt @ {n = 'Pfad'; e = {$ Pfad}}, IdentityReference, AccessControlType, FileSystemRights | ConvertTo-Csv Bin ich richtig? – Arbelac

+0

Ja, Sie haben Recht. Ich habe den Anfang dieser Zeile übersehen, Entschuldigung für die Verwirrung. – wOxxOm