Board logo

标题: [文本处理] 指定文件夹中的XML内查找内容,显示查找到的内容后面8个字符。 [打印本页]

作者: Bonnie    时间: 2022-11-14 15:52     标题: 指定文件夹中的XML内查找内容,显示查找到的内容后面8个字符。

需求:1、在固定路径C:\oud\traces下的AC、ADS、AP三个文件夹内寻找最新时间创建的文件夹。
         2、在最新时间创建的文件夹下查找XML文件里所包含的字符"3101DF05”。提取后面的8个字符。
         3、把提取出来的8个字符(16进制)转换进制为字符串并复制出来。任何文本文档或程序内可以直接粘贴使用。
作者: hfxiang    时间: 2022-11-14 16:03

  1. gawk "/3101DF05.{8}/{print gensub(/^.*3101DF05(.{8}).*$/,\"\\1\",\"g\")}" input.xml>output.txt
复制代码

作者: Bonnie    时间: 2022-11-14 16:08

hfxiang 发表于 2022-11-14 16:03



    怎么用ECHO显示所输出的数据3101DF05........
作者: hfxiang    时间: 2022-11-14 16:11

  1. for /f %%a in ('gawk "/3101DF05.{8}/{print gensub(/^.*3101DF05(.{8}).*$/,\"\\1\",\"g\")}" input.xml') do echo %%a
复制代码

作者: aloha20200628    时间: 2022-11-14 17:07

LZ请看最新回复》http://www.bathome.net/viewthrea ... mp;extra=#pid261350
作者: Bonnie    时间: 2022-11-14 17:14

LZ请看最新回复》
aloha20200628 发表于 2022-11-14 17:07
  1. @set @n=0;WScript.Echo(eval('String.fromCharCode('+WScript.Arguments(0).replace(/(..)/g,"0x$1,")+'"")'));/*
  2. @echo off
  3. set /p "hex=请输入数据:"
  4. for /f "delims=" %%Y in ('cscript -nologo -e:jscript "%~0" %hex%') do set "bin=%%Y"
  5. ECHO 您的密码为:%bin%
  6. pause */
复制代码
以上16进制转换成字符串代码没办法串进去 (3101DF05后面的8个字符)
作者: aloha20200628    时间: 2022-11-14 17:54

还是重新粘贴一下在“批处理如何在XML内查找内容,显示查找到的内容及后面8个字符”旧帖中的最后回复吧...
如下》
刚下载了lz的样本文件,才知其中‘给CMD埋了个大坑’,目标字段‘3101DF05...’所在行的字符量=70680,远超过纯P单个变量的上限(8k),for (...) 和 set ... 根本扛不住findstr的返回值。如此本贴须请其他脚本代劳了。
再给一个cmd+cscript@js混编代码》
还是先用 dir /s/b/a-d/o-d/tw 获取指定目录下最新创建或修改的目标文件
  1. @set @v=1 /*
  2. @echo off
  3. (dir/s/b/a-d/o-d/tw C:\oud\traces\*.xml)>tmp.txt&(set/p inF=<tmp.txt)&(del/q tmp.txt)
  4. for /f "delims=" %%v in ('cscript /e:jscript "%~f0" "%inF%" ') do echo,%%v
  5. exit/b
  6. */
  7.     var v = WSH.Arguments;
  8.     var fso = new ActiveXObject('scripting.filesystemobject');
  9.     var fp = fso.opentextfile(v(0));
  10.     var alllines = fp.readall(); fp.Close();
  11.     alllines = alllines.replace(/[\r\n]/g, '');
  12.     var kwF = alllines.replace(/.*(3101DF05.{8}).*/gi, '$1');
  13.     WSH.Echo(kwF);
复制代码

作者: BAT221110    时间: 2022-11-14 18:49

以下脚本保存为OUD.VBS,放在C:\oud\traces,在命令行输入
  1. CSCRIPT OUD.VBS
复制代码
来运行。运行结果示例:
  1. Microsoft (R) Windows Script Host Version 5.8
  2. 版权所有(C) Microsoft Corporation 1996-2001。保留所有权利。
  3. 31323334
复制代码
代码:
  1. FUNCTION MRF192(FD4, DATE4, FN4) ' MOST RECENT FOLDER
  2. DIM F1, FSO
  3. SET FSO = CREATEOBJECT("SCRIPTING.FILESYSTEMOBJECT")
  4. FOR EACH F1 IN FSO.GETFOLDER(FD4).SUBFOLDERS
  5. IF F1.DATECREATED > DATE4 THEN
  6. DATE4 = F1.DATECREATED
  7. SET FN4 = F1
  8. END IF
  9. NEXT
  10. SET MRF192 = FN4
  11. END FUNCTION
  12. FUNCTION GETMRF
  13. DIM FSO, F, FC, FD4
  14. DIM MRF925V ' MOST RECENT FOLDER
  15. SET FSO = CREATEOBJECT("SCRIPTING.FILESYSTEMOBJECT")
  16. SET MRF925V = MRF192("AC", DATESERIAL(1970, 1, 1), NOTHING)
  17. SET MRF925V = MRF192("ADS", MRF925V.DATECREATED, MRF925V)
  18. SET MRF925V = MRF192("AP", MRF925V.DATECREATED, MRF925V)
  19. SET GETMRF = MRF925V
  20. END FUNCTION
  21. SUB FINDXML28(OUT207)
  22. DIM MRF925V, CMD329, WSH329
  23. SET WSH329 = CREATEOBJECT("WSCRIPT.SHELL")
  24. SET MRF925V = GETMRF
  25. CMD329 = "CMD /C FINDSTR /S /M 3101DF05 """ & MRF925V.PATH & "\*.xml"" > " & OUT207
  26. WSH329.RUN CMD329, 10, TRUE
  27. END SUB
  28. FUNCTION READ4UTF8(FPATH)
  29.     DIM ADOS
  30.     SET ADOS = CREATEOBJECT("ADODB.STREAM")
  31.      
  32.     WITH ADOS
  33.         .CHARSET = "UTF-8"
  34.         .TYPE = 2
  35.         .OPEN
  36.         .LOADFROMFILE FPATH
  37.         READ4UTF8 = .READTEXT
  38.         .CLOSE
  39.     END WITH
  40.     SET ADOS = NOTHING
  41. END FUNCTION
  42. SUB FINDHEX46(LINE40)
  43. DIM REGEX, MATCH, MATCHES
  44. SET REGEX = NEW REGEXP
  45. REGEX.PATTERN = "3101DF05(........)"
  46. SET MATCHES = REGEX.EXECUTE(READ4UTF8(LINE40))
  47. WSCRIPT.ECHO MATCHES(0).SUBMATCHES(0)
  48. END SUB
  49. SUB FINDHEXS(OUT207)
  50. DIM FSO, TS
  51. SET FSO = CREATEOBJECT("SCRIPTING.FILESYSTEMOBJECT")
  52. SET TS = FSO.OPENTEXTFILE(OUT207, 1, FALSE, TRISTATETRUE)
  53. DO UNTIL TS.ATENDOFSTREAM
  54. FINDHEX46 TS.READLINE
  55. LOOP
  56. TS.CLOSE
  57. END SUB
  58. SUB MAIN4
  59. DIM OUT207
  60. OUT207 = "OUDTMP12421.TXT"
  61. FINDXML28 OUT207
  62. FINDHEXS OUT207
  63. END SUB
  64. MAIN4
复制代码

作者: Bonnie    时间: 2022-11-14 19:29

以下脚本保存为OUD.VBS,放在C:\oud\traces,在命令行输入来运行。运行结果示例:代码:
BAT221110 发表于 2022-11-14 18:49
  1. Microsoft Windows [版本 10.0.19045.2251]
  2. (c) Microsoft Corporation。保留所有权利。
  3. C:\Windows\system32>cd C:\oud\traces
  4. C:\oud\traces>CSCRIPT oud.vbs
  5. Microsoft (R) Windows Script Host Version 5.812
  6. 版权所有(C) Microsoft Corporation。保留所有权利。
  7. C:\oud\traces\oud.vbs(5, 2) Microsoft VBScript 运行时错误: 路径未找到
  8. C:\oud\traces>
复制代码
哈哈。。。
作者: BAT221110    时间: 2022-11-14 20:29

5.1版
  1. SUB DEBUGPRINT819(S)
  2. 'WSCRIPT.ECHO "[DEBUG] " & S
  3. END SUB
  4. FUNCTION DATECREATED821(X) ' DATECREATED OR NOTHING
  5. IF X IS NOTHING THEN
  6. DATECREATED821 = DATESERIAL(1970, 1, 1)
  7. ELSE
  8. DATECREATED821 = X.DATECREATED
  9. END IF
  10. END FUNCTION
  11. FUNCTION MRF192(FD4, FN4) ' MOST RECENT FOLDER
  12. SET FSO = CREATEOBJECT("SCRIPTING.FILESYSTEMOBJECT")
  13. IF FSO.FOLDEREXISTS(FD4) THEN
  14. DEBUGPRINT819 "READ FOLDER " & FD4
  15. DATE4 = DATECREATED821(FN4)
  16. FOR EACH F1 IN FSO.GETFOLDER(FD4).SUBFOLDERS
  17. IF F1.DATECREATED > DATE4 THEN
  18. DATE4 = F1.DATECREATED
  19. SET FN4 = F1
  20. END IF
  21. NEXT
  22. END IF
  23. SET MRF192 = FN4
  24. END FUNCTION
  25. FUNCTION GETMRF
  26. DIM FSO, F, FC, FD4
  27. DIM MRF925V ' MOST RECENT FOLDER
  28. SET FSO = CREATEOBJECT("SCRIPTING.FILESYSTEMOBJECT")
  29. SET MRF925V = NOTHING
  30. SET MRF925V = MRF192("AC", MRF925V)
  31. SET MRF925V = MRF192("ADS", MRF925V)
  32. SET MRF925V = MRF192("AP", MRF925V)
  33. SET GETMRF = MRF925V
  34. END FUNCTION
  35. SUB FINDXML28(OUT207)
  36. DIM MRF925V, CMD329, WSH329
  37. SET WSH329 = CREATEOBJECT("WSCRIPT.SHELL")
  38. SET MRF925V = GETMRF
  39. CMD329 = "CMD /C FINDSTR /S /M 3101DF05 """ & MRF925V.PATH & "\*.xml"" > " & OUT207
  40. WSH329.RUN CMD329, 10, TRUE
  41. END SUB
  42. FUNCTION READ4UTF8(FPATH)
  43.     DIM ADOS
  44.     SET ADOS = CREATEOBJECT("ADODB.STREAM")
  45.      
  46.     WITH ADOS
  47.         .CHARSET = "UTF-8"
  48.         .TYPE = 2
  49.         .OPEN
  50.         .LOADFROMFILE FPATH
  51.         READ4UTF8 = .READTEXT
  52.         .CLOSE
  53.     END WITH
  54.     SET ADOS = NOTHING
  55. END FUNCTION
  56. FUNCTION HEX2INT701(X)
  57. HEX2INT701 = EVAL("&H" & X)
  58. END FUNCTION
  59. FUNCTION HEX2STR75(X)
  60. S768 = ""
  61. FOR I = 1 TO LEN(X) / 2
  62. I798 = HEX2INT701(MID(X, I*2 - 1, 2))
  63. S768 = S768 & CHR(I798)
  64. NEXT
  65. HEX2STR75 = S768
  66. END FUNCTION
  67. SUB FINDHEX46(LINE40)
  68. DIM REGEX, MATCH, MATCHES
  69. SET REGEX = NEW REGEXP
  70. REGEX.PATTERN = "3101DF05(........)"
  71. SET MATCHES = REGEX.EXECUTE(READ4UTF8(LINE40))
  72. WSCRIPT.ECHO HEX2STR75(MATCHES(0).SUBMATCHES(0))
  73. END SUB
  74. SUB FINDHEXS(OUT207)
  75. DIM FSO, TS
  76. SET FSO = CREATEOBJECT("SCRIPTING.FILESYSTEMOBJECT")
  77. SET TS = FSO.OPENTEXTFILE(OUT207, 1, FALSE, TRISTATETRUE)
  78. DO UNTIL TS.ATENDOFSTREAM
  79. FINDHEX46 TS.READLINE
  80. LOOP
  81. TS.CLOSE
  82. END SUB
  83. SUB RMFILE109(X)
  84. SET FSO = CREATEOBJECT("SCRIPTING.FILESYSTEMOBJECT")
  85. FSO.DELETEFILE(X)
  86. END SUB
  87. SUB MAIN4
  88. OUT207 = "OUDTMP12421.TXT"
  89. FINDXML28 OUT207
  90. FINDHEXS OUT207
  91. RMFILE109 OUT207
  92. END SUB
  93. MAIN4
复制代码





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