Automatically Batch File if still running after 5 minutes i.e. hanging - windows

I have a BAT File which connects to an FTP site to send/receive files. On rare occasions the batch file will hang if there is an issue with the network. I would like to know if there is a command to automatically exit the batch file if it has been running longer than 5 minutes? This would be placed in the batch file I assume, a kind of countdown from when it first opens.
Thanks.
Brett

You have to have two windows.
start ftp etc
Timeout /t 300
taskkill /im ftp.exe /f
This will kill all ftp.exe not just one. NOTE this relys on the fact we are running ftp without running cmd in the second console window.

Related

How to silently run a batch file every 5 minutes

A Windows 10 application starts to lag every 10-15 minutes so every once in a while, when I notice the lag, I run the below batch file to delete the temp files. It works to eliminate the application lag but I would like to automate the batch file so that once I initiate it, it loops every 5 minutes without the cmd window poping up. Since I dont know how long I will be using the application and dont want the batch file runing after I finish using the application I need to be able to terminate the batch file manually.
Appreciate to let me know if there is options with and without the windows task scheduler.
#echo off
set targetdir=C:\ExampleDir
del /q %targetdir%\*
for /d %%x in (%targetdir%\*) do #rd /s /q ^"%%x^"

set batch script for loop to NOT wait for previous iteration to complete

I have the following script which launches multiple sessions of RDP connection files at once (colleted from a folder which only contains rdp files).
If I launch this from cmd prompt, it launches all sessions in parallel (which is what I want)
for /r %i in (*.rdp) do (mstsc %~nxi /f)
**while **if i run this script, it just launches the first session then waits for the relative process to end before running the second connection and so on.
for /r %%i in (*.rdp) do (mstsc %%~nxi /f)
What I'm doing wrong? Shouldn't it be the default behavior of batch to run all commands in parallel?
I've checked this but it doesn't address my exact scenario and it doesn't work anyhow as expected (e.g. START myBatchScript.bat doens't change the "waiting for process" behavior)
EDIT
Added answer based on comments (Thanks to #Compo and #Modi)
The coding solution to replicate the cmd behavior as a batch script is using the "start" command (as below, thanks to #Compo)
#for /r %i in (*.rdp) do #(start "" mstsc "%~nxi" /f)
for the reason why this happens refer to #Mofi comments.

Shut down machine after being idle with batch file?

I don't even know if this is possible, but is there anyway to shut down a machine after (n) minutes, using a batch file?
Currently I already have a batch file which works alongside ranorex and a virtual machine, and I need the machine to shut itself down after it has been idle for 10 minutes or so, just to give everything else in the batch file plenty of time to run. Is there any way of doing this?
Just use the command for shutdown and add the option for the time to wait:
shutdown /s /t 600 /f
/s is for shutting down the computer
/t is for wait before do the operation with time in seconds (10*60=600)
/f is for forcefully close all applications
With shutdown /a you can abort the action befor the time runs off. For more options use the help of the shutdown-command.

Saving project with a batch file

I use batch files between my PLC and computer. PLC sending some commands to PC via batch files. For example I open a program or close. It works well done. But when I close the program It does not save anything.
I use command below:
taskkill /F /IM myprogram.exe
How can I close the program with saving?
That command kills the process, not allowing it to perform any actions when it closes. It just shuts down and exits.
You could try to omit the /F option, and save all data when your program gets the command to exit.

My batch file is closing a program too early

Thank you, for those whom took the time to read my question. I am a gamer and would like to execute a few things. First, I would like to Trigger a batch file when I click a program, how do you do that or is it even possible? Basically, activating a game, triggers the batch file.
NOw for the batch file problem, I want to execute Xpadder when I activate games (this is an mmo) and when I close the game I want Xpadder's process/service to close. Ultimately, it's auto trigger,activate,wait,terminate.
That's kind of the process I want it to go if all can be done.
Batch File:
#echo off
start "Blade" "C:\Users\user\Documents\Blade.xpadderprofile" Blade.xpadderprofile
ECHO Blade.xpadderprofile STARTED
start /w "C:\Program Files (x86)\game\games.exe" games.exe
ECHO games STARTED
taskkill /f /im Xpadder.exe
This actually works but the problem is there are two ".exe" files with mmo's. I'll start the game and it would close Xpadder too early because one launcher starts another launcher/client. Xpadder works for the first launcher but the first launcher closes so the game will start. I hope I am explaining myself clear enough.
Reference link: How to automatically close App_A when I close App_B using batchfile
Essentially, this is the same question I have but it's not very clear. What is the batch code to get Xpadder to stay on until the second launcher/client is closed not the first one?
P.S
The game has to open through the launcher then into the second launcher/client or it will not work.
here is the other clients name and path i think:
C:\Program Files (x86)\game\gamer\bin\gam.exe
How about the use of the PsExec, a small MS ulility? Using it, your batch should work:
cmd /c psexec -d "Blade" "C:\Users\user\Documents\Blade.xpadderprofile" Blade.xpadderprofile
start /w "C:\Program Files (x86)\game\games.exe" games.exe
taskkill /f /im Xpadder.exe
The file psexec.exe must be placed in folder enlisted in the system variable WINDIR or PATH, or, otherwise, you should call it with its full path, eg. *C:\Program Files\Others\pstools.exe".
You can add #echo off, salt, pepper or some green Tabasco if you have mon€y for this :D

Resources