【问题标题】:Batch scripting: read lines and execute a command after every other line read批处理脚本:读取行并在每隔一行读取后执行一个命令
【发布时间】:2016-04-10 16:30:51
【问题描述】:

假设我有一个这样的文本文件

node1.log    
node2.log
node3.log 
node4.log etc...

我想逐行读取并执行

alan.exe node1.log node2.log
alan.exe node3.log node4.log

等等。

最好的方法是什么?

【问题讨论】:

  • 请注意DOS是80/90年代的操作系统!请改用标签 Windows。

标签: batch-file dos


【解决方案1】:
@echo off
setlocal EnableDelayedExpansion

for /F "delims=" %%a in (file.txt) do (
   if not defined line (
      set "line=%%a"
   ) else (
      ECHO alan.exe !line! %%a
      set "line="
   )
)

【讨论】:

    【解决方案2】:

    像这样:

    @echo off
    
    setlocal enabledelayedexpansion
    set args=
    set n=2
    for /f "tokens=*" %%x in (file.txt) do (
        set args=!args! %%x
        set /a n -= 1
        if !n! EQU 0 (
            alan !args!
            set args=
            set n=2
        )
    )
    

    【讨论】:

      【解决方案3】:

      试试这个。 File.txt 将包含您的日志文件名。

      @echo off
      setlocal enabledelayedexpansion
      
      FOR /F "delims=" %%G IN ('find /c /v "" ^<file.txt') DO SET NUM=%%G
      
      SET /A NUM=NUM / 2
      
      < file.txt (
          FOR /L %%G IN (1,1,%NUM%) DO (
              SET /P LINE1=
              SET /P LINE2=
              echo mypgm.exe !LINE1! !LINE2!
          )
      )
      pause
      

      输出

      mypgm.exe node1.log node2.log
      mypgm.exe node3.log node4.log
      mypgm.exe node5.log node6.log
      Press any key to continue . . .
      

      去掉echo这个词,把mypgm.exe替换成你要运行的程序。回声只是为了证明概念。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-02
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-08
        • 2011-11-17
        相关资源
        最近更新 更多