2016-05-17 21 views
0

開頭:我爲我的糟糕英語和拼寫錯誤表示歉意,我是這個論壇的新成員,如果我做錯了某些事情,可以隨意點它出來,我剛開始編程,所以我就是所謂的菜鳥;)批處理文件:在IF子句中使用以前的輸出

我有一個非常具體的問題。我正在嘗試編寫批處理文件來爲遊戲安裝服務器,並且我必須下載三個文件。爲此,我使用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

+0

非常感謝編輯我的文章! @Deepend(我不知道它是否以這種方式工作) – XaverXor

+0

不用擔心,也不需要感謝我。希望你能得到答案。歡迎來到SO – Deepend

回答

0

雖然我不能作證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 

勘誤過失):請比較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^

資源(必讀):

+0

感謝您的回答!我試過你的代碼(https://www.dropbox.com/s/cag2ogyjx8oyy9d/notepad_2016-05-18_18-01-42.png?dl=0),然後出現錯誤...(https:// www。 dropbox.com/s/txya0bv3tidws7o/explorer_2016-05-18_18-02-07.png?dl=0)向trubleshoot添加超時命令後(https://www.dropbox.com/s/12osv4hz7jmpg3w/notepad_2016-05- 18_18-03-38.png?dl = 0)出現:https://www.dropbox.com/s/hni5bpmm40fpsys/explorer_2016-05-18_18-03-57.png?dl=0正如你所看到的最後一張照片文件完整下載。有什麼建議麼? (抱歉所有的鏈接,但我沒有找到其他的方式) – XaverXor

+0

@XaverXor我請你原諒那個愚蠢的錯誤:-( – JosefZ

+0

它工作完美!非常感謝! – XaverXor

相關問題