返回列表 发帖
本帖最后由 flashercs 于 2022-7-31 18:19 编辑
<#*,:&cls
@echo off
cd /d "%~dp0"
powershell -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~f0\" -ReadCount 0 | Out-String)))"
pause
exit /b
#>
$txtpath = "c:\txt\a.txt"
$dirdata = "c:\news\1001"
$dicDataPath = @{}
$rootdir = [System.IO.Directory]::CreateDirectory($dirdata)
foreach ($line in (Get-Content -LiteralPath $txtpath -ReadCount 0)) {
  if ($line -match '^\s*$') {
    continue
  }
  $a, $b, $c, $d = $line -split '\|'
  $datapath = [System.IO.Path]::Combine($rootdir.FullName, "${a}_${b}.dat")
  if (-not $dicDataPath.ContainsKey($datapath)) {
    $dicDataPath[$datapath] = 1
    $filemode = [System.IO.FileMode]::Create
  } else {
    $filemode = [System.IO.FileMode]::Append
  }
  try {
    $stream = New-Object System.IO.FileStream -ArgumentList @($datapath, $filemode, [System.IO.FileAccess]::Write, [System.IO.FileShare]::Read)
    $bw = New-Object System.IO.BinaryWriter -ArgumentList $stream
    $bw.Write($c -as [int])
    $bw.Write($d -as [float])
  } finally {
    if ($bw) {
      $bw.Close()
      $bw = $null
    }
    if ($stream) {
      $stream.Close()
      $stream = $null
    }
  }
  trap {}
}COPY
1

评分人数

微信:flashercs
QQ:49908356

TOP

本帖最后由 flashercs 于 2022-7-30 22:47 编辑

回复 25# 领航


    楼上代码修复了错误,你试试.
日期更新的 行 放到a.txt 下面,会替换原来的文件.
如:
1|603982|20220722|39200819
1|603982|20220725|735065COPY
下行会替换上行数据.
微信:flashercs
QQ:49908356

TOP

回复 27# 领航


    修改了增量问题.你需要测试一下
微信:flashercs
QQ:49908356

TOP

回复 29# 领航


    不知道datacfg.dat的数据是什么格式.无法判断原因
微信:flashercs
QQ:49908356

TOP

回复 31# 领航


    又修改了一下代码,你试试这次能打开datacfg.dat吗
微信:flashercs
QQ:49908356

TOP

本帖最后由 flashercs 于 2022-8-1 11:37 编辑

回复 33# 领航
<#*,:&cls
@echo off
cd /d "%~dp0"
powershell -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~f0\" -ReadCount 0 | Out-String)))"
pause
exit /b
#>
# txtpath = dirdata
$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 {
  # support ps2.0, -LiteralPath
  [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) {
      # read data record
      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
        }
      }
    }
    # add txt record
    foreach ($pso in $_.Group) {
      $sortlist[($pso.c -as [int])] = $pso.d -as [float]
    }
    # write data record
    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
微信:flashercs
QQ:49908356

TOP

回复 35# 领航


    34楼修改了
微信:flashercs
QQ:49908356

TOP

返回列表