本帖最后由 flashercs 于 2022-8-1 11:37 编辑
回复 33# 领航 | | | | | | | | | | | | | | | | | $dictxtpath = @{ | | "c:\txt\a.txt" = "c:\news\1001" | | "c:\txt\b.txt" = "c:\news\1002" | | "c:\txt\c.txt" = "c:\news\1003" | | } | | function Import-WgcCsv { | | | | [CmdletBinding(DefaultParameterSetName = 'Delimiter')] | | param ( | | [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] | | [Alias('PSPath')] | | [string[]]$LiteralPath, | | [ValidateSet('ASCII', 'BigEndianUnicode', 'Default', 'OEM', 'Unicode', 'UTF7', 'UTF8', 'UTF32')] | | [string]$Encoding = 'ASCII', | | [Parameter(ParameterSetName = 'Delimiter')] | | [char]$Delimiter = ',', | | [Parameter(ParameterSetName = 'UseCulture')] | | [switch]$UseCulture, | | [Parameter()] | | [ValidateNotNullOrEmpty()] | | [string[]]$Header | | ) | | process { | | foreach ($lPath in $LiteralPath) { | | trap {} | | try { | | $txt = (Get-Content -ReadCount 0 -LiteralPath $lPath -Encoding $Encoding) -join ([Environment]::NewLine) | | if ($PSCmdlet.ParameterSetName -eq 'Delimiter') { | | if ($null -ne $Header -and $Header.Count -gt 0) { | | ConvertFrom-Csv -InputObject $txt -Delimiter $Delimiter -Header $Header | | } else { | | ConvertFrom-Csv -InputObject $txt -Delimiter $Delimiter | | } | | } else { | | if ($null -ne $Header -and $Header.Count -gt 0) { | | ConvertFrom-Csv -InputObject $txt -UseCulture:$UseCulture -Header $Header | | } else { | | ConvertFrom-Csv -InputObject $txt -UseCulture:$UseCulture | | } | | } | | } finally { | | | | } | | } | | } | | } | | $sortlist = New-Object 'System.Collections.Generic.SortedList[int,float]' | | foreach ($txtpath in $dictxtpath.Keys) { | | $txtpath | | $dirdata = $dictxtpath[$txtpath] | | $rootdir = [System.IO.Directory]::CreateDirectory($dirdata) | | | | Import-WgcCsv -LiteralPath $txtpath -Encoding UTF8 -Delimiter '|' -Header a, b, c, d | Group-Object -Property a, b | ForEach-Object { | | $datapath = [System.IO.Path]::Combine($rootdir.FullName, "$($_.Values -join '_').dat") | | $sortlist.Clear() | | if (Test-Path -LiteralPath $datapath) { | | | | try { | | $stream = New-Object System.IO.FileStream -ArgumentList @($datapath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read) | | $br = New-Object System.IO.BinaryReader -ArgumentList $stream | | while ($true) { | | $key = $br.ReadInt32() | | $value = $br.ReadSingle() | | $sortlist[$key] = $value | | } | | } catch {} | | finally { | | if ($br) { | | $br.Close() | | $br = $null | | } | | if ($stream) { | | $stream.Close() | | $stream = $null | | } | | } | | } | | | | foreach ($pso in $_.Group) { | | $sortlist[($pso.c -as [int])] = $pso.d -as [float] | | } | | | | try { | | $stream = New-Object System.IO.FileStream -ArgumentList @($datapath, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write, [System.IO.FileShare]::Read) | | $bw = New-Object System.IO.BinaryWriter -ArgumentList $stream | | foreach ($key in $sortlist.Keys) { | | $bw.Write($key) | | $bw.Write($sortlist[$key]) | | } | | } finally { | | if ($bw) { | | $bw.Close() | | $bw = $null | | } | | if ($stream) { | | $stream.Close() | | $stream = $null | | } | | } | | trap {} | | } | | }COPY |
|