Starting and ending batch files - windows

I am attempting to start recording the screen using Camtasia however am triggering this using a batch file.
As i need the recordings to be 30 seconds long, i have a secondary batch file. It doesn't work as intended as i would like to avoid multiple windows executing the commands opening. I know the command taskkill isn't correct and have included it to illustrate what i am intending to achieve.
The batch files need to be able to run on both Windows 7 and Windows 8 devices.
The process is started with a starting batch file that launches the secondary batch file to commence the countdown.
StartFile.bat
start EndFile.bat
"C:\Program Files\TechSmith\Camtasia Studio 8\CamRecorder.exe" /record
EndFile.bat
timeout /T 30 /NOBREAK
"C:\Program Files\TechSmith\Camtasia Studio 8\CamRecorder.exe" /stop
timeout /T 30 /NOBREAK
taskkill /f /im StartFile.bat <-- To kill the first batch file so there aren't multiple windows
start StartFile.bat <-- To create a new instance of the starting batch file.
exit

Just do it all in a single batch file with a GOTO loop (edited to take account of the discussion in the comments; the start is only needed because starting recording doesn't return control):
:startofloop
start "" "C:\Program Files\TechSmith\Camtasia Studio 8\CamRecorder.exe" /record
timeout /T 30 /NOBREAK
"C:\Program Files\TechSmith\Camtasia Studio 8\CamRecorder.exe" /stop
timeout /T 30 /NOBREAK
goto startofloop

Related

Execute 2 batch files nonstop

I am using 2 batch files that are calling each other and i don't want them to stop until i do it manually.
My issue is that when they start running, they are calling each other for approximately 2 hours.
Batch file 1(power_failure.bat):
python c:\alex_folder\UPS_SCRIPTS\TURN_OFF_FOR_BAT.py >>c:\alex_folder\UPS_SCRIPTS\log.txt
TIMEOUT /T 20 /NOBREAK >nul
call power_ok.bat
Batch file 2(power_ok.bat):
python c:\alex_folder\UPS_SCRIPTS\TURN_ON_FOR_BAT.py >>c:\alex_folder\UPS_SCRIPTS\log.txt
TIMEOUT /T 20 /NOBREAK >nul
call power_failure.bat
What can be changed in order for these scripts to run for ever until i stop them and not only 2 hours?

Close and restart a process after a certain time period in batch

I'm trying to make a batch file that opens a certain exe and then, after (for example) 5 minutes, closes it then reopens it again. I have tried this:
#echo off
:loop
cd /d %~dp0
certain exe
timeout /t "time" (by minutes)
taskkill /f /im "certain exe"
goto loop
but it wouldn't close the exe nor open it, what can I do?
The problem is that cmd will wait for the current command to end before executing the next command, so timeout (and taskill) will not be executed until example.exe closes by itself.
To ensure that cmd doesn't wait example.exe, you'll need to use start /B, as in this example:
#echo off
:loop
start /B "example.exe"
ping -n seconds_of_delay+1 127.0.0.1>nul
taskkill /f /im "example.exe"
goto :loop
Replace seconds_of_delay+1 with the number of seconds you want the time period to last plus one.
/B is needed because, without it, start would execute example.exe in a new window (if you want that, simply use start).
I used ping as the way to create a time delay because it has been found to consume less processor time than sleep or timeout (you can find more details here).

Need a batch file to start, delay a close, and restart another batch file.

I have searched and searched and this is the closest code I have found:
#echo off
:loop
C:\CryptoCurrency\nexus_cpuminer\start.bat
timeout /t 30 >null
taskkill /f /im nexus_cpuminer.exe >nul
goto loop
A few things: notice the start.bat. The .exe I need to launch has to start via the .bat file because the .bat file contains information the .exe needs.
Secondly, the .exe launches a CMD prompt window which shows me what's going on.
(keep this in mind because this is not your normal .exe, I WANT that CMD prompt window to close when it's KILLED)
I am aware I have it set for 30 seconds. I'm just testing right now. I'd like to set it for 4 hours before the kill command is called. Also, I'd like to set a "delay" of 30 seconds before the whole process starts over. I am running Windows 7 x 64.
You must change the name of the second Batch file to other name (i.e. starter.bat) and execute it via the start internal command in order to execute it in parallel:
#echo off
:loop
start "" cmd /C "C:\CryptoCurrency\nexus_cpuminer\starter.bat"
timeout /t 30 >null
taskkill /f /im nexus_cpuminer.exe >nul
goto loop
The last line in starter.bat file must be the execution of nexus_cpuminer.exe, so when it is killed via taskkill, the .bat file ends immediately.
Another simpler approach is to directly execute nexus_cpuminer.exe in this Batch file, via start "" cmd /C nexus_cpuminer.exe command, so this process be opened in its own cmd.exe window.
If you CALL start.bat, it will return to your 'calling' script.
If you give start.bat a TITLE, you can /FIlter your TASKKILL command to EQ that WINDOWTITLE

Batch file won`t start program when launched from windows service

I have created a batch file to run when a specific service stops.
The batch file should stop the relevant software running, restart some services (stop / start) and start the software again.
It`s a simple code:
#echo off
Taskkill /F /IM program1.exe
Taskkill /F /IM program2.exe
timeout /t 5
net stop service1
net stop service2 && net start service2
net start service3
timeout /t 2
start C:/path/program1.exe
start C:/path/program2.exe
Have set the service up to run the batch file as recovery at first failure.
Batch file is linked via a shortcut to be able to run it as administrator.
This works perfectly when running the batch file directly, but when it`s executed by the service recovery, the start-up of the software fails.
Does anyone have any idea what could be wrong?
In some circumstances timeout /t 5 might cause trouble. Try to replace it with PING -n 6 127.0.0.1 > NUL.

Batch file to commit changes to Embedded XP and then restart pc

I would like to create a batch file to run a cmd command to commit changes to a windows embedded pc from a USB drive and restart the PC to make the changes active.
The cmd line I use is:
ewfmgr -commit c:
But I need to open the cmd prompt and run the command then once it has run, restart the PC
This is what worked
#echo OFF
:reboot
c:\windows\system32\ewfmgr.exe C: -commit
echo Rebooting...Please Wait
c:\windows\system32\xpepm.exe -restart
pause
shutdown /? could give some hints. Then your batch file might look as follows:
ewfmgr -commit c:
shutdown /r
To ensure batch wait until ewfmgr command ends, use
start "" /W ewfmgr -commit c:
shutdown /r
With the /W or /WAIT switch, the start command will start application and wait for it to terminate. More info on start command.
To give a some kind of wait after the commit command so that it can run and finish its task, e.g. for a delay of 30 seconds:
add timeout /T 30 /nobreak>nul line before shutdown /r, and/or
use shutdown with /t xxx switch (this sets the time-out period
before shutdown to xxx seconds), i.e. shutdown /r /t 30
A workaround if timeout command is not recognized: PING -n 31 127.0.0.1>nul
Create a new file with the .bat extension. Open it in your preferred text editor and enter the commands you want to be run, and save the file. The commands that JosefZ wrote would probably do the job perfectly.

Resources