Boinc:一个 Windows 批处理文件,用于使用调度程序恢复挂起的任务

Boinc : a Windows batch file to resume suspended tasks with the scheduler

向 Windows 批处理脚本向导请求一些魔法...:D

For competition purposes, members of my team use BoincTasks to automatically suspend Boinc workunits at 99% completion, thus they can resume them after a starting dateline, sending a lot of results in a short time. Unfortunately, BoincTasks can automatically suspend tasks, but can't resume them automatically. So : in case the user can't stay in front of the computer at a certain date/time to manually resume tasks, it would be nice to have a Windows batch script being able to do the job, triggered by the Windows scheduler.

Boinc 使用 (false).XML 文件来跟踪他管理的任何任务。 通常,您会在这里找到它:C:\ProgramData\BOINC\client_state.xml

这是其中有趣的部分(完整文件为 368k):

<active_task>
   <project_master_url>http://asteroidsathome.net/boinc/</project_master_url>
   <result_name>ps_170405_input_161867_8_1</result_name>
   <active_task_state>1</active_task_state>
   ...
</active_task>

每个任务都有一个 "active_task" 标签。 暂停任务的 "active_task_state" 子标签设置为“0”。

Boinc 有一个命令行可执行文件,可以恢复单个任务,前提是您知道一些参数。这是一个例子:

boinccmd --task [project_master_url] [result_name] resume

所以,我想编写一个批处理脚本,能够从选定的 "project_master_url" 恢复挂起的任务。 这是理想的场景:我想从 "asteroidsathome" Boinc 项目恢复所有任务(参见上面代码中的 url)。

  1. 打开并解析 "C:\ProgramData\BOINC\client_state.xml" ;
  2. 找到所有具有 "project_master_url"=="the project url""active_task_state"==0 的 "active_task" 分支;
  3. 对于每个任务,执行命令行,如上所述。

我很习惯Linux .sh 和PHP 脚本,但我只是不明白如何在Windows 批处理中获得相同的结果。我已经找到了如何使用这个解析 XML : ...但是 "filtering/throwing the extracted data to a command line" 过程仍然是个谜 :(

要施放一些法术?谢谢 :D

好的。我设法做到了......它有效:) 我很确定还有很大的改进空间。 欢迎提出更好的方法

@echo off
setlocal enableextensions EnableDelayedExpansion

rem Settings
rem ==============================================================
rem Defines the url of the project we want the tasks to be resumed
set ResumeProjectUrl=https://wuprop.boinc-af.org/
rem ==============================================================
rem Defines the path to the client_state.xml Boinc file
set ClientStateFile=%ProgramData%\BOINC\client_state.xml
rem Defines the path to the boinccmd.exe
set BoincCmdFile=%ProgramFiles%\BOINC\boinccmd.exe

rem This is real content, extracted from client_state.xml
rem (useful to have a quick reference)
rem    <project_master_url>https://wuprop.boinc-af.org/</project_master_url>
rem    <result_name>data_collect_v4_1490525102_609550_0</result_name>
rem    <active_task_state>1</active_task_state>

rem Find the lines that contains a specific string... "project_master_url" seems OK (1st line in the above example)
for /f "delims=:" %%I in ('findstr.exe /I /L /N /C:"project_master_url" "%ClientStateFile%" 2^>nul') do (

    rem assign variables :
    rem a is the first line number, containing <project_master_url>value</project_master_url>
    rem b is the next line number, containing <result_name>value</result_name>
    rem c is the next line number, containing <active_task_state>value</active_task_state>
    set /a a=%%I
    set /a b=a+1
    set /a c=a+2

    rem As we want to resume only a specific project WUs, let's evaluate the url first
    rem read first line (a)
    for /f "tokens=1* delims=]" %%A in ('^<"%ClientStateFile%" FIND /N /V "" ^| FINDSTR /B /C:"[!a!]"') do (

        rem extract project_master_url value (between <tag></tag>)
        rem %%a is opening tag, %%b is the value, %%c is closing tag
        for /f  "tokens=2-4delims=<>" %%a in ("%%B") do (

            rem Let's compare the project_master_url value and the desired url (in Settings)
            if "%ResumeProjectUrl%"=="%%b" (

                rem If values are the same let's find if <active_task_state> is 0 (suspended)
                rem Read third line (c)
                for /f "tokens=1* delims=]" %%A in ('^<"%ClientStateFile%" FIND /N /V "" ^| FINDSTR /B /C:"[!c!]"') do (

                    rem extract active_task value (between <tag></tag>)
                    rem %%a is opening tag, %%b is the value, %%c is closing tag
                    for /f  "tokens=2-4delims=<>" %%a in ("%%B") do (

                        rem Value is 0 (suspended)
                        if "%%b"=="0" (

                            rem Read second line (b)
                            for /f "tokens=1* delims=]" %%A in ('^<"%ClientStateFile%" FIND /N /V "" ^| FINDSTR /B /C:"[!b!]"') do (

                                rem extract result_name value (between <tag></tag>)
                                rem %%a is opening tag, %%b is the value, %%c is closing tag
                                for /f  "tokens=2-4delims=<>" %%a in ("%%B") do (

                                    rem let's call the boinccmd executable to resume the task
                                    start "Resuming" /B "%BoincCmdFile%" --task %ResumeProjectUrl% %%b resume
                                )
                            )
                        )
rem                     if "%%b" NEQ "0" (
rem                         echo "Good project, but the task is active"
rem                     )
                    )
                )
            )
rem         if "%ResumeProjectUrl%" NEQ "%%b" (
rem             echo "Other project"
rem         )
        )
    )

    rem clear variables
    set a=
    set b=
    set c=

)
endlocal