因此,我想创建一个批处理脚本来循环一组特定的代码,同时在系统上打开某个进程,其中循环代码以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分钟的间隔检查以保存文件,如我的代码所示。提前致谢。
首先,您必须使用'tasklist'命令(也是倒计时的超时命令)检查任务是否打开:
@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
)
如果仍然打开进程'%process%','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
)
现在你完成了,这是整个文件(如果你想复制):
@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
)
注意:确保将文本'[在此处输入您的进程名称]'(在文件顶部)替换为您的进程名称!
我用前面的答案解决了它。我希望代码在进程启动时继续运行,所以我将:loop
标记放在每个循环开始时重置变量值。我还将超时更改为15分钟。这段代码解决了我的问题。谢谢您的帮助
@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
)