标题: [文本处理] 指定文件夹中的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
- 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
- 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
- @set @n=0;WScript.Echo(eval('String.fromCharCode('+WScript.Arguments(0).replace(/(..)/g,"0x$1,")+'"")'));/*
- @echo off
- set /p "hex=请输入数据:"
- for /f "delims=" %%Y in ('cscript -nologo -e:jscript "%~0" %hex%') do set "bin=%%Y"
- ECHO 您的密码为:%bin%
- 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 获取指定目录下最新创建或修改的目标文件- @set @v=1 /*
- @echo off
- (dir/s/b/a-d/o-d/tw C:\oud\traces\*.xml)>tmp.txt&(set/p inF=<tmp.txt)&(del/q tmp.txt)
- for /f "delims=" %%v in ('cscript /e:jscript "%~f0" "%inF%" ') do echo,%%v
- exit/b
- */
- var v = WSH.Arguments;
- var fso = new ActiveXObject('scripting.filesystemobject');
- var fp = fso.opentextfile(v(0));
- var alllines = fp.readall(); fp.Close();
- alllines = alllines.replace(/[\r\n]/g, '');
- var kwF = alllines.replace(/.*(3101DF05.{8}).*/gi, '$1');
- WSH.Echo(kwF);
复制代码
作者: BAT221110 时间: 2022-11-14 18:49
以下脚本保存为OUD.VBS,放在C:\oud\traces,在命令行输入复制代码
来运行。运行结果示例:- Microsoft (R) Windows Script Host Version 5.8
- 版权所有(C) Microsoft Corporation 1996-2001。保留所有权利。
-
- 31323334
复制代码
代码:- FUNCTION MRF192(FD4, DATE4, FN4) ' MOST RECENT FOLDER
- DIM F1, FSO
-
- SET FSO = CREATEOBJECT("SCRIPTING.FILESYSTEMOBJECT")
- FOR EACH F1 IN FSO.GETFOLDER(FD4).SUBFOLDERS
- IF F1.DATECREATED > DATE4 THEN
- DATE4 = F1.DATECREATED
- SET FN4 = F1
- END IF
- NEXT
-
- SET MRF192 = FN4
- END FUNCTION
-
-
- FUNCTION GETMRF
- DIM FSO, F, FC, FD4
- DIM MRF925V ' MOST RECENT FOLDER
-
- SET FSO = CREATEOBJECT("SCRIPTING.FILESYSTEMOBJECT")
- SET MRF925V = MRF192("AC", DATESERIAL(1970, 1, 1), NOTHING)
- SET MRF925V = MRF192("ADS", MRF925V.DATECREATED, MRF925V)
- SET MRF925V = MRF192("AP", MRF925V.DATECREATED, MRF925V)
- SET GETMRF = MRF925V
- END FUNCTION
-
-
- SUB FINDXML28(OUT207)
- DIM MRF925V, CMD329, WSH329
-
- SET WSH329 = CREATEOBJECT("WSCRIPT.SHELL")
- SET MRF925V = GETMRF
-
- CMD329 = "CMD /C FINDSTR /S /M 3101DF05 """ & MRF925V.PATH & "\*.xml"" > " & OUT207
- WSH329.RUN CMD329, 10, TRUE
- END SUB
-
-
- FUNCTION READ4UTF8(FPATH)
- DIM ADOS
- SET ADOS = CREATEOBJECT("ADODB.STREAM")
-
- WITH ADOS
- .CHARSET = "UTF-8"
- .TYPE = 2
- .OPEN
- .LOADFROMFILE FPATH
- READ4UTF8 = .READTEXT
- .CLOSE
- END WITH
- SET ADOS = NOTHING
- END FUNCTION
-
-
- SUB FINDHEX46(LINE40)
- DIM REGEX, MATCH, MATCHES
- SET REGEX = NEW REGEXP
- REGEX.PATTERN = "3101DF05(........)"
- SET MATCHES = REGEX.EXECUTE(READ4UTF8(LINE40))
- WSCRIPT.ECHO MATCHES(0).SUBMATCHES(0)
- END SUB
-
-
- SUB FINDHEXS(OUT207)
- DIM FSO, TS
- SET FSO = CREATEOBJECT("SCRIPTING.FILESYSTEMOBJECT")
- SET TS = FSO.OPENTEXTFILE(OUT207, 1, FALSE, TRISTATETRUE)
-
- DO UNTIL TS.ATENDOFSTREAM
- FINDHEX46 TS.READLINE
- LOOP
-
- TS.CLOSE
- END SUB
-
- SUB MAIN4
- DIM OUT207
-
- OUT207 = "OUDTMP12421.TXT"
- FINDXML28 OUT207
- FINDHEXS OUT207
- END SUB
-
- MAIN4
复制代码
作者: Bonnie 时间: 2022-11-14 19:29
以下脚本保存为OUD.VBS,放在C:\oud\traces,在命令行输入来运行。运行结果示例:代码:
BAT221110 发表于 2022-11-14 18:49
- Microsoft Windows [版本 10.0.19045.2251]
- (c) Microsoft Corporation。保留所有权利。
-
- C:\Windows\system32>cd C:\oud\traces
-
- C:\oud\traces>CSCRIPT oud.vbs
- Microsoft (R) Windows Script Host Version 5.812
- 版权所有(C) Microsoft Corporation。保留所有权利。
-
- C:\oud\traces\oud.vbs(5, 2) Microsoft VBScript 运行时错误: 路径未找到
-
-
- C:\oud\traces>
复制代码
哈哈。。。
作者: BAT221110 时间: 2022-11-14 20:29
5.1版- SUB DEBUGPRINT819(S)
- 'WSCRIPT.ECHO "[DEBUG] " & S
- END SUB
-
-
- FUNCTION DATECREATED821(X) ' DATECREATED OR NOTHING
- IF X IS NOTHING THEN
- DATECREATED821 = DATESERIAL(1970, 1, 1)
- ELSE
- DATECREATED821 = X.DATECREATED
- END IF
- END FUNCTION
-
-
- FUNCTION MRF192(FD4, FN4) ' MOST RECENT FOLDER
- SET FSO = CREATEOBJECT("SCRIPTING.FILESYSTEMOBJECT")
- IF FSO.FOLDEREXISTS(FD4) THEN
- DEBUGPRINT819 "READ FOLDER " & FD4
- DATE4 = DATECREATED821(FN4)
- FOR EACH F1 IN FSO.GETFOLDER(FD4).SUBFOLDERS
- IF F1.DATECREATED > DATE4 THEN
- DATE4 = F1.DATECREATED
- SET FN4 = F1
- END IF
- NEXT
- END IF
-
- SET MRF192 = FN4
- END FUNCTION
-
-
- FUNCTION GETMRF
- DIM FSO, F, FC, FD4
- DIM MRF925V ' MOST RECENT FOLDER
-
- SET FSO = CREATEOBJECT("SCRIPTING.FILESYSTEMOBJECT")
- SET MRF925V = NOTHING
- SET MRF925V = MRF192("AC", MRF925V)
- SET MRF925V = MRF192("ADS", MRF925V)
- SET MRF925V = MRF192("AP", MRF925V)
- SET GETMRF = MRF925V
- END FUNCTION
-
-
- SUB FINDXML28(OUT207)
- DIM MRF925V, CMD329, WSH329
-
- SET WSH329 = CREATEOBJECT("WSCRIPT.SHELL")
- SET MRF925V = GETMRF
-
- CMD329 = "CMD /C FINDSTR /S /M 3101DF05 """ & MRF925V.PATH & "\*.xml"" > " & OUT207
- WSH329.RUN CMD329, 10, TRUE
- END SUB
-
-
- FUNCTION READ4UTF8(FPATH)
- DIM ADOS
- SET ADOS = CREATEOBJECT("ADODB.STREAM")
-
- WITH ADOS
- .CHARSET = "UTF-8"
- .TYPE = 2
- .OPEN
- .LOADFROMFILE FPATH
- READ4UTF8 = .READTEXT
- .CLOSE
- END WITH
- SET ADOS = NOTHING
- END FUNCTION
-
-
- FUNCTION HEX2INT701(X)
- HEX2INT701 = EVAL("&H" & X)
- END FUNCTION
-
-
- FUNCTION HEX2STR75(X)
- S768 = ""
-
- FOR I = 1 TO LEN(X) / 2
- I798 = HEX2INT701(MID(X, I*2 - 1, 2))
- S768 = S768 & CHR(I798)
- NEXT
-
- HEX2STR75 = S768
- END FUNCTION
-
-
- SUB FINDHEX46(LINE40)
- DIM REGEX, MATCH, MATCHES
- SET REGEX = NEW REGEXP
- REGEX.PATTERN = "3101DF05(........)"
- SET MATCHES = REGEX.EXECUTE(READ4UTF8(LINE40))
- WSCRIPT.ECHO HEX2STR75(MATCHES(0).SUBMATCHES(0))
- END SUB
-
-
- SUB FINDHEXS(OUT207)
- DIM FSO, TS
- SET FSO = CREATEOBJECT("SCRIPTING.FILESYSTEMOBJECT")
- SET TS = FSO.OPENTEXTFILE(OUT207, 1, FALSE, TRISTATETRUE)
-
- DO UNTIL TS.ATENDOFSTREAM
- FINDHEX46 TS.READLINE
- LOOP
-
- TS.CLOSE
- END SUB
-
-
- SUB RMFILE109(X)
- SET FSO = CREATEOBJECT("SCRIPTING.FILESYSTEMOBJECT")
- FSO.DELETEFILE(X)
- END SUB
-
-
- SUB MAIN4
- OUT207 = "OUDTMP12421.TXT"
- FINDXML28 OUT207
- FINDHEXS OUT207
- RMFILE109 OUT207
- END SUB
-
- MAIN4
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |