批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程
[批处理文件精品]批处理版照片整理器[批处理文件精品]纯批处理备份&还原驱动在线第三方下载
返回列表 发帖

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

现用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

  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
复制代码
1

评分人数

    • yakeyun: 乐于助人,完美解决。技术 + 1

TOP

回复 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

TOP

回复 3# yakeyun


    直接复制我的就行,more +1你改错了,仔细对比下
1

评分人数

TOP

本帖最后由 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

TOP

本帖最后由 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
复制代码
1

评分人数

TOP

本帖最后由 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

TOP

回复 7# yakeyun
显示目录错误已修改。

TOP

回复 8# qixiaobin0715


测试正常,2个要求都解决。

TOP

最终代码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

TOP

返回列表