Friedrich Rehren
Friedrich Rehren

Reputation: 41

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

At the beginning: I do apologize for my bad English and bad spelling, I'm new in this Forum and if I'm doing something wrong feel free to point it out and I just started programming so I'm what is called a noob ;)

I have a very specific problem. I'm trying to write a Batch-File to install a server for a game and I have to download about three files. For that I'm using the bitsadmin command, but sometimes the download fails and I want the batch to delete this bad file and download it again if it fails.

Here is one of the used code fragments:

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

(without the free lines it looked really messed up)

So I'm trying to write a log of the command and check the log to see if it says transfer complete. But it is not working.

I'm open to any suggestions, but I don't like to use 3rd partner programs.

To see all of the code - I put it on my Pastebin page. http://pastebin.com/w7wLsvkF

Thanks for taking the time just to read this... xD

Upvotes: 0

Views: 135

Answers (1)

JosefZ
JosefZ

Reputation: 30153

Although I can't testify correctness of bitsadmin command line, next commented code snippet could help with log file parsing:

@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

Erratum (mea culpa): please compare 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 ^

Resources (required reading):

Upvotes: 1

Related Questions