Check if a batch completed successfully (and was not closed by the user.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
triggerer
Posts: 6
Joined: 03 May 2019 06:04

Check if a batch completed successfully (and was not closed by the user.

#1 Post by triggerer » 02 Jun 2019 16:24

Ultimate goal: Check if a batch completed successfully (and was not closed by the user or process error.)

My idea: Lets say that I want the file "main.bat" to run successfully and close itself and not by any other source (either user or program error). My solution is to create another batch file "checker.bat" (all files in the same folder). The process will begin by having the user start the "checker.bat" file.

Step 1) The checker will first create a dummy/temp file (lets say "flag.txt") with a flag variable (lets say "running").
Step 2) The checker will call the "main.bat".
Step 3) The checker will enter a loop where it will continuously check if the "main.bat" file is running.
Step 4) When the "main.bat" finishes, it will change the flag variable from "running" to "completed" and then it will close.
Step 5) The checker will notice that the "main.bat" process was closed and then it will check the flag variable. If the flag variable is equal to "completed", it will delete the file "flag.txt" and then exit (or echo "task completed" or do stuff).

Sidestep) If the checker notices that the "main.bat" process was closed and the flag variable is equal to "running", then alert the user that the task was not successful (or echo "task not successful" or do other stuff).

Is this a solid plan? Are there any other easier alternatives? Do you suggest any correction to the concept? Thank you deeply for your time and help in advance.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Check if a batch completed successfully (and was not closed by the user.

#2 Post by aGerman » 03 Jun 2019 12:44

The checker.bat was closed by the user. And now you need a check_checker.bat to observe it, right? And you will likely need a check_check_checker.bat, too.
So, no that doesn't sound reasonable. Your real ultimate goal is to get the content of main.bat executed, and you really should tell us about what it is in order to find a good solution. What you called an "ultimate goal" is what I would have called an "XY problem" :wink:

Steffen

triggerer
Posts: 6
Joined: 03 May 2019 06:04

Re: Check if a batch completed successfully (and was not closed by the user.

#3 Post by triggerer » 03 Jun 2019 15:32

Hello Steffen,

you are right about the XY problem. What I mean is that I do over-complicate things some times. ;)

Now... the main.bat is an encryption sequence. I do not think that there is any need to know what main.bat is but there ya go. As you said very wisely, the primary concept is to get the content of main.bat executed.
I know that if the checker was closed by the user then I would need to create a checker for the checker and so on. At least there is a "line of defence".
Another idea is to make the main.bat log different actions in a similar "temp/log" file. However I do not know why I still like the checker approach a little more.

P.S. I really do like to read new interesting stuff like the XY problem. Thank you for that.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Check if a batch completed successfully (and was not closed by the user.

#4 Post by aGerman » 03 Jun 2019 16:46

triggerer wrote:
03 Jun 2019 15:32
I do not think that there is any need to know what main.bat is
Maybe not. But It's definitely important to know how you call it. Is it via double click? Or do you call it from within another script?
And what shall happen if the script failed or was terminated before it finished the encryption task?
What happens if the encryption succeeded? Any subsequent tasks? How are they triggered? Anything else but Batch scripts involved?

Perhaps you could just reverse the logic in your attempt like that:

caller.bat

Code: Select all

@echo off &setlocal
echo in caller.bat
call "main.bat"
echo back in caller.bat - ready to do other tasks here
pause

main.bat

Code: Select all

@echo off
echo in main.bat
echo if you close the window now, the process flow doesn't return to caller.bat
pause
Run caller.bat and see what happens.
Steffen

triggerer
Posts: 6
Joined: 03 May 2019 06:04

Re: Check if a batch completed successfully (and was not closed by the user.

#5 Post by triggerer » 04 Jun 2019 01:07

aGerman wrote:
03 Jun 2019 16:46
Maybe not. But It's definitely important to know how you call it.
Agreed.

1) main.bat currently is started by double click
2) it gets a user input/choice (password and encryption)
3) if the script does not complete the process, the files for encryption might get corrupt (might, but that is the possibility I want to reduce).
4) of course there are several steps inside the main.bat (encryption script), but it is just sequential programming.
5) only batch scripts involved.
aGerman wrote:
03 Jun 2019 16:46
Run caller.bat and see what happens.
That is exactly what I had in mind. As far as the over-complication you mentioned is concerned, are there any advantages/disadvantages with such a method compared to another approach? Should I stick to that and call it a day or look for other options and methods?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Check if a batch completed successfully (and was not closed by the user.

#6 Post by aGerman » 04 Jun 2019 04:03

I still don't get the problem. If main.bat was interrupted and the encrypted output was corrupted, then nothing happens because the entire script flow was terminated and the corrupted output will not be used in subsequent tasks of the script. What do I miss here?

Steffen

triggerer
Posts: 6
Joined: 03 May 2019 06:04

Re: Check if a batch completed successfully (and was not closed by the user.

#7 Post by triggerer » 04 Jun 2019 11:07

The information of the flag variable or the log could be used by a future function of the script to "decorrupt" or save any important file (or at least notify the user) if the script fails by any reason.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Check if a batch completed successfully (and was not closed by the user.

#8 Post by aGerman » 04 Jun 2019 11:46

You said it's sequential. If the script failed or was interrupted, would you be able to begin somewhere in the middle of the sequences?
For me it's all like crystal-ball gazing because I don't have any context. So, I still don't understand the sense of a flag.

My understanding:
caller.bat

Code: Select all

@echo off &setlocal
echo in caller.bat
call "main.bat"
echo back in caller.bat
type "test.txt"
pause

main.bat

Code: Select all

@echo off
echo in main.bat
echo if you close the window now, the process flow doesn't return to caller.bat
>"test.txt" (for /l %%i in (1 1 100000) do echo %%i)
caller.bat runs main.bat which writes the numbers 1 to 100,000 into file test.txt. After that, caller.bat will process this file (prints its content). If you interrupt main.bat then test.txt is incomplete. But that's harmless because the incomplete text file won't be processed. The user has to run caller.bat again.

Steffen

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: Check if a batch completed successfully (and was not closed by the user.

#9 Post by pieh-ejdsch » 05 Jun 2019 07:41

Do you want to know if your file is still being read out or created?
There is error correction and error detection.
The detection can be done with a file comparison.
You can make a copy gradually in the process and compare it with the original with FC.
The next option is to count the characters before and during processing.

One way to check the message is Hashsum.bat - you check if the text or the new file matches this file hash.
You can calculate the hash before the message is destroyed.
You add this hash as additional information to the encrypted message.
Extract the hash, remove the hash from the message, decrypt the message.
et voila now you know if the message is complete.

Post Reply