在批处理脚本中使用“查找”命令写入文件

马特斯·沃特曼·瓦塔(Mateusz Watman Wata)

我需要Windows执行以下操作的批处理脚本:

  1. 将目录中的所有文件名转储到.txt文件中(通常是几百个,最多50万个文件)
  2. 在输出文件中搜索特定的字符串(其中约35个),对它们进行计数并使用结果创建另一个文件

鉴于我今天之前没有编写脚本,因此提出了以下建议:

@echo off  
dir /b > maps.txt  
(  
find /c /i "string1" maps.txt  
find /c /i "string2" maps.txt  
...  
find /c /i "string35" maps.txt  
)  > results.txt  

结果是有希望的,但是我需要result.txt文件中枚举的字符串以及计数,以便结果文件看起来像这样:

string1 = 3  
string2 = 5  
...  
string35 = 1 

.csv文件也可以,在这种情况下,我需要以下格式:

string1;3  
string2;5  
...  
string35;1  

这样的事情有可能吗?

多喝皮条鱼果汁IT

每个字符串的总字符串数

批处理脚本(隐式)

@ECHO OFF

::: If this file exists, delete it 
IF EXIST "Results.txt" DEL /Q /F "Results.txt"

::: Bare format DIR command listing ONLY filename.extension 
DIR /B > maps.txt

::: Please go to command line and type FOR /? and look through there for the FOR /F explanations
::: This is saying for each line in strings.txt do a FIND /I for each string in maps.txt and if FIND /I finds a string, then CALL the StringCountRoutine and pass the string found as the first argument to the CALL :label (:StringCountRoutine in this instance)
::: Please note that is a string IS NOT FOUND then there will be no count and not a zero unfortunately so it's implied that is the string is not in the results.txt file, then the count of that string is zero
FOR /F "TOKENS=*" %%S IN (Strings.txt) DO (FIND /I "%%S" maps.txt && CALL :StringCountRoutine "%%~S")
::: GOTO EOF needed here to pass control back to the CALLER or END once loop is complete to it doesn't move on to logic beneath which should only be called
GOTO EOF

:StringCountRoutine
::: This is saying the TOKEN count is three and each token to count (the DELIMITER) are colons and spaces ("DELIMS=: ") so for example this (---------- MAPS.TXT: 14) has two spaces and one colon so only have the variable be what's left afterwards which is just the number when set this way
::: The first argument is passed to the FIND /C command as listed below and also the ECHO command afterwards
FOR /F "TOKENS=3DELIMS=: " %%A IN ('FIND /C "%~1" maps.txt') DO (ECHO %~1 = %%A >> Results.txt)
::: GOTO EOF needed here to pass control back to the CALLER or END once loop is complete to it doesn't move on to logic beneath which should only be called
GOTO EOF

在文件中搜索字符串并在另一个文件中找到相同的字符串

以下是两个示例,这些示例显示了一种可以满足您的需求的方法,但是您希望将string保存到单独的文本文件中,其中每个字符串在该文件的每一行中表示。只要TOKENS=*FOR /F行中有,它就会读取每行中有无空格作为string您在map.txt文件中寻找

隐式定义脚本

@ECHO OFF

::: If this file exists, delete it 
IF EXIST "Results.txt" DEL /Q /F "Results.txt"

::: Bare format DIR command listing ONLY filename.extension 
DIR /B > maps.txt

::: Set seq variable to 1 for the first sequence number
SET seq=1

::: Please go to command line and type FOR /? and look through there for the FOR /F explanations
::: This is saying for each line in strings.txt do a FIND /I for each string in maps.txt and if FIND /I finds a string, then CALL the SeqAdditionRoutine and pass the string found as the first argument to the CALL :label (:SeqAdditionRoutine in this instance)
FOR /F "TOKENS=*" %%S IN (Strings.txt) DO (FIND /I "%%S" maps.txt && CALL :SeqAdditionRoutine "%%~S")
::: GOTO EOF needed here to pass control back to the CALLER or END once loop is complete to it doesn't move on to logic beneath which should only be called
GOTO EOF

:SeqAdditionRoutine
::: This is saying FIND /I but with first argument passed as the string (same as above FIND /I but the first argument is passed here), and if successful (the double AND) ECHO the string equals 1 (or the sequence number variable value) to results.txt
FIND /I "%~1" maps.txt && ECHO %~1 = %seq% >> results.txt
::: This is saying (see SET /?) whatever seq variable is set to, ADD one to it and set it to this new value for whatever adding one to it will make it when it goes to EOF, it'll loop the next command (the CALLing loop) with this new value until it is successful in finding a strings and comes back down here again
SET /A seq=%seq%+1
::: GOTO EOF needed here to pass control back to the CALLER or END once loop is complete to it doesn't move on to logic beneath which should only be called
GOTO EOF

明确定义的脚本

@ECHO OFF

SET stringlist=C:\folder\folder\Strings.txt
SET mapsfile=C:\folder\folder\Maps.txt
SET resultsfile=C:\folder\folder\Results.txt

IF EXIST "%resultsfile%" DEL /Q /F "%resultsfile%"

DIR /B > "%mapsfile%"

SET seq=1
FOR /F "TOKENS=*" %%S IN (%stringlist%) DO (FIND /I "%%S" "%mapsfile%" && CALL :SeqAdditionRoutine "%%~S")
GOTO EOF

:SeqAdditionRoutine
FIND /I "%~1" "%mapsfile%" && ECHO %~1 = %seq% >> "%resultsfile%"
SET /A seq=%seq%+1
GOTO EOF

更新

我从隐式脚本对此进行了测试,它可以按预期工作。

我仅获得了string = number用于在中找到匹配的那些字符串的,Strings.txt而没有从maps.txt同一目录中或其他txt文件中找到任何匹配的字符串

我的定义中的字符串Strings.txt的文件确实包含数字,所以用FIND /V我也注意到,string1也符合string10string11在我的例子。我不确定这是否对您来说是个问题,还是不适合您values的搜索字符串值条件,但这可能是您申请时要考虑的问题。我不能肯定是否FINDSTR /LFINDSTR /I /C:"%~1"将是更好与否。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在批处理脚本中使用“FIND”命令时避免打印文件名

来自分类Dev

使用批处理脚本循环写入文件的特定行

来自分类Dev

使用批处理脚本循环写入文件的特定行

来自分类Dev

在批处理脚本中使用查找时出现奇怪的错误

来自分类Dev

在循环批处理脚本(Windows,批处理脚本)的路径中使用findstr命令变量扩展

来自分类Dev

使用批处理命令查找扩展名为csv的文件

来自分类Dev

使用批处理命令查找扩展名为csv的文件

来自分类Dev

在 Windows 批处理脚本中使用 FOR LOOP 创建多个文件

来自分类Dev

在命令行批处理文件中使用变量

来自分类Dev

在批处理文件中使用TaskList命令时挂断

来自分类Dev

在Windows批处理文件中使用PowerShell命令的输出

来自分类Dev

批处理文件:在命令行中使用Loop

来自分类Dev

在批处理文件中使用TaskList命令时挂断

来自分类Dev

如何在批处理文件中使用sed命令

来自分类Dev

如何在批处理文件中使用启动命令

来自分类Dev

如何从批处理文件中使用 Runas 命令运行批处理文件

来自分类Dev

如何在Windows命令行的文件名和批处理脚本中使用日语字符?

来自分类Dev

如何在Windows命令行中的文件名和批处理脚本中使用日语字符?

来自分类Dev

在批处理文件中使用 Echo 将 IF 语句写入文本文件

来自分类Dev

无法使用批处理脚本写入.vbs脚本

来自分类Dev

在/ f批处理命令中使用空格

来自分类Dev

使用批处理脚本每分钟将时间写入文本文件

来自分类Dev

使用批处理脚本,在文本文件中每行的特定列处写入文本

来自分类Dev

如何使用批处理命令在文本文件中写入标准错误

来自分类Dev

批处理命令/脚本以从.txt文件执行命令?

来自分类Dev

批处理脚本-Ping地址-失败时写入文件

来自分类Dev

批处理脚本-将变量的所有内容写入文件

来自分类Dev

批处理脚本-将变量的所有内容写入文件

来自分类Dev

使用批处理文件在目录中查找属性文件,然后调用命令

Related 相关文章

  1. 1

    在批处理脚本中使用“FIND”命令时避免打印文件名

  2. 2

    使用批处理脚本循环写入文件的特定行

  3. 3

    使用批处理脚本循环写入文件的特定行

  4. 4

    在批处理脚本中使用查找时出现奇怪的错误

  5. 5

    在循环批处理脚本(Windows,批处理脚本)的路径中使用findstr命令变量扩展

  6. 6

    使用批处理命令查找扩展名为csv的文件

  7. 7

    使用批处理命令查找扩展名为csv的文件

  8. 8

    在 Windows 批处理脚本中使用 FOR LOOP 创建多个文件

  9. 9

    在命令行批处理文件中使用变量

  10. 10

    在批处理文件中使用TaskList命令时挂断

  11. 11

    在Windows批处理文件中使用PowerShell命令的输出

  12. 12

    批处理文件:在命令行中使用Loop

  13. 13

    在批处理文件中使用TaskList命令时挂断

  14. 14

    如何在批处理文件中使用sed命令

  15. 15

    如何在批处理文件中使用启动命令

  16. 16

    如何从批处理文件中使用 Runas 命令运行批处理文件

  17. 17

    如何在Windows命令行的文件名和批处理脚本中使用日语字符?

  18. 18

    如何在Windows命令行中的文件名和批处理脚本中使用日语字符?

  19. 19

    在批处理文件中使用 Echo 将 IF 语句写入文本文件

  20. 20

    无法使用批处理脚本写入.vbs脚本

  21. 21

    在/ f批处理命令中使用空格

  22. 22

    使用批处理脚本每分钟将时间写入文本文件

  23. 23

    使用批处理脚本,在文本文件中每行的特定列处写入文本

  24. 24

    如何使用批处理命令在文本文件中写入标准错误

  25. 25

    批处理命令/脚本以从.txt文件执行命令?

  26. 26

    批处理脚本-Ping地址-失败时写入文件

  27. 27

    批处理脚本-将变量的所有内容写入文件

  28. 28

    批处理脚本-将变量的所有内容写入文件

  29. 29

    使用批处理文件在目录中查找属性文件,然后调用命令

热门标签

归档