运行带有循环和条件的cmd .bat文件

时间:2017-12-05 23:43:34

标签: windows batch-file cmd scrapy scrapy-spider

您好我正在尝试使用cmd批处理文件中的start命令运行scrapy spider。我想从一个包含500多个名字的csv文件中并行运行10个名字的蜘蛛。所以我的想法是基本上在1到500的循环中添加一些条件来检查10个命令窗口是否已关闭,如果没有则等待它们关闭(它们在蜘蛛完成后自动关闭)。一旦打开的10个窗户关闭,打开下一个10,依此类推。以下是我的代码,我很确定它有很大的语法错误。你能帮我调试吗?感谢

cd /d "C:\Users\xyz"
for /f "tokens=1,*" %%m in ('tasklist ^| find /I /C "conhost.exe"') do (set var1=%%m)
set counter=1
for /f "usebackq tokens=1 delims=," %%n in ("test.csv") do (
    (START /MIN "" scrapy crawl xyz_scraper -a query="%%n" -a pages=20) 
    set /a counter=counter+1
    for /f "tokens=1,*" %%p in ('tasklist ^| find /I /C "conhost.exe"') do (set var2=%%p)
    SET /A _result=counter%%10
    echo %_result% 
    IF _result EQU 0 (
        :abcd 
        timeout /t 10
        if var2 EQU var1 (
            goto bcde
            ) 
            ELSE (
            goto abcd)
        )
    :bcde
    )
pause

编辑:删除for循环一。根据一些建议编辑了上面的代码。我不明白我在哪里使用变量引号和不在哪里以及如何将变量值打印到cmd。

以下更新的代码:

    cd /d "C:\Users\sodhian\sodhi-scraper"
    for /f "tokens=1,*" %%m in ('tasklist ^| find /I /C "conhost.exe"') do (set var1=%%m)
    echo %var1%
    set counter=1
    for /f "usebackq tokens=1 delims=," %%n in ("test comp.csv") do (
        (START /MIN "" scrapy crawl ind_scraper -a query="%%n" -a pages=20) 
        set /a "counter=counter+1"
        echo %counter%
        SET /A _result="counter%%2"
        echo %_result%
        IF "%_result%" EQU "0" (
            :abcd 
            timeout /t 10
            for /f "tokens=1,*" %%p in ('tasklist ^| find /I /C "conhost.exe"') do (set var2=%%p)
            echo %var2%
            if var2==var1 (
                goto bcde
                ) 
                ELSE (
                goto abcd)
            )
        :bcde
        )
    pause

编辑2: 根据斯蒂芬的回答。试图完成我在答案评论中提到的内容:

    setlocal enabledelayedexpansion

    set counter=0
    for /f "usebackq tokens=1 delims=," %%n in ("test comp.csv") do (
      set /a counter+=1
      start /MIN "MySpider!counter!" scrapy crawl ind_scraper -a query="%%n" -a pages=20
      for /f "tokens=1,*" %%b in ('tasklist /v ^| find /I /C "MySpider"') do (set var1=%%b)
      if !var1! geq 5 call :wait
    )
    :wait
    timeout /t 5
    for /f "tokens=1,*" %%p in ('tasklist /v ^| find /I /C "MySpider"') do (set var2=%%p)
    if !var2! geq 5 call :wait
    goto :eof 

将其更改为以下内容:( / v(任务列表中的详细选项使上述速度变慢)

setlocal enabledelayedexpansion
set counter=0
set max_scrappers=7
for /f "tokens=1,*" %%a in ('tasklist ^| find /C "conhost"') do (set var1=%%a)
for /f "usebackq tokens=1 delims=," %%n in ("test comp.csv") do (
    set /a counter+=1   
    start /min "MySpider!counter!" scrapy crawl ind_scraper -a query="%%n" -a pages=20
    for /f "tokens=1,*" %%b in ('tasklist ^| find /C "conhost"') do (set var2=%%b)
    set /a var3=!var2!-!var1!
    if !var3! geq !max_scrappers! call :wait
)
:wait
for /f "tokens=1,*" %%p in ('tasklist ^| find /C "conhost"') do (set var4=%%p)
set /a var5=!var4!-!var1!
if !var5! geq !max_scrappers! call :wait
goto :eof 

1 个答案:

答案 0 :(得分:1)

正如评论中已经提到的,代码块内的标签不起作用。但你可以调用一个“函数”,其中goto和标签没问题:

我选择了其他数字和另一个命令,使其适用于每个系统(并且更快)。用蜘蛛替换timeout命令并调整数字应该没问题。

@echo off
setlocal enabledelayedexpansion

REM next line just for generating a testfile:
>"test comp.csv" (for /l %%i in (1,1,10) do echo !random:~-1!)

set counter=0
for /f "usebackq tokens=1 delims=," %%n in ("test comp.csv") do (
  set /a counter+=1
  start "MySpider!counter!" timeout %%n
  if !counter! geq 3 call :wait
)
:wait
tasklist /v|find "MySpider">nul && goto :wait
set counter=0
goto :eof 

搜索conhost不是一个好主意,因为可能还有其他进程。选择一个唯一的窗口标题(在我的示例中为MySpider)并查找该标题(tasklist /v