set /a

  • Thread starter Thread starter Jean Pierre Daviau
  • Start date Start date
J

Jean Pierre Daviau

Hey,


set /a _conteur=%_conteur%+1
if %_conteur% EQU 50 goto :eof
@echo %_conteur%

Why the EQU dont stop the loop?

Thanks


Jean Pierre Daviau
 
Jean Pierre Daviau said:
Hey,


set /a _conteur=%_conteur%+1
if %_conteur% EQU 50 goto :eof
@echo %_conteur%

Why the EQU dont stop the loop?

Thanks

Two reasons:
a) because there is no loop, and
b) because you forgot to initialise _conteur

Try this:
@echo off
set _conteur=1
:Loop
set /a _conteur=%_conteur%+1
if %_conteur% EQU 50 goto :eof
echo %_conteur%
goto Loop
 
Pegasus said:
Two reasons:
a) because there is no loop, and
b) because you forgot to initialise _conteur

Try this:
@echo off
set _conteur=1
:Loop
set /a _conteur=%_conteur%+1

I prefer this syntax:

set/a _conteur += 1

Also note that the expression on the right side of the assignment operator
in a "SET/A" command can include variables without percent signs, i.e.:

set/a area = width * height


/Al
 
Jean Pierre Daviau said:
Hey,


set /a _conteur=%_conteur%+1
if %_conteur% EQU 50 goto :eof
@echo %_conteur%

Why the EQU dont stop the loop?

Thanks


Jean Pierre Daviau

You haven't shown the loop. Batch can be sensitive to context - your code
fraction is too small.

IF you have

set _conteur=49
for %%i in (some list of values) do (
set /a _conteur=%_conteur%+1
if %_conteur% EQU 50 goto :eof
@echo %_conteur%
)

THEN the PARSE-TIME value of _conteur is substituted, and the loop is
executed as

for %%i in (some list of values) do (
set /a _conteur=49+1
if 49 EQU 50 goto :eof
@echo 49
)

To change this behaviour, you can invoke DELAYED EXPANSION mode

SETLOCAL ENABLEDELAYEDEXPANSION
set _conteur=49
for %%i in (some list of values) do (
set /a _conteur=!_conteur!+1
@echo before : %_conteur% after : !_conteur!
if !_conteur! EQU 50 goto :eof
)

BUT this creates its own problems - any environment changes made after a
SETLOCAL ENABLEDELAYEDEXPANSION instruction are backed-out by a matching
ENDLOCAL or by reaching end-of-file, which is an implicit ENDLOCAL.

Without being able to see more than the few lines of your code that you are
showing us, its impossible to diagnose futher.
 
I used GTR and everything was ok. Has I reed your post I have things to
learn.

-------------
@echo off
set _mypth=
set _conteur=
@echo
>zx.txt
::for %a in (*.gif) do @echo [a href="%a"] [/a][br]
for %%a in (*.gif) do call :sub %%a
@echo.
@echo [/td][td] >>zx.txt
@echo %_mypth% >>zx.txt
@echo [/td][/tr] >>zx.txt
@echo [/table] >>zx.txt
set _mypth=
goto :end
::
:sub
set /a _conteur=%_conteur%+1
set _mypth=%_mypth%[a href="%1"][img src="%1"][/a][br]
if %_conteur% GTR 38 ( @echo.
@echo [/td][td] >>zx.txt
@echo %_mypth% >>zx.txt
@echo.
set _mypth=
set _conteur=
)
goto :eof
:end
 
I used GTR and everything was ok.  Has I reed your post I have things to
learn.

-------------
@echo off
set _mypth=
set _conteur=
@echo
>zx.txt
::for %a in (*.gif) do @echo [a href="%a"] [/a][br]
for %%a in (*.gif) do call :sub %%a
@echo.
@echo [/td][td] >>zx.txt
@echo %_mypth% >>zx.txt
@echo [/td][/tr] >>zx.txt
@echo [/table] >>zx.txt
set _mypth=
goto :end
::
:sub
set /a _conteur=%_conteur%+1
set _mypth=%_mypth%[a href="%1"][img src="%1"][/a][br]
if  %_conteur% GTR 38 ( @echo.
   @echo [/td][td] >>zx.txt
   @echo %_mypth% >>zx.txt
   @echo.
   set _mypth=
   set _conteur=
   )
goto :eof
:end[/QUOTE]

You can replace all of the open brackets ([) with ^< and the close
brackets (]) with ^> and you won't have to reprocess the output file.
It will already be in HTML format - just change the name of the
batch's output file to zx.htm.

Also, the loop counter, conteur, can be executed thus ...

set /a _conteur +=1

Also, once an ECHO OFF statement is issued, the inline echo
suppression character, @, is no longer needed.

I reworked your procedure a little to reflect these points ...

@echo off
set _mypth=
set _conteur=
echo ^<table^>^<tr^>^<td^> >zx.htm
for %%a in (*.gif) do call :sub %%a
(echo.
echo ^</td^>^<td^>
echo %_mypth%
echo ^</td^>^</tr^>
echo ^</table^>
) >>zx.htm
set _mypth=
goto :eof
::
:sub
set /a _conteur +=1
set _mypth=%_mypth%^<a href="%1"^>^<img src="%1"^>^</a^>^<br^>
if %_conteur% GTR 38 ( echo.
echo ^</td^>^<td^>
echo %_mypth%
echo.
set _mypth=
set _conteur=
)>>zx.htm

These is more that could be done to simplify it, but that's enough for
you to chew on, now.

Tom Lavedas
==========
 
Jean Pierre Daviau said:
I used GTR and everything was ok. Has I reed your post I have things to
learn.

-------------
@echo off
set _mypth=
set _conteur=
@echo
>zx.txt
::for %a in (*.gif) do @echo [a href="%a"] [/a][br]
for %%a in (*.gif) do call :sub %%a
@echo.
@echo [/td][td] >>zx.txt
@echo %_mypth% >>zx.txt
@echo [/td][/tr] >>zx.txt
@echo [/table] >>zx.txt
set _mypth=
goto :end
::
:sub
set /a _conteur=%_conteur%+1
set _mypth=%_mypth%[a href="%1"][img src="%1"][/a][br]
if %_conteur% GTR 38 ( @echo.
@echo [/td][td] >>zx.txt
@echo %_mypth% >>zx.txt
@echo.
set _mypth=
set _conteur=
)
goto :eof
:end[/QUOTE]

A couple of tips:

1. "@" before a command means "do not echo this command to the console
before executing it"
So, "@echo off" as the FIRST line of a batch prevents the "echo off" from
being echoed to the console.

Once "@echo off" has been executed, commands will no longer be echoed to the
console, so your "@echo" commands may be replaced by simply "echo" EXCEPT
the very first (the "@echo off")

2. You may escape the redirector characters (> and <) by prefixing them with
the caret (^)

so "@echo [/td][td] >>zx.txt" for example may become

echo ^</td^>^<td^> >>zx.txt

3. You can place the redirectors at the BEGINNING of the statement if you
wish, so

is valid - and avoids confusion between ">" used as a redirector and ">" as
an escaped character.
 
Many thanks

I placed 6 .gif icons on the desktop

This line gives: < was unattended.

set _mypth=%_mypth%^<a href="%1"^>^<img src="%1"^>^</a^>^<br^>

The others prints ok.
 
Many thanks

I placed 6 .gif icons on the desktop

This line gives:            <                 was unattended.

 set _mypth=%_mypth%^<a href="%1"^>^<img src="%1"^>^</a^>^<br^>

The others prints ok.

Try replacing the :SUB with this one ...

:sub
set /a _conteur +=1
if %_conteur% LEQ 1 ( echo. & echo ^</td^>^<td^>) >>zx.htm
echo ^<a href="%1"^>^<img src="%1"^>^</a^>^<br^> >>zx.htm
if %_conteur% GEQ 39 ( echo. & set _conteur=) >>zx.htm

The escape character is lost once stored using the SET, so that it
causes a problem on the next iteration. This approach does away with
the need for the SET.

Tom Lavedas
==========
 
Jean Pierre Daviau said:
Many thanks

I placed 6 .gif icons on the desktop

This line gives: < was unattended.

set _mypth=%_mypth%^<a href="%1"^>^<img src="%1"^>^</a^>^<br^>

The others prints ok.

Yes - you are correct BUT the error is actually occurring on the following
LOGICAL line.

if %_conteur% GTR 38 ( echo.
echo ^</td^>^<td^>
echo %_mypth%
echo.
set _mypth=
set _conteur=
)>>zx.htm

is ALL ONE logical line on seven PHYSICAL lines. The processor will
substitute the current value of _mypth into this line, and the current value
is "<a href="filename"><img src="filename"></a><br>"

since this starts with an unescaped "<", it is interpreted as a redirector -
and this is invalid syntax.

Fixing this problem is not easy, but it can be done.


This solution developed using XP
It may work for NT4/2K

----- batch begins -------
[1]@echo off
[2]set _mypth=
[3]set _conteur=
[4]echo ^<table^> >zx.htm
[5]for %%a in (*.gif) do call :sub %%a
[6]if defined _mypth call :output
[7]echo ^</table^> >>zx.htm
[8]goto :eof
[9]::
[10]:sub
[11]set /a _conteur +=1
[12]set _mypth=%_mypth%[a href="%1"][/a][br]
[13]if %_conteur% GTR 38 call :output
[14]goto :eof
[15]
[16]:output
[17]echo .
[18]set _mypth=%_mypth:~1,-1%
[19](
[20] echo ^<tr^>^<td^>
[21] echo ^<%_mypth:][=^>^<%^>
[22] echo ^</td^>^</tr^>
[23])>>zx.htm
[24]set _mypth=
[25]set _conteur=
[26]goto :eof
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed

The label :eof is defined in NT+ to be end-of-file but MUST be expressed as
:eof


[5] calls :sub for each .GIF

:SUB builds _mypth as
[a href="x1"][img src="x1"][/a][br][a href="x2"][img src="x2"][/a][br]...

until 38 have been accumulated, then calls :output.

:OUTPUT outputs a <TR><TD> frame around _mypth.
[18] removes the first character of _mypth, which must be a "[" and also the
last char which must be a "]"
[21] echoes _mypt5h, replacing each "][" sequence with "><", with a leading
"<" and a trailing ">" to replace the characters removed by [18]
and then [24,25] clear _mypth and _conteur

If the last line is not 38 elements long, _mypth will still be defined when
the FOR loop terminates, so [6] calls :OUTPUT to produce the remaining
lines.


*** Comment:
I'm not sure that this is the desired HTML output. I would have expected
<TR>
<TD><A HREF...</A></TD><TD><A HREF...</A></TD>
</TR>

*** This can be easily accomplished by adding [TD] before [A HREF and {/TD]
in place of, or before or after [BR] in [12]

But there also seems to be no reason for building _mypth in any case -
wouldn't
<TD><A HREF...</A></TD>
<TD><A HREF...</A></TD>
....

accomplish the same thing in HTML - that is, one line for EACH filename,
with appropriate /TR and TR tokens at appropriate points?
 
There is a lot in all this.
It is the most advance cmd I worked at until now.

My html can be reviewed has you point but this 'madness' made me find a lot
about:

subroutines
@echo off
THEN the PARSE-TIME value of _conteur is substituted, and the loop is
The escape character is lost once stored using the SET

and design wich I was not up too yet. Do something, create the thing,
fix-it, design it and forget about it.


:o)

You are so good.
Thank you
 
billious said:
You haven't shown the loop. Batch can be sensitive to context - your code
fraction is too small.

IF you have

set _conteur=49
for %%i in (some list of values) do (
set /a _conteur=%_conteur%+1
if %_conteur% EQU 50 goto :eof
@echo %_conteur%
)

THEN the PARSE-TIME value of _conteur is substituted, and the loop is
executed as

for %%i in (some list of values) do (
set /a _conteur=49+1
if 49 EQU 50 goto :eof
@echo 49
)

To change this behaviour, you can invoke DELAYED EXPANSION mode

SETLOCAL ENABLEDELAYEDEXPANSION
set _conteur=49
for %%i in (some list of values) do (
set /a _conteur=!_conteur!+1
@echo before : %_conteur% after : !_conteur!
if !_conteur! EQU 50 goto :eof
)

BUT this creates its own problems - any environment changes made after a
SETLOCAL ENABLEDELAYEDEXPANSION instruction are backed-out by a matching
ENDLOCAL or by reaching end-of-file, which is an implicit ENDLOCAL.

This is true, however, it is possible for code within a setlocal/endlocal
block to effect environmental changes. I often use it this way:

call:getInfo info
echo/info returned: %info%
pause & goto:EOF

:getInfo
setlocal
rem code here that sets some desired value in local variable data
(endlocal & set info=%data%)
goto:EOF

The trick is that %data% is expanded to the value of the variable BEFORE the
endlocal statement is actually executed.

if it is desired to modify a bunch of variables, it could be done this way
to keep the statement length reasonable{

(
endlocal
(set info=%date)
(set thetime=%time%)
(set thedate=%date%)
rem and etc...
)

/Al
 
Hi,
It is very simply that you forgot to include the 'setlocal' command
(assuming you had your 'goto loop' line in your original batch ) In the case
you left it out: your batch script would exit after it's first run and "
_conteu" would equal 50, right? So then the next time you ran it in the
same Command Window your 50-exit-point would have gone because on it's
second run "_conteu" would begin at 51 and so would never stop.

Including the 'setlocal' command would ensure the batch script would not
actually set anything in the current CMD environment and so would always
start with "_conteu" undefined, and so would exit every time at 50 as
expected. Otherwise the sample is okay (other than you missed out the 'goto
loop' and ':loop' lines in your quote, which, I assume, was a oversight)

----------------- quoted script -----------------

@echo off
setlocal
:loop
set /a COUNT=%COUNT%+1
if %COUNT% EQU 50 goto :EOF
echo %COUNT%
goto loop

----------------- quoted script -----------------

(With the 'setlocal' line, will not fail)

==


Cheers, Tim Meddick, Peckham, London.
 
----------------- quoted script -----------------

@echo off
setlocal
:loop
set /a COUNT=%COUNT%+1
if %COUNT% EQU 50 goto :EOF
echo %COUNT%
goto loop

----------------- quoted script -----------------
Thank you.
 
Back
Top