我想编写一个批处理脚本,它将解析特定位置的.xml文件,并输出xml文件中特定标记的总数。但是在这样做的时候,我在文件夹中有很多.xml文件,但是我想只解析那些文件名中没有字符串“DEL”的.xml文件。
例如,假设我在文件夹中有以下xml文件列表:
abc.xml
pwrdt.xml
terwyw.xml
drDELyt.xml
yrte.xml
uyteDEL.xml
DELytety.xml
ahdDELwe.xml
我想编写一个批处理脚本,它只解析上面列表中那些文件名中不包含字符串DEL
的.xml文件。
所以我只想解析,
abc.xml
pwrdt.xml
terwyw.xml
yrte.xml
@echo off
findstr /ip /c:"/ORDNUM" C:\Users\mypath\Desktop\folder\*.xml >> log-it.txt
在上面的语句中,我想只在那些文件名中不包含“DEL”的.xml文件中搜索/ORDNUM
以下是我现在使用的脚本:
@Echo off
(for /f "delims=" %%F in ('Dir /B "C:\Users\soumya.kanti.dey\Desktop\Splunk\*.xml" ^| Findstr /v "DEL" ') do (
Echo Processing file %%F
findstr /ip /c:"/ORDNUM" "%%F"
)) > log-it.txt
for /f "delims=: tokens=2" %%C in ('find /C "/ORDNUM" log-it.txt') Do Set /A "Count=%%C"
echo %count% > "C:\Users\soumya.kanti.dey\Desktop\total.txt"
使用通配符作为findstr的输入,你不能排除任何文件,你需要一个for / f来解析由另一个find / findstr / v过滤的dir输出,只处理想要的xml文件。
@Echo off
for /f "delims=" %%F in ('Dir /B "C:\Users\mypath\Desktop\folder\*.xml" ^| Findstr /v "DEL" ') do findstr /ip /c:"/ORDNUM" "%%F" >> log-it.txt
由于findstr在处理您可能使用的单个文件时不输出文件名
@Echo off
(for /f "delims=" %%F in ('Dir /B "C:\Users\mypath\Desktop\folder\*.xml" ^| Findstr /v "DEL" ') do (
Echo Processing file %%F
findstr /ip /c:"/ORDNUM" "%%F"
)) > log-it.txt
将qazxsw poi更改为qazxsw poi以在每次运行中创建文件。
要保存文件>>
中的匹配数,请使用(追加)
>
这是一个简单的log-it.txt
尝试此任务的示例。它假设你对包含for /f "delims=: tokens=2" %%C in ('find /C "/ORDNUM" log-it.txt') Do Set /A "Count=%%C"
echo %count% > "C:\Users\mypath\Desktop\total.txt"
的行的数量感到满意,而不是它的实例数(不一定相同)。
batch-file
如果/ORDNUMBER
的内容没有显示您想要的内容,请您更好地解释您的整体任务。
如果你只想要所有文件的总数,而不需要识别与每个文件相关联的单个文件名,你可以试试这个(它不使用任何@>"%UserProfile%\Desktop\total.txt" ( For /F Delims^=^ EOL^= %%A In (
'FindStr /IMP "\/ORDNUMBER" *.xml 2^>Nul^|Find /V "DEL"'
)Do @For /F Tokens^=1* %%B In ('Find /C "/ORDNUMBER" "%%A"')Do @Echo(%%C)
循环)。
total.txt