[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
回复 30# flashercs


    感谢老师回复!
再问一下,这个批处理可以批量处理一批文档吧?能做在一个批处理当中吗?谢谢!
如把c:\a.txt 每一行写到一个特定的.dat  放在c:\news\1001\
      c:\c.txt   每一行写到一个特定的.dat  放在c:\news\1002\
      c:\d.txt   每一行写到一个特定的.dat  放在 c:\news\1003\

TOP

回复 31# 领航


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

TOP

回复 32# flashercs

可以了。
     感谢老师回复!
再问一下,我想处理一批文档;能做在一个批处理当中吗?谢谢!
如把c:\a.txt 每一行写到一个特定的.dat  放在c:\news\1001\
      c:\c.txt   每一行写到一个特定的.dat  放在c:\news\1002\
      c:\d.txt   每一行写到一个特定的.dat  放在 c:\news\1003\

TOP

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

回复 33# 领航
  1. <#*,:&cls
  2. @echo off
  3. cd /d "%~dp0"
  4. powershell -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~f0\" -ReadCount 0 | Out-String)))"
  5. pause
  6. exit /b
  7. #>
  8. # txtpath = dirdata
  9. $dictxtpath = @{
  10.   "c:\txt\a.txt" = "c:\news\1001"
  11.   "c:\txt\b.txt" = "c:\news\1002"
  12.   "c:\txt\c.txt" = "c:\news\1003"
  13. }
  14. function Import-WgcCsv {
  15.   # support ps2.0, -LiteralPath
  16.   [CmdletBinding(DefaultParameterSetName = 'Delimiter')]
  17.   param (
  18.     [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  19.     [Alias('PSPath')]
  20.     [string[]]$LiteralPath,
  21.     [ValidateSet('ASCII', 'BigEndianUnicode', 'Default', 'OEM', 'Unicode', 'UTF7', 'UTF8', 'UTF32')]
  22.     [string]$Encoding = 'ASCII',
  23.     [Parameter(ParameterSetName = 'Delimiter')]
  24.     [char]$Delimiter = ',',
  25.     [Parameter(ParameterSetName = 'UseCulture')]
  26.     [switch]$UseCulture,
  27.     [Parameter()]
  28.     [ValidateNotNullOrEmpty()]
  29.     [string[]]$Header
  30.   )
  31.   process {
  32.     foreach ($lPath in $LiteralPath) {
  33.       trap {}
  34.       try {
  35.         $txt = (Get-Content -ReadCount 0 -LiteralPath $lPath -Encoding $Encoding) -join ([Environment]::NewLine)
  36.         if ($PSCmdlet.ParameterSetName -eq 'Delimiter') {
  37.           if ($null -ne $Header -and $Header.Count -gt 0) {
  38.             ConvertFrom-Csv -InputObject $txt -Delimiter $Delimiter -Header $Header
  39.           } else {
  40.             ConvertFrom-Csv -InputObject $txt -Delimiter $Delimiter
  41.           }
  42.         } else {
  43.           if ($null -ne $Header -and $Header.Count -gt 0) {
  44.             ConvertFrom-Csv -InputObject $txt -UseCulture:$UseCulture -Header $Header
  45.           } else {
  46.             ConvertFrom-Csv -InputObject $txt -UseCulture:$UseCulture
  47.           }
  48.         }
  49.       } finally {
  50.    
  51.       }
  52.     }
  53.   }
  54. }
  55. $sortlist = New-Object 'System.Collections.Generic.SortedList[int,float]'
  56. foreach ($txtpath in $dictxtpath.Keys) {
  57.   $txtpath
  58.   $dirdata = $dictxtpath[$txtpath]  
  59.   $rootdir = [System.IO.Directory]::CreateDirectory($dirdata)
  60.   Import-WgcCsv -LiteralPath $txtpath -Encoding UTF8 -Delimiter '|' -Header a, b, c, d | Group-Object -Property a, b | ForEach-Object {
  61.     $datapath = [System.IO.Path]::Combine($rootdir.FullName, "$($_.Values -join '_').dat")
  62.     $sortlist.Clear()
  63.     if (Test-Path -LiteralPath $datapath) {
  64.       # read data record
  65.       try {
  66.         $stream = New-Object System.IO.FileStream -ArgumentList @($datapath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read)
  67.         $br = New-Object System.IO.BinaryReader -ArgumentList $stream
  68.         while ($true) {
  69.           $key = $br.ReadInt32()
  70.           $value = $br.ReadSingle()
  71.           $sortlist[$key] = $value
  72.         }
  73.       } catch {}
  74.       finally {
  75.         if ($br) {
  76.           $br.Close()
  77.           $br = $null
  78.         }
  79.         if ($stream) {
  80.           $stream.Close()
  81.           $stream = $null
  82.         }
  83.       }
  84.     }
  85.     # add txt record
  86.     foreach ($pso in $_.Group) {
  87.       $sortlist[($pso.c -as [int])] = $pso.d -as [float]
  88.     }
  89.     # write data record
  90.     try {
  91.       $stream = New-Object System.IO.FileStream -ArgumentList @($datapath, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write, [System.IO.FileShare]::Read)
  92.       $bw = New-Object System.IO.BinaryWriter -ArgumentList $stream
  93.       foreach ($key in $sortlist.Keys) {
  94.         $bw.Write($key)
  95.         $bw.Write($sortlist[$key])
  96.       }
  97.     } finally {
  98.       if ($bw) {
  99.         $bw.Close()
  100.         $bw = $null
  101.       }
  102.       if ($stream) {
  103.         $stream.Close()
  104.         $stream = $null
  105.       }
  106.     }
  107.     trap {}
  108.   }
  109. }
复制代码
微信:flashercs
QQ:49908356

TOP

本帖最后由 领航 于 2022-8-1 00:01 编辑

回复 34# flashercs

报告老师,经反复测试,可以批量生成dat,但是软件读取不了,经比对,怀疑是之前的dat里面的历史数据都没有了,麻烦您分析一下是什么原因,谢谢!
附件里面是,单个导入和批量导入的前后文件,提供给老师参考https://wwn.lanzout.com/iBbDt08s090b

TOP

回复 35# 领航


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

TOP

回复 36# flashercs

请问老师,下面这个批处理,a.bat,b.bat,c,bat.d.bat分别运行完了,那些窗口怎么自动关闭?
    @echo off

start b.bat
start a.bat
start c.bat
start d.bat
exit

TOP

回复 36# flashercs


    还是不行,不麻烦老师了,我就一个一个运行吧

TOP

返回列表