Strange Batch Fil Problem & Fix

27 views
Skip to first unread message

Peter

unread,
Jul 14, 2021, 10:19:05 AM7/14/21
to
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

unread,
Jul 15, 2021, 2:04:11 AM7/15/21
to
On Wed, 14 Jul 2021 07:19:03 -0700 (PDT), Peter wrote:
> 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

unread,
Jul 15, 2021, 6:47:49 AM7/15/21
to
In article <wjamkca6nw8f.uq871imtep81$.d...@40tude.net>,
JJ <jj4p...@gmail.com> wrote:
>On Wed, 14 Jul 2021 07:19:03 -0700 (PDT), Peter wrote:
>> 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.

OP says the "bottom" line caused the crash, and the bottom line is:

>> 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

unread,
Jul 15, 2021, 10:20:10 AM7/15/21
to
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

unread,
Jul 15, 2021, 8:22:07 PM7/15/21
to
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

unread,
Jul 16, 2021, 5:49:06 AM7/16/21
to
On 16.07.2021 02:22, Peter wrote:

>> =================================
>> :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

unread,
Jul 17, 2021, 9:40:25 PM7/17/21
to
Peter wrote:
>
> 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

unread,
Jul 18, 2021, 3:49:37 AM7/18/21
to
On Thu, 15 Jul 2021 17:22:05 -0700 (PDT), Peter wrote:
>> I was updating a batch file I wrote to backup one of my PCs. It kept
>> crashing at a certain point. ...
[snip]
>>
>> 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).
Reply all
Reply to author
Forward
0 new messages