批处理文件逐行解析文本文件,然后检查该行是否存在于其他文本文件中

时间:2014-02-10 03:33:47

标签: batch-file for-loop

这是我的第一篇文章,请求允许在这里寻求帮助。

我对批处理文件知之甚少。我有一个使用FOR / f的脚本,它读取每行一个单词的文本文件,然后使用该单词作为我的变量。

问题是我想使用该变量(参考文件中的每一行)来检查所有目标文本文件中是否存在该变量,现在我不知道如何为此目的创建批处理文件。 我花时间搜索答案但找不到任何答案,如果StackOverflow中有类似的批处理文件,请给我链接。

目标:计算有多少PC从参考文件packageList.txt中获取包。我已经在所有PC上收集了包列表,并将它们保存为PC1_bare.txt,PC2_bare.txt,PC3_bare.txt等等......

示例:

我的文字文件是......

packageList.txt

Acrobat

Chrome

Flash

Photoshop

msOffice

PC1_bare.txt的内容:

Acrobat

Chrome

Flash

Photoshop

msOffice

PC2_bare.txt的内容:

Acrobat

Chrome

Flash

msOffice

PC3_bare.txt的内容:

Acrobat

Flash

msOffice

pcList的内容:

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

2 个答案:

答案 0 :(得分:1)

这应该可以解决问题 -

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
)

答案 1 :(得分:0)

@ECHO OFF
SETLOCAL

:: make a tempfile
:maketemp
SET "tempfile=%temp%\%random%"
IF EXIST "%tempfile%*" (GOTO maketemp) ELSE (ECHO.>"%tempfile%a")

FOR /f "delims=" %%p IN (pclist.txt) DO (>>"%tempfile%a" TYPE "%%p")
FOR /f "delims=" %%p IN (packagelist.txt) DO (
 FOR /f "delims=" %%c IN ('findstr /i /b /e /L /c:"%%p" "%tempfile%a"^|find /c /v ""') DO echo %%p is installed to %%c PC
)
DEL /Q "%tempfile%a"

GOTO :EOF

制作临时档案。
将找到的文件列表连接到该文件中 对于每个包,请将包名称设为/L(字面值)/b/e(开始和结束)一行,/i忽略大小写/c: “空格可能会显示在包名称中”,然后是FINDcount(/ c)生成的not empty(/ v“”)行数,并显示此计数以及包名称。

(也适用于“安装在0台电脑上”)