From dictation.
Hello, this is not a quick solution to your problem.
A number of people have looked at your post but have not giving you any kind of reply. Therefore I thought I would deeply say something in an effort to be helpful.
For myself is extremely difficult to debug batch files that tend to be rather involved. Mostly because the conventions that are used in that file programming are not self-evident to most programmers who have come from another language.
This is intended to be constructive criticism. If it were me, I would start out with a broad outline of my objective. In some cases using somebody else's code might be the best solution. But not always. One of the things you have to do in programming is to analyze ahead of time all the things that might go wrong. .
Although batch file cando a lot of things, it can become awkward . Other script languages are much easier to read. I'm thinking of a language such as VBScript. With Vb Script there is a way to identify a things with descriptive names inside the program script.
So for me, that would be enough reason to want to use Vb Script or a similar programming language that has a way to refer to variables and methods with names that make sense, I think that can be done in batch file programming, but takes more effort. I think.
It might be the batch file you have chosen can be adopted. Or it may be necessary to write a completely new program. Possibly in batch or maybe in some other scripting language that is available in Windows.
Again, this is meant to be helpful. I hope this is of some help.
End of dictation.