Solved I have a batch script using if else method

November 29, 2016 at 23:59:23
Specs: Windows 7
I have a batch script using if else loop, but i don't know why it's is working. Could anyone help my code. There is some problem with it. when i press u it should run iperf.exe -u but it is not, it is running default.

====================code================================================


@echo off

:start
cls
echo ==============================
echo 1)Iperf Server
echo 2)Iperf Client
echo 3)exit
echo ==============================
set /p choice="Please enter choice: "

if '%choice%'=='1' goto iperfserver
if '%choice%'=='2' goto iperfclient
if '%choice%'=='3' goto end

:iperfserver
set default=iperf.exe -s -w 512k
set /p type="Please enter u for udp, enter for default tcp: "
if '%type%'=='' echo "iperf command: %default% "
%default%
else if '%type%'=='u'
%default% -u


See More: I have a batch script using if else method

Reply ↓  Report •


✔ Best Answer
November 30, 2016 at 01:37:31
AFAIK, batch can't do IF and ELSE on the same line. Also if you want to use multiple commands in an IF, you need to use parentheses (except if you use & and/or &&, but that gets messy really quick)

:iperfserver
set default=iperf.exe -s -w 512k
set /p type="Please enter u for udp, enter for default tcp: "
if '%type%'=='' (
        echo "iperf command: %default% "
	%default%
) else (
	if '%type%'=='u' (
		%default% -u
	)
)

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)

message edited by RainBawZ



#1
November 30, 2016 at 00:45:44
Try it without the quotes in the "if" clauses:

if %choice%==1 goto iperfserver

etc.


Reply ↓  Report •

#2
November 30, 2016 at 01:20:00
N, i don't think it's the if %choice%==1 goto iperfserver.
I think it should be this problem.
if '%type%'==' ' echo "iperf command: %default% "
%default%
else if '%type%'=='u'
%default% -u

the output i wrong, if i press 1, adn press u, it should run iperf.exe -u
output:
==============================
1)Iperf Server
2)Iperf Client
3)exit
==============================
Please eneter choice: 1
Pleaase enter u for udp, enter for default tcp: u
------------------------------------------------------------
Server listening on TCP port 5001
TCP window size: 512 KByte
------------------------------------------------------------



Reply ↓  Report •

#3
November 30, 2016 at 01:31:59
So you've tried removing the quotes and it still doesn't work? Or are you just saying that you don't think that will work?

Reply ↓  Report •

Related Solutions

#4
November 30, 2016 at 01:37:31
✔ Best Answer
AFAIK, batch can't do IF and ELSE on the same line. Also if you want to use multiple commands in an IF, you need to use parentheses (except if you use & and/or &&, but that gets messy really quick)

:iperfserver
set default=iperf.exe -s -w 512k
set /p type="Please enter u for udp, enter for default tcp: "
if '%type%'=='' (
        echo "iperf command: %default% "
	%default%
) else (
	if '%type%'=='u' (
		%default% -u
	)
)

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)

message edited by RainBawZ


Reply ↓  Report •

#5
November 30, 2016 at 17:30:39
i have try and it doesn't work

Reply ↓  Report •

#6
November 30, 2016 at 18:41:30
Hi RainBawZ , it work. Thanks,

Reply ↓  Report •

Ask Question