在我的多个文件的每一行上进行批量执行命令?

在我的多个文件的每一行上进行批量执行命令?

我有以下球棒:

echo >> "log.txt"
setlocal
for /f %%i in (NAMES.txt) do (
  for /f %%j in (HOST.txt) do (

...entire batch processing

    ) >> "log.txt"
  ) >> "log.txt"
endlocal

NAMES 包含如下内容:

NAMEA
NAMEB
NAMEC

并且 HOST 包含如下内容:

HOST1
HOST2
HOST3

最终结果应该是:

NAMEA - HOST1
NAMEB - HOST2
NAMEC - HOST3

但实际的结果是:

NAMEA - HOST1
NAMEB - HOST1
NAMEC - HOST1

我认为这与那些行有关,因为对于 NAMES 中的每一行,它都会再次读取 HOST,并且只获取第一行,但我不知道如何解决它?在 NAMES 的第二行,它还需要读取 HOSTS 文件的第二行。

for /f %%i in (NAMES.txt) do (
  for /f %%j in (HOST.txt) do (
    )
  )

答案1

以下建议是从每个文件中获取相同的行号!

  • 将每一行保存在变量中(“可递增“)。

  • 对循环中的每个文件分别执行此操作。

@echo off && setlocal EnableDelayedExpansion
for /f %%i in (.\NAMES.txt)do set /a "_N+=1" && call set "_i!_N!=%%~i"
for /f %%j in (.\HOST.txt)do set /a "_H+=1" && call set "_j!_H!=%%~j"
for /l %%L in ( 1 1 !_H!)do echo\!_i%%L! - !_j%%L!
endlocal

或者,再写一个更易读的:

@echo off

setlocal EnableDelayedExpansion

for /f %%i in (.\NAMES.txt)do ( 
     set /a "_N+=1"
     set "_i!_N!=%%~i"
    )
    
for /f %%j in (.\HOST.txt)do (
     set /a "_H+=1"
     set "_j!_H!=%%~j"
    )

for /l %%L in ( 1 1 !_H!)do echo\!_i%%L! !_j%%L!

endlocal

  • 在您的代码中添加:
@echo off

setlocal EnableDelayedExpansion

for /f %%i in (.\NAMES.txt)do ( 
     set /a "_N+=1"
     set "_i!_N!=%%~i"
    )
    
for /f %%j in (.\HOST.txt)do (
     set /a "_H+=1"
     set "_j!_H!=%%~j"
    )

for /f %%i in (NAMES.txt) do for /f %%j in (HOST.txt) do (
     set /a "_cnt+=1"
     rem ...entire batch processing 
     
     rem ...for echo each line in a file in doble loop for /f... 
     for %%G in (!_cnt!) do >>".\log.txt" (
           findstr /c:"!_i%%~G! - !_j%%~G!" ".\log.txt" >nul || echo=!_i%%~G! - !_j%%~G! 
          ) 2>nul
     )
 
rem ...for echo each line in your screen... 
for /l %%L in ( 1 1 !_H!)do echo\!_i%%L! !_j%%L!

endlocal

其他资源:

相关内容