Board logo

标题: [文本处理] 批处理如何搜索日志中的关键字并锁屏? [打印本页]

作者: lion991128    时间: 2013-9-16 19:00     标题: 批处理如何搜索日志中的关键字并锁屏?

有个软件在每天会产生很多LOG文件(TXT文本格式),在文本里面会有DONE或者ERROR表示成功或者失败:

如何用批处理进行文本的搜索如果是有ERROR的复制一份到桌面并且对本机进行锁屏操作。是否这样可以用批处理实现?
作者: lion991128    时间: 2013-9-16 21:21

没有人能教教我吗?
作者: DAIC    时间: 2013-9-16 21:31

  1. @echo off
  2. if exist "%userprofile%\Desktop\" (
  3.     set "mydesk=%userprofile%\Desktop"
  4. ) else if exist "%userprofile%\桌面\" (
  5.     set "mydesk=%userprofile%\Desktop"
  6. ) else (
  7.     echo 找不到桌面的位置
  8. )
  9. for /f "delims=" %%i in ('findstr /m "ERROR" *.log') do (
  10.     copy /y "%%i" %mydesk%
  11. )
  12. pause
复制代码

作者: lion991128    时间: 2013-9-17 10:07

谢谢老大慷慨赐教 但是还有一个问题 我何如能在搜索到错误信息时锁定本机呢?
作者: lion991128    时间: 2013-9-17 11:17

还有一个问题 在XP下运行会出现语法错误 是怎么回事呢
作者: apang    时间: 2013-9-17 11:21

修改3楼的:
  1. @echo off
  2. if exist "%userprofile%\Desktop\" (
  3.     set "mydesk=%userprofile%\Desktop"
  4. ) else (
  5.     if exist "%userprofile%\桌面\" (
  6.         set "mydesk=%userprofile%\桌面"
  7.     ) else echo 找不到桌面的位置
  8. )
  9. for /f "delims=" %%i in ('findstr /m "ERROR" *.log') do (
  10.     copy /y "%%i" "%mydesk%\"
  11. )
  12. rundll32.exe user32.dll,LockWorkStation
复制代码

作者: lion991128    时间: 2013-9-17 11:49

完美运行 谢谢2位高手的帮忙
作者: DAIC    时间: 2013-9-17 12:41

回复 7# lion991128


    6楼的代码在搜索不到错误信息的情况下也会锁屏,你试试。
作者: DAIC    时间: 2013-9-17 12:41

回复 5# lion991128


    不管什么时候,请把报错信息贴出来,否则大家没有办法继续帮助你。
作者: lion991128    时间: 2013-9-18 11:29

Date;2013-06-11;09:15:48
Product-Sequence;Melco;Calibration_Test_FAST
PartNumber-SerialNumber;783801-0029;QF0464S
TestTime;29.24;sec
Result-Error-Description;Failed;75;Range OUT of Limits
Start Calibration Mode;;;Exp;OK;Meas;OK;/;1;/assed;0
Initialization Value Mem;;;Exp;01;Meas;01;/;;;Passed;0
Initialization Value Check;;;Exp;01;Meas;01;/;;;Passed;0
Range Temp;Min;360;Max;440;Meas;408;bit;;;Passed;0
Communication Speed;;;;;Meas;250;/;;;Passed;0
CAN_NML_SND;;;;;Meas;18FF1002;/;;;Passed;0
CAN_NML_REC;;;;;Meas;18FF1000;/;;;Passed;0
Range;;;;;Meas;408;bit;;;Passed;0
Range;;;;;Meas;408;bit;;;Passed;0
Range;;;;;Meas;408;bit;;;Passed;0
Sweep Time;Min;500;Max;3000;Meas;1495;msec;;;Passed;0
FOHS;;;;;Meas;2;bit;;;Passed;0
Screw FOHS Position;Min;13;Max;15;Meas;13;bit;;;Passed;0
Recheck Screw FOHS Position;Min;13;Max;15;Meas;12;bit;;;Failed;75

这个是错误信息

Date;2013-06-11;07:11:47
Product-Sequence;Melco;Calibration_Test_FAST
PartNumber-SerialNumber;783801-0029;QF0410S
TestTime;133.72;sec
Result-Error-Description;Passed;0;
Start Calibration Mode;;;Exp;OK;Meas;OK;/;1;/;Passed;0
Initialization Value Mem;;;Exp;01;Meas;01;/;;;Passed;0
Initialization Value Check;;;Exp;01;Meas;01;/;;;Passed;0
Range Temp;Min;360;Max;440;Meas;411;bit;;;Passed;0
Communication Speed;;;;;Meas;250;/;;;Passed;0
CAN_NML_SND;;;;;Meas;18FF1002;/;;;Passed;0
CAN_NML_REC;;;;;Meas;18FF1000;/;;;Passed;0
Range;;;;;Meas;411;bit;;;Passed;0

以上是成功的信息
作者: lion991128    时间: 2013-9-18 11:34

回复 8# DAIC

这位老兄真细心 是有这个问题存在,而且还有一个问题就是如果以前的错误信息也会在判定成功失败的时候认为是现在出现的失败。

是否能达到进行一个差备的比较或者是能根据系统时间去认证只在今天产生的LOG中是否有错误存在?
作者: terse    时间: 2013-9-18 13:46

本帖最后由 terse 于 2013-9-18 13:49 编辑

这样行不
  1. @echo off
  2. set "flag="
  3. set reg=reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
  4. for /f "skip=4 tokens=2*" %%i in ('%reg% /v "desktop"') do set "desktop=%%j"
  5. for /f "delims=" %%i in ('findstr /im "%date:~,10%" *.log') do (
  6. findstr /im ERROR "%%i" >nul && (
  7. copy "%%i" "%desktop%"
  8. set flag=rundll32.exe user32.dll,LockWorkStation
  9. )
  10. )
  11. %flag%
复制代码

作者: lion991128    时间: 2013-9-18 16:38

这样行不
terse 发表于 2013-9-18 13:46


%date:~ 的这里是取系统时间的值吗?但是生成的LOG并不是标准的时间格式:783801-0029_QF0464S_20130611_091548.txt 例如这样的名字 红色的地方是标识日期的这个如何识别啊?
作者: terse    时间: 2013-9-18 18:44

本帖最后由 terse 于 2013-9-23 14:07 编辑

回复 13# lion991128
文件名含当天日期
  1. @echo off
  2. set "flag="
  3. set reg=reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
  4. set dt=%date:~,10%
  5. for /f "skip=4 tokens=2*" %%i in ('%reg% /v "desktop"') do (
  6. for /f "tokens=*" %%k in ("%%j") do set "desktop=%%k"
  7. )
  8. for /f "delims=" %%i in ('findstr /im "ERROR" *.log^|findstr "%dt:-=-*%"') do (
  9.         copy "%%i" "%desktop%"
  10.         set flag=rundll32.exe user32.dll,LockWorkStation   
  11. )
  12. %flag%
  13. pause
复制代码
文件内容含当天日期
  1. @echo off
  2. set "flag="
  3. set reg=reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
  4. set dt=%date:~,10%
  5. for /f "skip=4 tokens=2*" %%i in ('%reg% /v "desktop"') do (
  6. for /f "tokens=*" %%k in ("%%j") do set "desktop=%%k"
  7. )
  8. for /f "delims=" %%i in ('findstr /im "%dt:-=-*%" *.log') do (
  9.         findstr /im ERROR "%%i" >nul && (
  10.                 copy "%%i" "%desktop%"
  11.                 set flag=rundll32.exe user32.dll,LockWorkStation
  12.         )
  13. )
  14. %flag%
  15. pause
复制代码
当天日期文件
  1. @echo off
  2. set "flag="
  3. set reg=reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
  4. for /f "skip=4 tokens=2*" %%i in ('%reg% /v "desktop"') do (
  5. for /f "tokens=*" %%k in ("%%j") do set "desktop=%%k"
  6. )
  7. for /f "delims=" %%i in ('findstr /im "ERROR" *.log') do (
  8.     for /f %%j in ("%%~ti") do (
  9.         if /i "%%j" == "%date:~,10%" (
  10.            copy "%%i" "%desktop%"
  11.            set flag=rundll32.exe user32.dll,LockWorkStation
  12.         )
  13.     )   
  14. )
  15. %flag%
  16. pause
复制代码

作者: lion991128    时间: 2013-9-22 10:10

回复 14# terse


  回大大 这个运行说是语法错误。
作者: terse    时间: 2013-9-23 13:45

回复 15# lion991128
你什么系统
另 代码搜索的是文件内容 看是否包含当天日期 也就是你在10楼的  “Date;2013-06-11;07:11:47”
看你指出的又貌似文件名包含了当天日期?
那么到底文件名含当天日期 还是文件内容含当天日期呢
我这里系统XP 测试几个文件没出错




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