Question

I'm currently emulating ps2 games on my pc and wanted to have an insurance for loss of Savedata. So in pcsx2 you Save by pressing F1 (and F2 switches to the next slot)

This snippet is a part of a bigger file for ps2 purposes (I can post it if necessary) but what it should basically achieve is to check if pcsx2.exe is running every 0.1 seconds and save the game every 3 minutes.
When I run this code the Batch script quits immediately without the possibility to eveen see an error message. Can anybody tell me what could be wrong with this, or must the problem lie somewhere else?

SET /a time=0

:loop

if %time%==180(
     nircmd.exe sendkey F1 press 
     nircmd.exe sendkey F2 press
        SET /a time=0

)

    tasklist /FI "IMAGENAME eq pcsx2-r5875.exe" 2>NUL | find /I /N "pcsx2r5875.exe">NUL
    nircmd.exe wait 100
    set /a time+=1


    if "%ERRORLEVEL%"=="0" goto loop

EDIT: Complete working script for anyone interested

@ echo off
nircmd.exe cmdwait 10 nircmd.exe sendkey 0x5B down 
nircmd.exe cmdwait 10 nircmd.exe sendkey D press
nircmd.exe cmdwait 10 nircmd.exe sendkey 0x5B up
::simulates windows+D to close all windows

taskkill /f /im explorer.exe 
::kills explorer
start /d "C:\Program Files (x86)\Google\Drive" googledrivesync.exe
::Google Drive to backup save files
start /d "C:\Program Files\MotioninJoy\ds3" DS3_Tool.exe
::DS3 Tool to play with wireless playstation controller
start %windir%\System32\DisplaySwitch.exe /internal
::switches to Single-Display mode

start /d "C:\Program Files (x86)\PCSX2 1.2.1" pcsx2-r5875.exe
::starts emulator

SET timeloop=0
goto loop2
:loop
    SET timeloop=0
    nircmd.exe sendkey F1 press 
    nircmd.exe sendkey F2 press
    echo Saved at %time%
  :loop2
      nircmd.exe wait 100
      set /a timeloop+=1
      tasklist /FI "IMAGENAME eq pcsx2-r5875.exe" 2>NUL | find /I /N "pcsx2-r5875.exe">NUL || goto EndOfLoop
      if %timeloop% LSS 1800 goto :loop2
goto loop
:EndOfLoop
::loop that autosaves to a different slot every 3 minutes and exits if emulator exits

start %windir%\System32\DisplaySwitch.exe /extend
taskkill /f /im googledrivesync.exe
taskkill /f /im explorer.exe
taskkill /f /im DS3_Tool.exe
start explorer.exe
::finally everything is cleaned up

Thanks for the help!

Was it helpful?

Solution

Since you're only showing us part of yor batch, it's difficult to tell, but

if %time%==180(

is syntactically incorrect ton two counts.

First, there must be a space between the 180 and the (

Second, time is a magic variable which returns the current system time in the format currently selected by the user. In my case, this is hh:mm:ss:ms - which is most unlikely to match 180. Once you set a user environment variable named time, the user variable overrides the magic variable. (this is nore of a by-the-by, but really not a good idea generally)

likely fixing these would cure your problem - and inserting a pause command strategically would allow you to see error messages (like just after the if line ... of course, you have to be able to guess where strategically is, and that comes with practice...)

OTHER TIPS

This should work - and exit when the executable is closed and the loop is done.

Don't use a system variable as a normal variable, like time date username etc.

:loop
    SET timeloop=0
    tasklist /FI "IMAGENAME eq pcsx2-r5875.exe" 2>NUL | find /I /N "pcsx2r5875.exe">NUL || goto :EOF
    nircmd.exe sendkey F1 press 
    nircmd.exe sendkey F2 press
  :loop2
      nircmd.exe wait 100
      set /a timeloop+=1
      if %timeloop% LSS 180 goto :loop2
goto loop
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top