我需要Windows执行以下操作的批处理脚本:
鉴于我今天之前没有编写脚本,因此提出了以下建议:
@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
这样的事情有可能吗?
批处理脚本(隐式)
@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
也符合string10
并string11
在我的例子。我不确定这是否对您来说是个问题,还是不适合您values
的搜索字符串值条件,但这可能是您申请时要考虑的问题。我不能肯定是否FINDSTR /L
或FINDSTR /I /C:"%~1"
将是更好与否。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句