Page 1 of 1

Why here echo double times?

Posted: 22 Oct 2024 07:38
by goodywp
Hi all,

This code below I was using before. But today I used it again just found that it echo double times, I need know why that happened...

The Sample.txt just list as NAXAUTO-1 NAXAUTO-2 NAXAUTO-3 NAXAUTO-4 NAXAUTO-5 NAXAUTO-6 NAXAUTO-7 NAXAUTO-8 NAXAUTO-9 NAXAUTO-10 NAXAUTO-11 NAXAUTO-12

I used this code to test each values in this Sample.txt has been echo properply but it echo twice the second round just ECHO is off

Code: Select all

@echo off

set TC_ID=Sample
for /f "tokens=1-12 delims=," %%A in (C:\CATA_AUTO\ata-master\BVT\BVTTAG\FAPS\%TC_ID%.txt) do call :ManualRepl %%A %%B %%C %%D %%E %%F %%G %%H %%I %%J %%K %%L

:ManualRepl
set "var1=%~1"
set "var2=%~2"
set "var3=%~3"
set "var4=%~4"
set "var5=%~5"
set "var6=%~6"
set "var7=%~7"
set "var8=%~8"
set "var9=%~9"
shift
set "var10=%~9"
shift
set "var11=%~9"
shift
set "var12=%~9"

echo %var1%
echo %var2%
echo %var3%
echo %var4%
echo %var5%
echo %var6%
echo %var7%
echo %var8%
echo %var9%
echo %var10%
echo %var11%
echo %var12%

The output as below:
NAXAUTO-1
NAXAUTO-2
NAXAUTO-3
NAXAUTO-4
NAXAUTO-5
NAXAUTO-6
NAXAUTO-7
NAXAUTO-8
NAXAUTO-9
NAXAUTO-10
NAXAUTO-11
NAXAUTO-12
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.

Why it echo twice and I need clean output
Thanks

Re: Why here echo double times?

Posted: 22 Oct 2024 08:47
by Lucky4Me
put an exit /b or goto :eof between the for /f and :ManualRepl

Code: Select all

for /f "tokens=1-12 delims=," %%A in (C:\CATA_AUTO\ata-master\BVT\BVTTAG\FAPS\%TC_ID%.txt) do call :ManualRepl %%A %%B %%C %%D %%E %%F %%G %%H %%I %%J %%K %%L
exit /b
:ManualRepl

Code: Select all

for /f "tokens=1-12 delims=," %%A in (C:\CATA_AUTO\ata-master\BVT\BVTTAG\FAPS\%TC_ID%.txt) do call :ManualRepl %%A %%B %%C %%D %%E %%F %%G %%H %%I %%J %%K %%L
goto :eof
:ManualRepl

Re: Why here echo double times?

Posted: 22 Oct 2024 11:16
by goodywp
Oh Ya Thanks for picking up the mistake...

Re: Why here echo double times?

Posted: 23 Oct 2024 05:51
by miskox
Maybe you would like to know the explanation:

Just run this:

Code: Select all

@echo off
echo BEFORE
for /L %%f in (1,1,3) do call :DOIT %%f
echo AFTER
goto :EOF

:DOIT
echo %1
goto :EOF
Output:

Code: Select all

e:\>a
BEFORE
1
2
3
AFTER
e:\>
As you can see FOR command calls : DOIT three times and then execution continues after this FOR command. After the ECHO AFTER there is a GOTO :EOF command which exits the .cmd (or you could send the execution flow to somewhere else) and because of this it never reaches the : DOIT subprogram again - your code did that: after FOR completion it continued with the :ManualRepl subprogram.

Saso

Re: Why here echo double times?

Posted: 24 Oct 2024 12:02
by goodywp
Great! it really help to understand the steps!
Thank Saso!!!!