0

Heres my Code

SET /A "index = 1"
SET /A "count = 30"
timeout 5
:while
if %index% leq %count% (
    "tinytask" Test.rec
    taskkill /F /IM tinytask.exe
    timeout 3
    set /A "index = index + 1"
    goto :while
)

it's supposed to open a tiny task recording and then after it's done kill it/ end the task and then wait 3 seconds before doing it again. it would do this 3 times. but instead, it doesn't kill the tiny task and it doesn't stop at the 3rd time if I kill it myself it will go through the loop all over again. Please Tell me What I'm doing wrong!

PikaCoder
  • 3
  • 2
  • 1
    the batch is waiting for tinytask to terminate, so does not reach the the `taskkill` process line at all, until it is manually terminated or the program exists. Try `start "" "tinytask" Test.rec`. I am however concerned about starting the app and killing it without the exectuable even having time to really start. additionally, a better method to increase index is `set /A index+=1` – Gerhard Jul 16 '20 at 18:25
  • 1
    Please open a [command prompt](https://www.howtogeek.com/235101/), run `set /?` and read the output help carefully from top of first to bottom of last page. See also my answer on [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) First recommendation: Do not use an arithmetic expression like `SET /A "index = 1"` to define an environment variable, use `set index=1` or more safe `set "index=1"` with enabled extensions as by default (see referenced answer for details). – Mofi Jul 16 '20 at 18:44
  • Second recommendation already suggested also by Gerhard is using an arithmetic expression as efficient as possible as explained by help of command __SET__ which means using `set /A index+=1` to increment a value assigned to an environment variable by one. – Mofi Jul 16 '20 at 18:47
  • When starting an executable from within a command prompt window which is not a console application, `cmd.exe` starts the GUI application as separate process and the user can immediately continue using the command prompt window and enter the next command line to execute. But on processing a batch file designed to run one command line after the other (meaning of __batch__), `cmd.exe` starts and executable (console or GUI application) and waits for the self-termination of the started executable. So command line `"tinytask" Test.rec` results in starting `tinytask` and waiting for its termination. – Mofi Jul 16 '20 at 18:50
  • The solution was suggested already by Gerhard. The GUI application `tinytask.exe` (better specified with file extension, best specified also with full path) is started using command `start` with help output on running `start /?`. The first double quoted argument string is interpreted as title for the console window even on started executable is a GUI application and so no console window is opened at all. For that reason it is advisable to always specify a title string immediately after command `start` as first argument which can be an empty string specified with `""` for a GUI application. – Mofi Jul 16 '20 at 18:53
  • `tinytask.exe` is not an executable installed by default on Windows. I suppose this file is in same directory as the batch file because of being specified with just its file name without file extension and without full path. So `cmd.exe` has to search for a file `tinytask.*` with a file extension listed in environment variable `PATHEXT` in current folder if none found next in all folders of which path is in environment variable `PATH`. The file `Test.rec` is also specified without path which means the file must be found in the current directory by `tinytask.exe`. What is the current directory? – Mofi Jul 16 '20 at 18:57
  • 1
    Consider that the application you're using, is most likely a small 32-bit program, so my advice would be to predetermine the bitness of the OS, then run the entire script using the appropriate bitness version of `cmd.exe`. Alternatively you could at least try to determine whether the current `cmd.exe` instance is a 64-bit or 32-bit process, then execute the appropriate 32-bit version of TaskKill if necessary. – Compo Jul 16 '20 at 19:01
  • Well, on double clicking on batch file, Windows shell sets the directory of the batch file as current directory on starting `cmd.exe`, except `Run as administrator` is used, see [this answer](https://stackoverflow.com/a/31655249/3074564) details. `cmd.exe` could immediately set itself the Windows directory as current directory on batch file being stored in a shared folder accessed using UNC path. There is in real one possibility that the batch file directory is the current directory on starting the batch file, and millions of possibilities that a different directory is the current directory. – Mofi Jul 16 '20 at 19:02
  • For that reason it would be a good idea to make sure `tinytask.exe` and `Test.rec` are always specified with full qualified file name which means with full path + name + extension. There is `%~dp0` which expands to drive and path of argument 0 which is the full path of the currently executed batch file which always ends with a backslash. So best is using `start "" "%~dp0tinytask.exe" "%~dp0Test.rec"` which results in a working command line which is independent on which directory is the current directory on starting the batch file. – Mofi Jul 16 '20 at 19:06
  • Please run also in command prompt window `taskkill /?` and read the output help. I don't recommend to use the option `/F` to __force__ a brutal kill of the executable by the operating system without giving the executable the possibility to gracefully terminate itself. The usage of this option is not recommended if `tinytask.exe` do file system operations like writing a file. I recommend to test if `tinytask.exe` terminates itself gracefully within a short time on using `%SystemRoot%\System32\taskkill.exe /IM tinytask.exe` on receiving `WM_CLOSE` message from `taskkill.exe`. – Mofi Jul 16 '20 at 19:12
  • 1
    If you use the `start "%~dp0tinytask.exe"` method, however, you would need to be sure that you give sufficient time for `tinytask` to open, and run, _(or begin to run)_, the `.rec` file before you attempt to close it, _(otherwise you're wasting your time running the script in the first place)_. This means you should introduce another `timeout` command _(possibly using the `/NoBreak` option and redirected to the `NUL` device)_, before running your `taskkill` command, to allow for that to happen – Compo Jul 16 '20 at 19:12
  • Thank you, Compo, Mofi, Gerhard for commenting and helping! i used Gerhard's suggestion to put start "" "tinytask" Test.rec''' . i also used another suggestion of Gerhard's: set /A index+=1 instead of set /A "index = index + 1". – PikaCoder Jul 16 '20 at 20:47

0 Answers0