2017-12-29 30 views
0

ループコードが実行される特定のプロセスがシステム上で開いている間に、特定のコードセットをループするバッチスクリプトを作成したい30分間隔で。バッチファイル - 特定のプロセスが開いている間の間隔ループ命令

このコードは、ゲームが一定の時間間隔で実行されている間にゲームのデータをバックアップすることを目的としています。

これは私が私によって実行されるコードです。最後に保存したデータをバックアップし、ゲームを起動します。

set currDate=%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2% 

xcopy /s /i "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Saved\SavedArksLocal" "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Save Backups\%currDate%\SavedArksLocal" 
xcopy /s /i "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Saved\LocalProfiles" "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Save Backups\%currDate%\LocalProfiles" 

start "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Binaries\Win64" ShooterGame.exe 

私はゲームはまだ私のコードに示すように、ファイルを保存するために実行し、30分間隔でいる間にチェックしたいと思います。前もって感謝します。

答えて

0

まず、あなたはタスクが「タスクリスト」コマンド(カウントダウンのためにもtimeoutコマンド)を使用して開かれているかどうかを確認する必要があります場合は

@echo off 
setlocal ENABLEDELAYEDEXPANSION 

timeout /t 1800 
rem Mean '1800' secs is 30 mins 

set isOpened=false 
set process=[enter your process name in here] 

For /f "tokens=* skip=3" %%a in ('tasklist') do (
    set dip=%%a 
    set dip=!dip:~0,29! 
    set dip=!dip: =! 
    if "!dip!"=="!process!" set isOpened=true 
) 

「isOpened」変数は、「真」を返しますプロセス '%process%'はまだ開かれています。

さて、あなたはゲームが、それは(自動的に)終了します他に、保存されます実行されているかどうかをチェックするためのコードを追加する必要があります。

if "!isOpened!"=="true" (
    rem Here is where you put your code when the process still running: 
    set currDate=%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2% 

    xcopy /s /i "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Saved\SavedArksLocal" "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Save Backups\%currDate%\SavedArksLocal" 
    xcopy /s /i "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Saved\LocalProfiles" "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Save Backups\%currDate%\LocalProfiles" 

    start "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Binaries\Win64" ShooterGame.exe 
) 
if "!isOpened!"=="false" (
    rem Now insert code if process is not running: (only 'goto :eof') 
    goto :eof 
) 

今、あなたが行われ、ここでは、ファイル全体である(uがしたい場合)コピーする:

@echo off 
setlocal ENABLEDELAYEDEXPANSION 

timeout /t 1800 
rem Mean '1800' secs is 30 mins 

set isOpened=false 
set process=[enter your process name in here] 

For /f "tokens=* skip=3" %%a in ('tasklist') do (
    set dip=%%a 
    set dip=!dip:~0,29! 
    set dip=!dip: =! 
    if "!dip!"=="!process!" set isOpened=true 
) 
    if "!isOpened!"=="true" (
    rem Here is where you put your code when the process still running: 
    set currDate=%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2% 

    xcopy /s /i "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Saved\SavedArksLocal" "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Save Backups\%currDate%\SavedArksLocal" 
    xcopy /s /i "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Saved\LocalProfiles" "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Save Backups\%currDate%\LocalProfiles" 

    start "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Binaries\Win64" ShooterGame.exe 
) 
if "!isOpened!"=="false" (
    rem Now insert code if process is not running: (only 'goto :eof') 
    goto :eof 
) 

注:をあなたのプロセス名など)、ファイルの先頭に(「[ここにあなたのプロセス名を入力してください]」テキストを置換することを確認します!

+0

あなたの答えは、たくさん助け、私は私の問題を解決するためにあなたの答えを使用して、それを修正しました。どうもありがとう。投稿した回答を見る – KCB4Rockstar

0

私は前の答えを使って解決しました。私はコードが実行されている間にコードが実行されているので、:loopタグを置き、すべてのループの開始時に変数値をリセットしました。タイムアウトを15分に変更しました。このコードは私の問題を解決します。私はタイムアウトとどのようにforループやif文を使用する方法については知らなかったので、助けてくれてありがとうは

@echo off 
setlocal ENABLEDELAYEDEXPANSION 

start "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Binaries\Win64" ShooterGame.exe 

:loop 
set isOpened=false 
set process=ShooterGame.exe 

set currDate=%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2% 

timeout /t 900 
rem Mean '1800' secs is 30 mins 

For /f "tokens=* skip=3" %%a in ('tasklist') do (
    set dip=%%a 
    set dip=!dip:~0,29! 
    set dip=!dip: =! 
    if "!dip!"=="!process!" set isOpened=true 
) 
if "!isOpened!"=="true" (
    rem Here is where you put your code when the process still running: 
    set currDate=%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2% 
    xcopy /s /i "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Saved\SavedArksLocal" "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Save Backups\%currDate% - auto\SavedArksLocal" 
    xcopy /s /i "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Saved\LocalProfiles" "D:\SteamLibrary\steamapps\common\ARK\ShooterGame\Save Backups\%currDate% - auto\LocalProfiles" 

    goto :loop 
) 
if "!isOpened!"=="false" (
    rem Now insert code if process is not running: (only 'goto :eof') 
    goto :eof 
    exit 
) 
関連する問題