有一个文件夹,里面有超过1000个压缩文件,每个压缩文件包含12个其他压缩文件,每个文件包含一个CSV文件。每个压缩文件包含12个其他压缩文件,每个文件包含一个CSV文件。我需要计算所有文件的总行数。
可以用windows powershell来完成,但是为了节省磁盘空间,解压文件,计算行数,然后再压缩,我遇到了麻烦。
$folderPath="C:\_Unzip_Folder";
Get-ChildItem $folderPath -recurse | %{
if($_.Name -match "^*.`.zip$")
{
$parent="$(Split-Path $_.FullName -Parent)";
write-host "Extracting $($_.FullName) to $parent"
$arguments=@("e", "`"$($_.FullName)`"", "-o`"$($parent)`"");
$ex = start-process -FilePath "`"C:\Program Files\7-Zip\7z.exe`"" -ArgumentList $arguments -wait -PassThru;
if( $ex.ExitCode -eq 0)
{
write-host "Extraction successful, deleting $($_.FullName)"
rmdir -Path $_.FullName -Force
}
}
}
Get-ChildItem $folderPath -recurse -Filter *.csv | %{
Get-Content $($_.FullName) | Measure-Object -Line
}
cmd /c pause | out-null
现在,它是计算行数,但是,它可以更容易,如果它SUM他们给我。
有人能帮助我完成这个任务吗?
谢谢大家。
你也可以把所有的文件都保存在内存中,像这样。
Set-StrictMode -Version "Latest"
$ErrorActionPreference = "Stop"
$InformationPreference = "Continue"
Add-Type -Assembly "System.IO.Compression.FileSystem"
$folderPath = "C:\_Unzip_Folder\*.zip"
$files = Get-ChildItem $folderPath -Recurse
$csvCount = 0
$lineCount = 0
$bufferSize = 1MB
$buffer = [byte[]]::new($bufferSize)
foreach ($file in $files)
{
Write-Information "Getting information from '$($file.FullName)'"
$zip = [System.IO.Compression.ZipFile]::OpenRead($file.FullName)
$csvs = $zip.Entries | Where-Object { [System.IO.Path]::GetExtension($_.Name) -eq ".csv" }
foreach ($csv in $csvs)
{
$csvCount++
Write-Information "Counting lines in '$($csv.FullName)'"
$stream = $csv.Open()
try
{
$byteCount = $stream.Read($buffer, 0, $bufferSize)
while ($byteCount)
{
for ($i = 0; $i -lt $byteCount; $i++)
{
# assume line feed (LF = 10) is the end-of-line marker
# you could also use carriage return (CR = 13)
if ($buffer[$i] -eq 10) { $lineCount++ }
}
$byteCount = $stream.Read($buffer, 0, $bufferSize)
}
}
finally
{
$stream.Close()
}
}
}
Write-Information "Counted a total of $lineCount line(s) in $csvCount CSV-file(s)"