|
Answer» Hi all:)
I am new (today) to machine script. This is something I put together and will be trying tomorrow.
cd /d "C:\path" Start Filename.bat
cd /d "H:\path" Start Program.exe
:CHECK ping -n 10 localhost 1> NUL TASKLIST /v /fi "IMAGENAME eq program.exe" 2>&1 > NUL IF ERRORLEVEL 1 GOTO CHECK
taskkill /f /im cmd.exe /t
If all goes well, filename.bat will run. Then a program will run. CMD will then check for program.exe until not found (exited), then close CMD.
In this case program.exe does not work with start /wait
I am wondering if the check will slow down my machine if run for a few hours.
I am using Windows 7 x64, SSD, 16GB RAM and AMD 8350.
Considering inserting a small timeout, but seems silly.
Is there a way to slow down the check, but still close immediately if program.exe is closed? Other alternatives? Thanks BATCH is batch. It was not meant for real time event trapping. Perhaps you should first explain what your objective is. Programs the MONITOR the progress of other programs a re a special class themselves. Generally outside of the scope of batch programming.
But maybe there is some kind of need yuo have for a batch file to STOP a program that is running too long. Please explain. Thanks for responding With some more effort this was accomplished and it's working. Very excited as it's my first script ever! But maybe it can be improved?
Works as FOLLOWS:
Launches Fallout4Launcher.exe (this opens through Steam.exe FIRST by design)
A 1 second loop cycles displaying Tasklist info on Fallout4Launcher.exe [shows that program hasn't started yet] Once found it displays tasklist info on Fallout4Launcher.exe and ends the loop [pointless but cool to see] [This block is used to delay the script for Steam/Fallout4 to finish launching and prevent skipping the second loop which would end the batch]
It then starts an xcopy loop, copying new save files from Fallout4 This loops every 8 seconds until Fallout4 is closed
Steam.exe is then forcefully terminated [has to be /f]
Countdown from 5 before closing
And here's the script
@echo off
title "Fallout 4" color 0A
cd /d "H:\SteamLibrary\SteamApps\common\Fallout 4\" Fallout4Launcher.exe
:check timeout /t 1 TaskList /fo list /fi "Imagename eq Fallout4Launcher.exe" /v TaskList | find "Fallout4Launcher.exe" 2>&1 > nul IF ERRORLEVEL 1 goto check
:loop xcopy /f /y "C:\Users\TRASHBUCKET\Documents\My Games\Fallout4\Saves\*.fos" "H:\Fallout4_Saves" /d timeout /t 8 TaskList | find "Fallout4Launcher.exe" 2>&1 > nul IF %ERRORLEVEL% NEQ 1 goto loop
taskkill /f /im Steam.exe /t
timeout /t 5
Could this be improved?As was stated by Geek-9pm, batch file programming really is not ideal for what you are trying to do. There are many tools better equiped to deal with event loops, including Perl, TCL, and possibly even VBScript/WSH and/or PowerShell. In your case, yes, this might possibly affect your machine performance because batch files are not meant to sit around waiting idle for something to happen; They're meant to EXECUTE multiple commands rapidly in sequence and then be done.
BTW, for what you seem to want to do.. I've done similar by creating an Excel spreadsheet with a VB code module which spins around in an event loop, waiting for a game's save file to appear. Once it does, it springs in to action, reads the game file, populates the spreadsheet with the game data I'm interested in, an so on. This is much more effective because Excel is an event driven application, and the VB code can put it to "sleep" by handing the processor back over to Excel (with the DoEvents VB function), without spinning around needlessly eating CPU cycles. This is the kind of thing which sounds more like what you're trying to do.And I thought I was so cool Task Monitor Info
CPU Time - 00:00:00 Avg. CPU - 0.01 Avg. Cycle - 0.01
|