Batch file 批处理文件以逐行分析文本文件,然后检查该行是否存在于其他文本文件中
这是我的第一篇帖子,请求允许在这里寻求帮助 我对批处理文件的知识有限。我有一个脚本,它使用FOR/f读取每行一个单词的文本文件,然后将该单词用作我的变量 问题是我想使用该变量(引用文件中的每一行)来检查该变量是否存在于所有目标文本文件中,现在我不知道如何为此创建批处理文件。 我花了时间寻找答案,但找不到任何答案,如果StackOverflow中有类似的批处理文件,请给我链接 目标:从参考文件packageList.txt中计算有多少台电脑具有软件包。我已经收集了所有电脑上的软件包列表,并将它们保存为PC1_bare.txt、PC2_bare.txt、PC3_bare.txt等等 例如: 我的文本文件是 packageList.txtBatch file 批处理文件以逐行分析文本文件,然后检查该行是否存在于其他文本文件中,batch-file,for-loop,Batch File,For Loop,这是我的第一篇帖子,请求允许在这里寻求帮助 我对批处理文件的知识有限。我有一个脚本,它使用FOR/f读取每行一个单词的文本文件,然后将该单词用作我的变量 问题是我想使用该变量(引用文件中的每一行)来检查该变量是否存在于所有目标文本文件中,现在我不知道如何为此创建批处理文件。 我花了时间寻找答案,但找不到任何答案,如果StackOverflow中有类似的批处理文件,请给我链接 目标:从参考文件packageList.txt中计算有多少台电脑具有软件包。我已经收集了所有电脑上的软件包列表,并将它们保
Acrobat
Chrome
Flash
Photoshop
msOffice
PC1\u bare.txt的内容:
Acrobat
Chrome
Flash
Photoshop
msOffice
Acrobat
Chrome
Flash
msOffice
Acrobat
Flash
msOffice
PC1_bare.txt
PC2_bare.txt
PC3_bare.txt
Acrobat is installed to 3 PC
Chrome is installed to 2 PC
Flash is installed to 3 PC
Photoshop is installed to 1 PC
msOffice is installed to 3 PC
PC2_bare.txt的内容:
Acrobat
Chrome
Flash
Photoshop
msOffice
Acrobat
Chrome
Flash
msOffice
Acrobat
Flash
msOffice
PC1_bare.txt
PC2_bare.txt
PC3_bare.txt
Acrobat is installed to 3 PC
Chrome is installed to 2 PC
Flash is installed to 3 PC
Photoshop is installed to 1 PC
msOffice is installed to 3 PC
PC3_bare.txt的内容:
Acrobat
Chrome
Flash
Photoshop
msOffice
Acrobat
Chrome
Flash
msOffice
Acrobat
Flash
msOffice
PC1_bare.txt
PC2_bare.txt
PC3_bare.txt
Acrobat is installed to 3 PC
Chrome is installed to 2 PC
Flash is installed to 3 PC
Photoshop is installed to 1 PC
msOffice is installed to 3 PC
pcList的内容:
Acrobat
Chrome
Flash
Photoshop
msOffice
Acrobat
Chrome
Flash
msOffice
Acrobat
Flash
msOffice
PC1_bare.txt
PC2_bare.txt
PC3_bare.txt
Acrobat is installed to 3 PC
Chrome is installed to 2 PC
Flash is installed to 3 PC
Photoshop is installed to 1 PC
msOffice is installed to 3 PC
预期结果:
Acrobat
Chrome
Flash
Photoshop
msOffice
Acrobat
Chrome
Flash
msOffice
Acrobat
Flash
msOffice
PC1_bare.txt
PC2_bare.txt
PC3_bare.txt
Acrobat is installed to 3 PC
Chrome is installed to 2 PC
Flash is installed to 3 PC
Photoshop is installed to 1 PC
msOffice is installed to 3 PC
这应该能奏效-
for /f "delims=" %%a in (packageList.txt) do (
set count=0
for /f %%b in (pcList) do for /f %%c in (%%b) do if "%%c"=="%%a" set /a count+=1
echo %%a is installed to !count! PCs
)
@ECHO关闭
SETLOCAL
::制作一个临时文件
:maketemp
设置“tempfile=%temp%\%random%”
如果存在“%tempfile%*”(转到maketemp)ELSE(ECHO.>%tempfile%a)
对于/f“delims=”(pclist.txt)中的%%p DO(>>%tempfile%a“类型”%%p”)
对于(packagelist.txt)中的/f“delims=“%%p”DO(
对于/f“delims=“%%c IN(“%findstr/i/b/e/L/c:%%p”“%tempfile%a”“^ find/c/v”“”)DO echo%%p已安装到%%c PC
)
删除/Q“%tempfile%a”
后藤:EOF
创建一个临时文件。将找到的文件列表连接到该文件中。
对于每个包,将packagename作为
/L
(文字)查找/b
和/e
(开始和结束)一行,/i
忽略大小写/c:
“空格可能出现在packagename中”,然后查找和计数(/c)不为空的数(/v“)生成并显示此计数以及packagename的行
(也适用于“安装在0台电脑上”)来这里之前您尝试过什么?请包含您已经拥有的任何代码。我在这行中遇到了麻烦,不知道如何解析两个文本文件。对于(%~dp0packageList.txt)中的/F%%I,请拨打电话:检查文件%%I感谢您的及时回复。我正在。。。msOffice已安装到!计数个人电脑不知道如何使它像_msOffice安装在3台电脑上,不用担心,已成功修复。必须在Begging中添加一个setlocal enableDelayedExpansion,非常感谢unclemeat@htan68很抱歉,是的,这就是我们所需要的。谢谢你,马古,这个也行。如果需要0计数,我需要这个。