Solved Batch Runs Twice, Why?

July 15, 2020 at 06:13:04
Specs: Windows 10
Hi All,

i have 2 batch files, the first one sets the parameter, then passes it to the second batch file, but for some reason, the choice menu runs 2 times on the second batch file, not sure why, any help would be appreciated.

Batch 1 Code:
@echo off
setlocal enabledelayedexpansion
for /F "tokens=1,2" %%i in (Hosts1.txt) do call :process1 %%i %%j
:process1
set "VARA1=%1"
set "VARB2=%2"
call ex2.bat %1 %2
:end


BATCH FILE 2 CODE:
@Echo off
setlocal enabledelayedexpansion

:begin

Echo %~1 %2
echo Select a task:
echo =============

echo 1) With Dragon
echo 2) Without Dragon
echo 3) Exit Program
echo -
set /p op=Type option:
if "%op%"=="1" goto op1
if "%op%"=="2" goto op2
if "%op%"=="3" goto op3


echo Please Pick an option:
REM goto begin


:op1
echo Backup Starting With Dragon
exit /b

:op2
echo Backing Up, Bypassing Dragon
exit /b

:op3
echo please exit
Exit /b


See More: Batch Runs Twice, Why?


#1
July 15, 2020 at 14:48:43
I don't know exactly your aim ? but try to make it in one batch file :

@echo off
setlocal enabledelayedexpansion
for /F "tokens=1,2" %%i in (Hosts1.txt) do call :process1 %%i %%j

:process1
set "VARA1=%1"
set "VARB2=%2"
call :begin %VARA1% %VARA2%
Pause & Exit

setlocal enabledelayedexpansion

:begin
cls
Echo %~1 %2
echo Select a task:
echo =============

echo 1) With Dragon
echo 2) Without Dragon
echo 3) Exit Program
echo -

set /p op=Type option:
if "%op%"=="1" goto op1
if "%op%"=="2" goto op2
if "%op%"=="3" goto op3


echo Please Pick an option:


:op1
echo Backup Starting With Dragon
exit /b

:op2
echo Backing Up, Bypassing Dragon
exit /b

:op3
echo please exit
Exit


Reply ↓  Report •

#2
July 15, 2020 at 19:21:26
i have but the same thing repeats, and your example also runs the menu option 2 times.

my aim was to run ex1.bat, it takes Myhosts.txt which has a few hostnames in it followed by a username as such: Desktop-D34K59, SmithR

ex1 takes that info, and passes it to Ex2.bat for command processing, but everytime i add this line to the batch, the batch file always runs 2 times for some reason. but the line that seems problematic is this one: it parses the myhost.txt, for the hostnames

for /F "tokens=1,2" %%i in (Hosts1.txt) do call :process1 %%i %%j

:process1
set "VARA1=%1"
set "VARB2=%2"

message edited by 99serge


Reply ↓  Report •

#3
July 15, 2020 at 21:51:40
✔ Best Answer
maybe need an exit:
@echo off
setlocal enabledelayedexpansion
for /F "tokens=1,2" %%i in (Hosts1.txt) do call :process1 %%i %%j
::---------- add exit here
exit /b


:process1
set "VARA1=%1"
set "VARB2=%2"
call ex2.bat %1 %2
:end


Reply ↓  Report •

Related Solutions

#4
July 24, 2020 at 10:06:16
thanks it worked good.

have a good one


Reply ↓  Report •

Ask Question