Killing command line FOR which runs a batfile?

#3
On Mon, 02 Jun 2008 20:47:30 -0500, you wrote:


>vefatica wrote:
>
>
>---Quote---
>> Is there any way to abort a command line FOR command which runs a batch
>> file? I
>> keep getting the "abort batch file" prompt and after answering "Y", the
>> FOR loop continues.
>---End Quote---
>How are you trying to abort it?
With Ctrl-C/Ctrl-Break.
--
- Vince
 
#4
vefatica wrote:
How are you trying to abort it?
Very occasionally (5th try, below), an "A" response to the "cancel batch" prompt works.

v:\> for /l %z in (1601,1,2008) agetodate.btm %@makeage[%z-02-28]
1601-02-28 00:00:00
[snip]
1644-02-28 00:00:00
^C

Cancel batch job V:\agetodate.btm ? (Y/N/A) : A
1646-02-28 00:00:00
[snip]
1661-02-28 00:00:00
^C

Cancel batch job V:\agetodate.btm ? (Y/N/A) : A
1663-02-28 00:00:00
[snip]
1675-02-28 00:00:00
^C

Cancel batch job V:\agetodate.btm ? (Y/N/A) : A
1677-02-28 00:00:00
[snip]
1687-02-28 00:00:00
^C

Cancel batch job V:\agetodate.btm ? (Y/N/A) : A
1689-02-28 00:00:00
[snip]
1698-02-28 00:00:00
^C

Cancel batch job V:\agetodate.btm ? (Y/N/A) : A
v:\>

FINALLY!
 

rconn

Administrator
Staff member
May 14, 2008
10,506
94
#5
vefatica wrote:

> Is there any way to abort a command line FOR command which runs a batch
> file? I
> keep getting the "abort batch file" prompt and after answering "Y", the
> FOR loop continues.
Can you give me the exact FOR syntax and batch file you're running?

I'm unable to reproduce the problem here with the FOR loop continuing.

Rex Conn
JP Software
 
#6
vefatica wrote:
Can you give me the exact FOR syntax and batch file you're running?

I'm unable to reproduce the problem here with the FOR loop continuing.
I already gave the exact command. Here it is again.

v:\> for /l %z in (1601,1,2008) agetodate.btm %@makeage[%z-02-28]
1601-02-28 00:00:00
[snip]
1644-02-28 00:00:00
^C

Cancel batch job V:\agetodate.btm ? (Y/N/A) : A
1646-02-28 00:00:00
[snip]
1661-02-28 00:00:00
^C

Cancel batch job V:\agetodate.btm ? (Y/N/A) : A
1663-02-28 00:00:00
[snip]
1675-02-28 00:00:00
^C
 

rconn

Administrator
Staff member
May 14, 2008
10,506
94
#7
vefatica wrote:

>
> Quote:
> Originally Posted by *rconn* View Post <showthread.php?p=600#post600>
> vefatica wrote:
> Can you give me the exact FOR syntax and batch file you're running?
>
> I'm unable to reproduce the problem here with the FOR loop continuing.
>
> I already gave the exact command. Here it is again.
>
> v:\> for /l %z in (1601,1,2008) agetodate.btm %@makeage[%z-02-28]
> 1601-02-28 00:00:00
> [snip]
> 1644-02-28 00:00:00
You didn't provide the batch file contents.

Rex Conn
JP Software
 
#8
You didn't provide the batch file contents.
> v:\> for /l %z in (1601,1,2008) agetodate.btm %@makeage[%z-02-28]
> 1601-02-28 00:00:00
> [snip]
> 1644-02-28 00:00:00

Actually I did post it in the Sparklist forum. I'm going to try to attach it (seems to have worked!). Just in case I'll paste it below. It's cryptic for speed.

I use a plugin @ISLEAP[] (=365|366). You'll need this:

function isleap=`%@if[%@eval[%1 %% 4] NE 0 .or. (%@eval[%1 %% 100] EQ 0 .and. %@eval[%1 %% 400] NE 0),365,366]`

::AGETODATE.BTM
setlocal
unset *
unalias *
unfunction *
set /a m=(%1 + 5000) \ 10000 & set /a d=%m \ 86400000 & set /a t=%m %% 86400000 & set /a s=%t \ 1000 & set /a t=%t %% 1000 & set y=1601
do while %d GE 146097
set /a y+=400 & set /a d-=146097
enddo
do i=1 to 3
iff %d GE 36524 then & set /a y+=100 & set /a d-=36524 & else & leave & endiff
enddo
do i=1 to 24
iff %d GE 1461 then & set /a y+=4 & set /a d-=1461 & else & leave & endiff
enddo
do i=1 to 3
iff %d GE 365 then & set /a y+=1 & set /a d-=365 & else & leave & endiff
enddo
set /a d+=1
set q=31 %@if[%@isleap[%y] EQ 365,28,29] 31 30 31 30 31 31 30 31 30 31
set m=0
do while %d GT %@word[%m,%q]
set /a d-=%@word[%m,%q] & set /a m+=1
enddo
echo %y-%@format[02,%@inc[%m]]-%@format[02,%d],%@maketime[%s].%@format[03,%t]
 

Attachments

rconn

Administrator
Staff member
May 14, 2008
10,506
94
#9
vefatica wrote:

>
> Quote:
> Originally Posted by *rconn* View Post <showthread.php?p=602#post602>
> You didn't provide the batch file contents.
>
> > v:\> for /l %z in (1601,1,2008) agetodate.btm %@makeage[%z-02-28]
> > 1601-02-28 00:00:00
> > [snip]
> > 1644-02-28 00:00:00
>
> Actually I did post it in the Sparklist forum. I'm going to try to
> attach it (seems to have worked!). Just in case I'll paste it below.
> It's cryptic for speed.
>
> I use a plugin @ISLEAP[] (=365|366). You'll need this:
>
> function isleap=`%@if[%@eval[%1 %% 4] NE 0 .or. (%@eval[%1 %% 100] EQ 0
> .and. %@eval[%1 %% 400] NE 0),365,366]`
WAD -- a Ctrl-C is *not* propagated back to parent commands. (Changing
this would be catastrophic for existing batch files and aliases!)

There is a special case when pressing a ^C in a batch file, in that you
are given the option of exiting the current batch file or all batch
files. However, it does *not* mean that the next command at the command
prompt will also be aborted.

After the ^C, the parser stops the batch file and returns to the FOR.
Since FOR doesn't (and shouldn't!) know that you pressed ^C, the only
thing it can do is check the return value of the batch file. Since you
didn't provide an ON BREAK, the return value is going to be the default (0).

If you want it to abort your FOR loop if you've aborted the at the
command prompt, you need to add something like this to the beginning of
the batch file:

on break quit 3

which will catch every ^C, return a 3 to the FOR, which will then abort
the loop. (FOR *always* aborts the loop if the target command returns a 3.)

Rex Conn
JP Software

p.s.: Your next question is going to be "Then why does it sometimes
break out of the FOR?" Because sometimes the ^C is during the batch
file initialization (or termination), so the ^C is sent to the current
command (which is the FOR).
 
#10
On Wed, 04 Jun 2008 23:02:20 -0500, you wrote:


> on break quit 3
>
>which will catch every ^C, return a 3 to the FOR, which will then abort
>the loop. (FOR *always* aborts the loop if the target command returns a 3.)
Thanks!

That's very useful info, and an elegant solution. Is that well known? Is it
mentioned in the help (should be ... so simple and useful)?

--
- Vince