Board logo

标题: [文本处理] [已解决]求助批处理提取文本数据重复项,如何只保留一个相同项。 [打印本页]

作者: yakeyun    时间: 2021-10-11 11:22     标题: [已解决]求助批处理提取文本数据重复项,如何只保留一个相同项。

现用CrystalDiskInfo.exe硬盘信息读取软件,获取到2个硬盘的Smart至文件如下:

硬盘一:CrystalDiskInfo\Smart\Flashwar SSD W300 120GB1900021840025\Smart.ini
[PC]
ComputerName=DESKTOP-ES5I72I
[Flashwar SSD W300 120GB1900021840025FIRST]
Date=2021/10/10 10:15:25
HealthStatus=1
Temperature=32
PowerOnHours=136  
PowerOnCount=371
Life=100
HostWrites=2009
HostReads=1391
01=0
02=0
03=0
04=0
05=0
ReallocatedSectorsCount=0
06=0
07=0
08=0
09=0
0A=0
0B=0
0C=0
0D=0
0E=0
0F=0
[Flashwar SSD W300 120GB1900021840025]
Date=2021/10/10 10:15:25
HealthStatus=1
Temperature=32
PowerOnHours=136
PowerOnCount=371
Life=100
HostWrites=2009
HostReads=1391
01=0
02=0
03=0
04=0
05=0
ReallocatedSectorsCount=0
06=0
07=0
08=0
09=0
0A=0
0B=0
0C=0
0D=0
0E=0
0F=0
[Flashwar SSD W300 120GB1900021840025THRESHOLD]
01=0
02=0
03=0
04=0
05=0
06=0
07=0
08=0
09=0
0A=0
0B=0
0C=0
0D=0
0E=0
0F=0

=====分隔符==========分隔符===========分隔符===========分隔符====

硬盘二:CrystalDiskInfo\Smart\GALAX T0240A7DFF0779126200000066\Smart.ini
[PC]
ComputerName=DESKTOP-ES5I72I
[GALAX T0240A7DFF0779126200000066FIRST]
Date=2021/10/10 10:15:25
HealthStatus=1
PowerOnCount=1404
01=100
09=100
0C=100
A8=100
AA=100
AD=100
C0=100
DA=100
F1=100
PowerOnHours=11302
[GALAX T0240A7DFF0779126200000066]
Date=2021/10/10 14:28:34
HealthStatus=1
PowerOnCount=1404
01=100
09=100
0C=100
A8=100
AA=100
AD=100
C0=100
DA=100
F1=100
PowerOnHours=11302
[GALAX T0240A7DFF0779126200000066THRESHOLD]
01=50
09=0
0C=0
A8=0
AA=10
AD=0
C0=0
DA=50
F1=0

=====分隔符==========分隔符===========分隔符===========分隔符====

信息提取代码如下:
@echo off
setlocal enabledelayedexpansion
for /f "tokens=* delims=, " %%a in ('dir/a/s/b .\Smart\*.ini') do (
    echo,%%~npa
    for /f "tokens=2 delims==&" %%b in ('type "%%a" ^|findstr "PowerOnHours="') do (echo 通电时间:%%b)
    for /f "tokens=2 delims=^=" %%b in ('type "%%a" ^|find "PowerOnCount="') do (echo 通电次数:%%b)
    for /f "tokens=2 delims=^=" %%b in ('type "%%a" ^|find "HostWrites="') do (echo 主机写入量总计:%%b)
    for /f "tokens=2 delims=^=" %%b in ('type "%%a" ^|find "HostReads="') do (echo 主机读取量总计:%%b)

)
pause


求助大佬,如何过滤重复数值,只保留一个显示。同时修改代码“echo,%%~npa”,让其只显示文件的上级目录:即Flashwar SSD W300 120GB1900021840025   和  GALAX T0240A7DFF0779126200000066。
文件打包下载地址:https://www.lanzoui.com/iN3GQv7chkh
作者: went    时间: 2021-10-11 12:24

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "tokens=* delims=, " %%a in ('dir/a/s/b .\Smart\*.ini') do (
  4. set "p=%%~dpa" & for %%i in ("!p:~0,-1!") do echo %%~nxi
  5.     for /f "tokens=2 delims==&" %%b in ('type "%%a" ^|findstr "PowerOnHours="^|more +1') do (echo 通电时间:%%b)
  6.     for /f "tokens=2 delims=^=" %%b in ('type "%%a" ^|find "PowerOnCount="^|more +1') do (echo 通电次数:%%b)
  7.     for /f "tokens=2 delims=^=" %%b in ('type "%%a" ^|find "HostWrites="^|more +1') do (echo 主机写入量总计:%%b)
  8.     for /f "tokens=2 delims=^=" %%b in ('type "%%a" ^|find "HostReads="^|more +1') do (echo 主机读取量总计:%%b)
  9. )
  10. pause
复制代码

作者: yakeyun    时间: 2021-10-11 12:40

回复 2# went


谢谢大佬,目录名显示功能正常了,不过过滤功能暂时报错,不能正常使用:

   
@echo off
setlocal enabledelayedexpansion
for /f "tokens=* delims=, " %%a in ('dir/a/s/b .\Smart\*.ini') do (
    set "p=%%~dpa" & for %%i in ("!p:~0,-1!") do echo %%~nxi
    for /f "tokens=2 delims=^=" %%b in ('type "%%a" ^|findstr "PowerOnHours=" ^|more +1'') do (echo 通电时间:%%b)
    for /f "tokens=2 delims=^=" %%b in ('type "%%a" ^|find "PowerOnCount="') do (echo 通电次数:%%b)
    for /f "tokens=2 delims=^=" %%b in ('type "%%a" ^|find "HostWrites="') do (echo 主机写入量总计:%%b)
    for /f "tokens=2 delims=^=" %%b in ('type "%%a" ^|find "HostReads="') do (echo 主机读取量总计:%%b)
)
pause
作者: went    时间: 2021-10-11 12:53

回复 3# yakeyun


    直接复制我的就行,more +1你改错了,仔细对比下
作者: yakeyun    时间: 2021-10-11 13:24

本帖最后由 yakeyun 于 2021-10-11 13:31 编辑

回复 4# went


确实是我这边的原因,我只对照修改了一处,然后后面+1后面符号没改对,重新修改解决问题。直接用大佬的代码完美解决问题。


最终代码:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=* delims=, " %%a in ('dir/a/s/b .\Smart\*.ini') do (
    set "p=%%~dpa" & for %%i in ("!p:~0,-1!") do echo %%~nxi
    for /f "tokens=2 delims=^=" %%b in ('type "%%a" ^|findstr "PowerOnHours=" ^|more +1') do (echo 通电时间:%%b)
    for /f "tokens=2 delims=^=" %%b in ('type "%%a" ^|find "PowerOnCount=" ^|more +1') do (echo 通电次数:%%b)
    for /f "tokens=2 delims=^=" %%b in ('type "%%a" ^|find "HostWrites=" ^|more +1') do (echo 主机写入量总计:%%b)
    for /f "tokens=2 delims=^=" %%b in ('type "%%a" ^|find "HostReads=" ^|more +1') do (echo 主机读取量总计:%%b)

)
pause>nul
作者: qixiaobin0715    时间: 2021-10-11 13:27

本帖最后由 qixiaobin0715 于 2021-10-11 14:18 编辑

未测试:
  1. @echo off
  2. set Names=PowerOnHours PowerOnCount HostWrites HostReads
  3. setlocal enabledelayedexpansion
  4. for /f "delims=" %%a in ('dir /b /s /a-d Smart\*.ini') do (
  5.     for %%k in ("%%~dpa.") do echo,%%~nk
  6.     set PowerOnHours=通电时间:
  7.     set PowerOnCount=通电次数:
  8.     set HostWrites=主机写入量总计:
  9.     set HostReads=主机读取量总计:
  10.     for /f "tokens=1,2 delims==" %%i in ('findstr "%Names%" "%%a"') do (
  11.         if defined %%i echo,!%%i!%%j
  12.         set %%i=
  13.     )
  14. )
  15. pause
复制代码

作者: yakeyun    时间: 2021-10-11 13:37

本帖最后由 yakeyun 于 2021-10-11 14:07 编辑

回复 6# qixiaobin0715


代码可用,不过显示文件所在目录会出错,结合"went" 大佬的代码(显示文件目录名),把代码修改了一下。最终代码如下:

@echo off
set Names=PowerOnHours PowerOnCount HostWrites HostReads
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir /b /s /a-d Smart\*.ini') do (
    set "p=%%~dpa" & for %%i in ("!p:~0,-1!") do echo %%~nxi
    set PowerOnHours=通电时间:
    set PowerOnCount=通电次数:
    set HostWrites=主机写入量总计:
    set HostReads=主机读取量总计:
    for /f "tokens=1,2 delims==" %%i in ('findstr "%Names%" "%%a"') do (
        if defined %%i echo,!%%i!%%j
        set %%i=
    )
)
pause>nul

作者: qixiaobin0715    时间: 2021-10-11 14:37

回复 7# yakeyun
显示目录错误已修改。
作者: yakeyun    时间: 2021-10-11 14:48

回复 8# qixiaobin0715


测试正常,2个要求都解决。
作者: yakeyun    时间: 2021-10-20 13:57

最终代码1(判断数据是否超标,如果超标就输出到错误文本):
@echo off
setlocal enabledelayedexpansion
if exist YPerror.ini (del YPerror.ini)
(for /f "tokens=* delims=, " %%a in ('dir/a/s/b .\Smart\*.ini') do (
    set "p=%%~dpa" & for %%i in ("!p:~0,-1!") do (
    for /f "tokens=2 delims==&" %%b in ('type "%%a" ^|findstr "PowerOnHours="^|more +1') do (if %%b GEQ 150 (echo %%~nxi   通电时间超时^(标准150小时^):%%b 小时 >>.\YPerror.ini))
    for /f "tokens=2 delims=^=" %%b in ('type "%%a" ^|find "PowerOnCount="^|more +1') do (if %%b GEQ 100 (echo %%~nxi  通电次数超标^(标准100次^):%%b 次 >>.\YPerror.ini))
    for /f "tokens=2 delims=^=" %%b in ('type "%%a" ^|find "HostWrites="^|more +1') do (if %%b GEQ 500 (echo %%~nxi  主机写入量总计超标^(标准500GB^):%%b GB >>.\YPerror.ini))
    for /f "tokens=2 delims=^=" %%b in ('type "%%a" ^|find "HostReads="^|more +1') do (if %%b GEQ 500 (echo %%~nxi  主机读取量总计超标^(标准500GB^):%%b GB >>.\YPerror.ini))
)))
type yperror.ini
pause>nul
exit




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2