批处理文件:在 IF 子句中使用先前的输出

Batch-File: Use a previous output in a IF-clause

开头:我为我糟糕的英语和拼写道歉,我是这个论坛的新人,如果我做错了什么,请随时指出,我刚刚开始编程,所以我什么叫菜鸟 ;)

我有一个非常具体的问题。我正在尝试编写批处理文件来安装游戏服务器,我必须下载大约三个文件。为此,我正在使用 bitsadmin 命令,但有时下载会失败,我希望批处理删除这个坏文件并在失败时重新下载。

这是使用的代码片段之一:

rem Download SteamCMD

:cmd_not_exist

>>%directory%RustServer\temp\log1.txt bitsadmin /transfer Download_SteamCMD 
/download /priority normal https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip %directory%SteamCMD\steamcmd.zip

set "do_cmd_exist="

for /F "skip=8 delims=" %%i in (>>%directory%RustServer\temp\log1.txt) do if not defined do_cmd_exist set "do_cmd_exist=%%i"

if "%do_cmd_exist%" == "Transfer complete." ( 

    goto cmd_exist

) else (

    del %directory%SteamCMD\steamcmd.zip

    del %directory%RustServer\temp\log1.txt

    goto cmd_not_exist

)

:cmd_exist

(没有空闲线看起来真的很乱)

所以我正在尝试编写命令日志并检查日志以查看是否显示传输完成。但它不起作用。

我乐于接受任何建议,但我不喜欢使用第三合作伙伴计划。

要查看所有代码 - 我将其放在我的 Pastebin 页面上。 http://pastebin.com/w7wLsvkF

感谢您花时间阅读本文... xD

虽然我无法证明 bitsadmin 命令行的正确性,但下一个注释代码片段可能有助于日志文件解析:

@ECHO OFF
SETLOCAL EnableExtensions

set "directory=define variable `directory` with no double quotes here" 

rem for better readability and to avoid typo mistakes
set "_log_file=%directory%RustServer\temp\log1.txt"
set "_zip_file=%directory%SteamCMD\steamcmd.zip"
set "_switches=/transfer Download_SteamCMD /download /priority normal"
set "_zip__URL=https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip"

rem Download SteamCMD

:cmd_not_exist
rem let user watch batch progress
echo %date% %time% attempt to download
>"%_log_file%" bitsadmin %_switches% %_zip__URL% "%_zip_file%"

set "do_cmd_exist="
for /F "tokens=*" %%i in ( 'findstr /i /C:"Transfer complete." "%_log_file%"'
                         ) do if not defined do_cmd_exist set "do_cmd_exist=%%i"

if defined do_cmd_exist (
    rem let user watch batch progress
    echo %date% %time% %do_cmd_exist%
    goto cmd_exist
) else (
    del "%_zip_file%"
    del "%_log_file%"
    goto cmd_not_exist
)
:cmd_exist

勘误表 (mea culpa): 请比较 for /F Loop command: against the results of another command.

for /F "tokens=*" %%i in ( 'findstr /i /C:"Transfer complete." "%_log_file%"'
rem forgot apostrophes     ^ here                                  and here ^

资源(必读):