Please check this code for any further optimization

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BVKmohan
Posts: 4
Joined: 16 Mar 2011 05:57
Location: mumbai
Contact:

Please check this code for any further optimization

#1 Post by BVKmohan » 18 Mar 2011 06:01

Hi Guys,

I am developing a code where function "done_send_to_engine" will take either single or double inputs
so before sending to this function I'll check whether they are null or not using below script snippet
Input Menu

Enter IP's
Start :
End :

so after taking the IP's from the above menu I'll check whether there are 2 IP's or only single IP
single IP can be entered either on Start or End so my code checks and pass it to my processing engine function "done_send_to_engine". If both are filled Start and End then my engine function will pin sweep all those IP's
if either Start or End are entered then my engine will ping only that machine. please review my below code and
check whether any further optimizations can be done.

:validate_inputs
SETLOCAL
set val_1=%1
set val_2=%2
if "%val_1%" EQU "" (set val1=0&&call:check_val_2) else (set val1=1&&call:check_val_2)
:check_val_2
if "%val1%" EQU 0 (call:check_val_2_for_0)
if "%val1%" EQU 1 (call:check_val_2_for_1)
:check_val_2_for_0
if "%val_2%" EQU "" (call:null_input) else (call:done_send_to_engine %val_2%)
:check_val_2_for_1
if "%val_2%" EQU "" (call:done_send_to_engine %val_1%) else (call:done_send_to_engine %val_1% %val_2%)
ENDLOCAL

Thanks

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

Re: Please check this code for any further optimization

#2 Post by DosItHelp » 23 Mar 2011 21:21

BVKmohan,

how about simply:

Code: Select all

if "%1%2" EQU "" (call:null_input) else (call:done_send_to_engine %1 %2)


if both %1 and %2 are empty then the concatenation "%1%2" will be empty and the condition be TRUE.

Otherwise:
if %1 is empty then %1 resolves to an empty string and you end up with "call:done_send_to_engine %2"
The extra space you get in the argument list should not hurt.

if %2 is empty then %2 resolves to an empty string and you end up with "call:done_send_to_engine %1 "
The extra space you get in the argument list should not hurt.

if both %1 and %2 are set then you end up with "call:done_send_to_engine %1 %2"

DosItHelp? :wink:

BVKmohan
Posts: 4
Joined: 16 Mar 2011 05:57
Location: mumbai
Contact:

Re: Please check this code for any further optimization

#3 Post by BVKmohan » 29 Mar 2011 13:32

Yes ! you made it simple :shock:

Thanks for the help :)

Post Reply