Discussion:
Strange Batch Fil Problem & Fix
(too old to reply)
Peter
2021-07-14 14:19:03 UTC
Permalink
I was updating a batch file I wrote to backup one of my PCs. It kept crashing at a certain point. For two days I tried various things to fix it with no luck. I finally guessed at a fix which does work but I don’t understand what the problem was or why the fix works. Here are a few lines from the batch file showing the original version.

(It’s basically a counter/timer loop. When the counter hits 5 seconds it exits the loop and continues elsewhere in the batch file. It’s the bottom line that caused the crash.)

=================================
:wait-Z
if %counter%==5 goto Start-N
timeout 1 > nul
set /A counter=%counter%+1
if not exist z:\transfer\batch goto wait-Z
=================================

The fix: in the top line and the bottom line I changed “wait-Z” to “wait ZZ”

The same thing happened elsewhere in the batch file with “wait-O” which I had to change to “wait-OO”. The problem does NOT happen with “wait-N”, “wait-P” or “wait-Y”.

I’m just trying to understand what’s wrong with “wait-Z” and why “wait-ZZ” works fine.

Possible clue: this happens on only one of my PCs. It’s a new one I built about two weeks ago. All else seems to be normal with it. Windows 10 Pro 64bit.

Thanks!
JJ
2021-07-15 06:04:08 UTC
Permalink
Post by Peter
I was updating a batch file I wrote to backup one of my PCs. It kept
crashing at a certain point. For two days I tried various things to fix
it with no luck. I finally guessed at a fix which does work but I don’t
understand what the problem was or why the fix works. Here are a few
lines from the batch file showing the original version.
(It’s basically a counter/timer loop. When the counter hits 5 seconds it
exits the loop and continues elsewhere in the batch file. It’s the
bottom line that caused the crash.)
=================================
:wait-Z
if %counter%==5 goto Start-N
timeout 1 > nul
set /A counter=%counter%+1
if not exist z:\transfer\batch goto wait-Z
=================================
The `if` command would generate a syntax error if `%counter%` is
empty/blank.
Kenny McCormack
2021-07-15 10:47:47 UTC
Permalink
Post by JJ
Post by Peter
I was updating a batch file I wrote to backup one of my PCs. It kept
crashing at a certain point. For two days I tried various things to fix
it with no luck. I finally guessed at a fix which does work but I dont
understand what the problem was or why the fix works. Here are a few
lines from the batch file showing the original version.
(Its basically a counter/timer loop. When the counter hits 5 seconds it
exits the loop and continues elsewhere in the batch file. Its the
bottom line that caused the crash.)
=================================
:wait-Z
if %counter%==5 goto Start-N
timeout 1 > nul
set /A counter=%counter%+1
if not exist z:\transfer\batch goto wait-Z
=================================
The `if` command would generate a syntax error if `%counter%` is
empty/blank.
Yes, but that's not the line OP says is causing the problem.
Post by JJ
Post by Peter
if not exist z:\transfer\batch goto wait-Z
Further commentary in the OP makes it clear that that line (the one that
ends with "wait-Z" is, indeed, the line that is causing the problem.

In any case, I think we should assume that OP knows what he is doing with
the "counter" variable, although it would, of course, be safer to test via:

if X%counter%==X5 goto Start-N

(Or any of the other standard batch file hacks that have arisen over the
years to deal with this issue...)
--
People sleep peaceably in their beds at night only because rough
men stand ready to do violence on their behalf.

George Orwell
Zaidy036
2021-07-15 14:20:10 UTC
Permalink
Post by Kenny McCormack
Post by JJ
Post by Peter
I was updating a batch file I wrote to backup one of my PCs. It kept
crashing at a certain point. For two days I tried various things to fix
it with no luck. I finally guessed at a fix which does work but I dont
understand what the problem was or why the fix works. Here are a few
lines from the batch file showing the original version.
(Its basically a counter/timer loop. When the counter hits 5 seconds it
exits the loop and continues elsewhere in the batch file. Its the
bottom line that caused the crash.)
=================================
:wait-Z
if %counter%==5 goto Start-N
timeout 1 > nul
set /A counter=%counter%+1
if not exist z:\transfer\batch goto wait-Z
=================================
The `if` command would generate a syntax error if `%counter%` is
empty/blank.
Yes, but that's not the line OP says is causing the problem.
Post by JJ
Post by Peter
if not exist z:\transfer\batch goto wait-Z
Further commentary in the OP makes it clear that that line (the one that
ends with "wait-Z" is, indeed, the line that is causing the problem.
In any case, I think we should assume that OP knows what he is doing with
if X%counter%==X5 goto Start-N
(Or any of the other standard batch file hacks that have arisen over the
years to deal with this issue...)
use if %counter% EQU 5 goto Start-N
since not text but numerical

Does :wait-Z exist?

Also I would never use a goto target with something similar to batch
commands but rather add "_" to front of it or something more logical to
human reader.
Peter
2021-07-16 00:22:05 UTC
Permalink
I was updating a batch file I wrote to backup one of my PCs. It kept crashing at a certain point. For two days I tried various things to fix it with no luck. I finally guessed at a fix which does work but I don’t understand what the problem was or why the fix works. Here are a few lines from the batch file showing the original version.
(It’s basically a counter/timer loop. When the counter hits 5 seconds it exits the loop and continues elsewhere in the batch file. It’s the bottom line that caused the crash.)
=================================
:wait-Z
if %counter%==5 goto Start-N
timeout 1 > nul
set /A counter=%counter%+1
if not exist z:\transfer\batch goto wait-Z
=================================
The fix: in the top line and the bottom line I changed “wait-Z” to “wait ZZ”
The same thing happened elsewhere in the batch file with “wait-O” which I had to change to “wait-OO”. The problem does NOT happen with “wait-N”, “wait-P” or “wait-Y”.
I’m just trying to understand what’s wrong with “wait-Z” and why “wait-ZZ” works fine.
Possible clue: this happens on only one of my PCs. It’s a new one I built about two weeks ago. All else seems to be normal with it. Windows 10 Pro 64bit.
Thanks!
Thank you to those who replied! Let me clarify a couple of things. 1) The counter is not the problem. It's working fine. 2) The problem is caused by the bottom line shown above. The batch file terminates when it hits that line. If I change it from "wait-Z" to "wait-ZZ" there's no problem. I hope this clarifies the issue.
Herbert Kleebauer
2021-07-16 09:49:03 UTC
Permalink
Post by Peter
Post by Peter
=================================
:wait-Z
if %counter%==5 goto Start-N
timeout 1 > nul
set /A counter=%counter%+1
if not exist z:\transfer\batch goto wait-Z
=================================
2) The problem is caused by the bottom line shown above.
The batch file terminates when it hits that line. If I change > it from "wait-Z" to "wait-ZZ" there's no problem. I hope this
clarifies the issue.
No, it doesn't clarify anything, you not even showed us the
error message. Remove anything from the batch file which
is not necessary to reproduce the error and then post
this batch. Did you try to change it back to wait-z (or
wait-x) after it worked with wait-zz?
Tom Del Rosso
2021-07-18 01:40:22 UTC
Permalink
Post by Peter
Thank you to those who replied! Let me clarify a couple of things.
1) The counter is not the problem. It's working fine. 2) The
problem is caused by the bottom line shown above. The batch file
terminates when it hits that line. If I change it from "wait-Z" to
"wait-ZZ" there's no problem. I hope this clarifies the issue.
No clear answer yet.

Is there any other line with "wait" or any file with that name?

Is wait__ a variable name? Dump the environment by adding "set&pause" in
the batch, especially right before the problem line.

Does z:\transfer\batch exist?

Does the line [if not exist z:\transfer\batch goto wait-Z] have a colon
before wait?

Do you run from the command line or by clicking on it?

Try running from the command line (not by clicking on it) so you can see
all output, but first put "echo A"..."echo Z" in between the other
lines. That will give a "trace" output to show what lines really
executed.
--
Defund the Thought Police
JJ
2021-07-18 07:49:35 UTC
Permalink
Post by Peter
I was updating a batch file I wrote to backup one of my PCs. It kept
crashing at a certain point. ...
[snip]
Post by Peter
Possible clue: this happens on only one of my PCs. It’s a new one I
built about two weeks ago. All else seems to be normal with it. Windows
10 Pro 64bit.
Thanks!
Thank you to those who replied! Let me clarify a couple of things. 1)
The counter is not the problem. It's working fine. 2) The problem is
caused by the bottom line shown above. The batch file terminates when it
hits that line. If I change it from "wait-Z" to "wait-ZZ" there's no
problem. I hope this clarifies the issue.
By "crash", do you meant the batch file (i.e. ends the batch file and
returns to command prompt), or the whole CMD.EXE program (i.e. ends the CMD
process and closes the CMD window)?

Can you clarify that, in that one PC, the batch file is run using Windows'
own CMD.EXE, instead of other - including a "CMD.EXE" which is not in the
Windows' system directory?

If it's indeed run using Windows' own CMD.EXE, based on the weird batch file
behaviour/interpretation, and the clue that only one PC is affected, I could
only guess that the cause is either bugged CMD.EXE (e.g. from bad Windows
Hotfix), tampered CMD.EXE (e.g. modified by third party software, or virus),
or third party software interference (which includes malware).

Loading...