Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 批处理文件-特定进程打开时的间隔循环指令_Windows_Loops_Batch File_Backup_Xcopy - Fatal编程技术网

Windows 批处理文件-特定进程打开时的间隔循环指令

Windows 批处理文件-特定进程打开时的间隔循环指令,windows,loops,batch-file,backup,xcopy,Windows,Loops,Batch File,Backup,Xcopy,因此,我想创建一个批处理脚本来循环一组特定的代码,同时在系统上打开某个进程,循环代码以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

因此,我想创建一个批处理脚本来循环一组特定的代码,同时在系统上打开某个进程,循环代码以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分钟保存文件,如代码所示。提前感谢。

首先,您必须使用“任务列表”命令(也是用于倒计时的超时命令)检查任务是否打开:

如果进程“%process%”仍处于打开状态,“isOpened”变量将返回“true”。

现在,您必须添加代码来检查游戏是否正在运行,它将保存,否则它将退出(自动):

现在您完成了,下面是整个文件(如果您想复制):


注意:确保将文本“[在此处输入流程名称]”(在文件顶部)替换为流程名称

我用前面的答案解决了这个问题。我希望代码在进程启动时保持运行,因此我放置了
: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
)

你的答案帮助很大,因为我不知道超时以及如何使用for循环和if语句,但我使用了你的答案并修改了它来解决我的问题。谢谢。看到我贴的答案了吗
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
)
@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
)