For Loop in (Findstr *.txt) 输出在文件之间串联
For Loop in (Findstr *.txt) Output Concatenated Between Files
我有一个代码可以使用以下代码打印来自多个文件的所有字符串:
for /f %%A in ('findstr /R "[0-9]" *.txt') do echo %%A
然而,当切换到不同的文件时,输出连接在一起:
C:\Users>echoarray.cmd
File1.txt:123
File1.txt:321
File1.txt:312File2.txt:456
File2.txt:654
File2.txt:546File3.txt:789
File3.txt:678
File3.txt:777File4.txt:123
File4.txt:789
File4.txt:999
我想做的是,将每行所有整数的总和(来自 *.txt)放入数组,然后将输出打印到一个文本文件中:
(感谢 Gerhard,从 here 找到了这个)
@echo off & setlocal enabledelayedexpansion
for /f "tokens=1* delims=:" %%a in ('findstr /R "[0-9]" *.txt') do (
set /a %%a+=1
set /a result[!%%a!]+=%%b
echo result[!%%a!] > result.txt
)
示例结果如下(来自上面 echoarray.cmd 的输出):
1491 ::my comment: first row result are from summation of 123+456+789+123
*continue for second row
*continue for third row
*continue for fourth row
我需要你的帮助来解决 findstr 的串联输出(希望有人也能建议我正确的数组解决方案)。
P/S:我在 Windows 10
上使用 cmd
也许这样的东西能满足您的需求。
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F "Delims==" %%G In ('"(Set Line[) 2>NUL"') Do Set "%%G="
Set "}=" & For /F "Tokens=1,* Delims=[]" %%G In ('
%SystemRoot%\System32\find.exe /N /V "" "*.txt" 2^>NUL ^|
%SystemRoot%\System32\findstr.exe /R "^\[[123456789][0123456789]*\][123456789][0123456789]*$"'
) Do Set /A Line[%%G] += %%H, } = %%G
If Not Defined } GoTo :EOF
(For /F "Tokens=1,* Delims==" %%G In ('"(Set Line[) 2>NUL"') Do Echo %%H) 1>"result.txt"
上面的代码使用 find.exe
来获取每个 .txt
文件的内容,不会因为缺少 CRLF
行结尾而出错。然后 findstr.exe
只识别那些只包含一系列数字的行,并将它们传递给循环外的 Do
部分。 Set /A
然后根据行号汇总内容,并仅将这些结果打印到 .txt
文件。
请注意,由于这里使用 Set /A
,因此忽略了首位数字为 0
的所有行。这可以防止八进制运算可能出现的问题。您还应该知道,由于您请求的输出字符串,如果您的任何文件包含一些包含非数字字符的行,则您无法确定哪些总计属于哪些行。
我有一个代码可以使用以下代码打印来自多个文件的所有字符串:
for /f %%A in ('findstr /R "[0-9]" *.txt') do echo %%A
然而,当切换到不同的文件时,输出连接在一起:
C:\Users>echoarray.cmd
File1.txt:123
File1.txt:321
File1.txt:312File2.txt:456
File2.txt:654
File2.txt:546File3.txt:789
File3.txt:678
File3.txt:777File4.txt:123
File4.txt:789
File4.txt:999
我想做的是,将每行所有整数的总和(来自 *.txt)放入数组,然后将输出打印到一个文本文件中:
(感谢 Gerhard,从 here 找到了这个)
@echo off & setlocal enabledelayedexpansion
for /f "tokens=1* delims=:" %%a in ('findstr /R "[0-9]" *.txt') do (
set /a %%a+=1
set /a result[!%%a!]+=%%b
echo result[!%%a!] > result.txt
)
示例结果如下(来自上面 echoarray.cmd 的输出):
1491 ::my comment: first row result are from summation of 123+456+789+123
*continue for second row
*continue for third row
*continue for fourth row
我需要你的帮助来解决 findstr 的串联输出(希望有人也能建议我正确的数组解决方案)。
P/S:我在 Windows 10
上使用 cmd也许这样的东西能满足您的需求。
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F "Delims==" %%G In ('"(Set Line[) 2>NUL"') Do Set "%%G="
Set "}=" & For /F "Tokens=1,* Delims=[]" %%G In ('
%SystemRoot%\System32\find.exe /N /V "" "*.txt" 2^>NUL ^|
%SystemRoot%\System32\findstr.exe /R "^\[[123456789][0123456789]*\][123456789][0123456789]*$"'
) Do Set /A Line[%%G] += %%H, } = %%G
If Not Defined } GoTo :EOF
(For /F "Tokens=1,* Delims==" %%G In ('"(Set Line[) 2>NUL"') Do Echo %%H) 1>"result.txt"
上面的代码使用 find.exe
来获取每个 .txt
文件的内容,不会因为缺少 CRLF
行结尾而出错。然后 findstr.exe
只识别那些只包含一系列数字的行,并将它们传递给循环外的 Do
部分。 Set /A
然后根据行号汇总内容,并仅将这些结果打印到 .txt
文件。
请注意,由于这里使用 Set /A
,因此忽略了首位数字为 0
的所有行。这可以防止八进制运算可能出现的问题。您还应该知道,由于您请求的输出字符串,如果您的任何文件包含一些包含非数字字符的行,则您无法确定哪些总计属于哪些行。