[batch] give some advice/correct

March 18, 2010 at 01:30:19
Specs: Windows XP
Hi everyone!

Please help.
I wrote my first batch script. Pleas look and give me some advice/correct. btw- maybe this script will halpful for somebody ;)


@echo off
TITLE TE WATCH SCRIPT
REM MichalM


set NLM=^


set NL=^^^%NLM%%NLM%^%NLM%%NLM%

cls
GOTO start

:start
COLOR 4f
color
mode con:cols=80 lines=45

CLS
echo.
echo.
echo ŰßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßŰ
echo Ű ŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰ Ű
echo Ű Ű Ű Ű
echo Ű Ű xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Ű Ű
echo Ű Ű * zzzzzzzzzzzzzzz* Ű Ű
echo Ű Ű _______________________________________________________ Ű Ű
echo Ű Ű [ 1 ] - Linux Ű Ű
echo Ű Ű [ 2 ] - Windows Ű Ű
echo Ű Ű Ű Ű
echo Ű Ű Ű Ű
echo Ű Ű [ 3 ] - ADD HOST Ű Ű
echo Ű Ű [ 4 ] - EDIT HOSTS Ű Ű
echo Ű Ű [ 5 ] - CAT HOSTS TABLE Ű Ű
echo Ű Ű [ 6 ] - OPEN HOSTS TABLE Ű Ű
echo Ű Ű [ 7 ] - REFRESH DNS Ű Ű
echo Ű Ű Ű Ű
echo Ű Ű [ 8 ] - RESTART Ű Ű
echo Ű Ű [ 9 ] - END Ű Ű
echo Ű ŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰŰ Ű
echo ŰÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜŰ
echo.
echo.
echo ===============================================================================
set /p choice=Please enter your choice. choice =
if not '%choice%'=='' set choice=%choice:~0,1%
echo ===============================================================================
echo.

IF %choice%==1 GOTO linux
IF %choice%==2 GOTO windows
IF %choice%==3 GOTO addnewhost
IF %choice%==4 GOTO edithosts
IF %choice%==5 GOTO cathosts
IF %choice%==6 GOTO openhosts
IF %choice%==7 GOTO refreshdns
IF %choice%==8 GOTO restart
IF %choice%==9 GOTO exit

echo "%choice%" is not valid please try again
echo.
PAUSE >nul
GOTO start

:linux
cls
echo.
echo =============================================================================== %NL% %NL%
set /p host=" enter hostname of server ---- "
pping %host% 22 1000 |find "connected." >nul
if errorlevel 1 echo %NL%%NL%%NL%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! %NL%%NL% ERROR --- PING CONNECTION REFUSED TO PORT 22 !!!
%NL%%NL%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! & PAUSE & GOTO ipYNlinux
pping %host% 3311 1000 |find "connected." >nul
if errorlevel 1 echo %NL%%NL%%NL%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! %NL%%NL% ERROR --- PING CONNECTION REFUSED TO PORT 3311 !!!
%NL%%NL%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! & PAUSE & GOTO ipYNlinux
echo %NL%%NL%===============================================================================
echo %NL%%NL% OK %NL%%NL%===============================================================================
PAUSE >nul
GOTO start

:ipYNlinux
choice %NL%%NL%%NL%DO YOU WANT TO ENTER IP ADDRESS OF YOUR SERVER ? /c:yn
if errorlevel 1 GOTO ipY
if errorlevel 2 GOTO ipN
:ipY
echo %NL%%NL%%NL%PLEASE WAIT! LOADING ADD MODULE.
GOTO linuxip
:ipN
echo GO OFF
GOTO start

:linuxip
cls
echo.
echo =============================================================================== %NL% %NL%
set /p ip="enter ip address of server ---- "
pping %ip% 22 1000 |find "connected." >nul
if errorlevel 1 echo %NL%%NL%%NL%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! %NL%%NL% ERROR --- PING CONNECTION REFUSED TO PORT 22 !!!
%NL%%NL%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! & PAUSE & GOTO error_all
pping %ip% 3311 1000 |find "connected." >nul
if errorlevel 1 echo %NL%%NL%%NL%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! %NL%%NL% ERROR --- PING CONNECTION REFUSED TO PORT 3311 !!!
%NL%%NL%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! & PAUSE & GOTO error_all
echo %NL%%NL%===============================================================================
echo %NL%%NL% OK %NL%%NL%===============================================================================
GOTO addhostYN

:windows
cls
echo.
echo =============================================================================== %NL% %NL%
set /p host=" enter hostname of server ---- "
pping %host% 23 1000 |find "connected." >nul
if errorlevel 1 echo %NL%%NL%%NL%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! %NL%%NL% ERROR --- PING CONNECTION REFUSED TO PORT 23 !!!
%NL%%NL%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! & PAUSE & GOTO ipYNwindows
pping %host% 3389 1000 |find "connected." >nul
if errorlevel 1 echo %NL%%NL%%NL%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! %NL%%NL% ERROR --- PING CONNECTION REFUSED TO PORT 3389 !!!
%NL%%NL%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! & PAUSE & GOTO ipYNwindows
echo %NL%%NL%===============================================================================
echo %NL%%NL% OK %NL%%NL%===============================================================================
PAUSE >nul
GOTO start

:ipYNwindows
choice %NL%%NL%%NL%DO YOU WANT TO ENTER IP ADDRESS OF YOUR SERVER ? /c:yn
if errorlevel 1 GOTO ipY
if errorlevel 2 GOTO ipN
:ipY
echo %NL%%NL%%NL%PLEASE WAIT! LOADING ADD MODULE.
GOTO linuxip
:ipN
echo GO OFF
GOTO start

:windowsip
cls
echo.
echo =============================================================================== %NL% %NL%
set /p ip="enter ip address of server ---- "
pping %ip% 23 1000 |find "connected." >nul
if errorlevel 1 echo %NL%%NL%%NL%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! %NL%%NL% ERROR --- PING CONNECTION REFUSED TO PORT 23 !!!
%NL%%NL%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! & PAUSE & GOTO error_all
pping %ip% 3389 1000 |find "connected." >nul
if errorlevel 1 echo %NL%%NL%%NL%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! %NL%%NL% ERROR --- PING CONNECTION REFUSED TO PORT 3389 !!!
%NL%%NL%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! & PAUSE & GOTO error_all
echo %NL%%NL%===============================================================================
echo %NL%%NL% OK %NL%%NL%===============================================================================
GOTO addhostYN

:addhostYN
choice %NL%%NL%%NL%DO YOU WANT TO ADD %ip% %host% TO HOSTS TABLE ? /c:yn
if errorlevel 1 GOTO addY
if errorlevel 2 GOTO addN

:addY
if exist c:\WINDOWS\system32\drivers\etc\hosts GOTO fileexist
if not exist c:\WINDOWS\system32\drivers\etc\hosts GOTO filenotexist

:fileexist
echo %NL%%NL% OK %NL%%NL%===============================================================================
echo %NL%%NL%%NL% PLEASE WAIT! ADDING %host% TO HOST TABLE! %NL%%NL%
GOTO didexist

:didexist
if "%host%"=="" (echo No name entered. & GOTO start)
type c:\WINDOWS\system32\drivers\etc\hosts | find /I "%host%" && GOTO hostexist
echo.%ip% %host%>> c:\WINDOWS\system32\drivers\etc\hosts
echo.
echo.
echo %hostname% HAS BEEN ADDED TO HOSTS TABLE!
echo.
echo.
PAUSE
GOTO start

:hostexist
echo.
echo.
echo HOSTNAME %host% EXIST IN HOSTS TABLE
echo.
echo.
GOTO start


:filenotexist
echo %NL%%NL% FILE HOSTS in c:\WINDOWS\system32\drivers\etc\ NOT EXIST!
%NL%%NL%===============================================================================
PAUSE >nul
GOTO start

:addN
echo GO OFF
GOTO start

:addhost

echo %ip% %host% >> c:\WINDOWS\system32\drivers\etc\hosts
PAUSE
GOTO start

:error_all
echo %NL%%NL%%NL% YOU DONT HAVE CONNECTION WITH %host% on address %ip%!!!!
echo.
echo.
echo CONTACT WITH SERVER ADMINISTRATOR
echo.
echo.
echo.
echo.
echo GO TO MENU START...
PAUSE >nul
GOTO start

:addnewhost
cls
echo.
echo =============================================================================== %NL% %NL%
set /p host=" enter hostname of server ---- "
echo.
echo.
set /p ip=" enter ip address of server ---- "
echo %NL%%NL%===============================================================================
echo %NL%%NL% OK %NL%%NL%%NL%%NL%===============================================================================
GOTO didexist

:edithosts
cls
IF EXIST c:\WINDOWS\system32\drivers\etc\hosts (
echo.
echo ===============================================================================
echo YOUR HOSTS TABLE HAS BEEN OPENED TO EDIT
echo =============================================================================== %NL% %NL%
start cmd /k edit c:\WINDOWS\system32\drivers\etc\hosts
) ELSE (
GOTO filenotexist
)
PAUSE
GOTO start

:cathosts
cls
IF EXIST c:\WINDOWS\system32\drivers\etc\hosts (
echo.
echo ===============================================================================
echo HOSTS TABLE
echo =============================================================================== %NL% %NL%
type c:\WINDOWS\system32\drivers\etc\hosts
) ELSE (
GOTO filenotexist
)
PAUSE
GOTO start

:openhosts
cls
IF EXIST c:\WINDOWS\system32\drivers\etc\hosts (
echo.
echo ===============================================================================
echo YOUR HOSTS TABLE HAS BEEN OPENED
echo =============================================================================== %NL% %NL%
start notepad c:\WINDOWS\system32\drivers\etc\hosts
echo.
echo.
) ELSE (
GOTO filenotexist
)
PAUSE
GOTO start

:refreshdns
cls
IF EXIST c:\WINDOWS\system32\drivers\etc\hosts (
echo.
echo ===============================================================================
echo REFRESHING DNS TABLE
echo =============================================================================== %NL% %NL%
ipconfig /flushdns
echo.
echo.
) ELSE (
GOTO filenotexist
)
PAUSE
GOTO start

:restart
GOTO start

:exit
cd\
echo All Rights Reserved. 2010/2011 - TE Watch Team //POLAND
PAUSE >nul
exit


See More: [batch] give some advice/correct

Report •


#1
March 18, 2010 at 04:02:58
That's quite some code to quickly check ... one thing already:

if errorlevel 1 GOTO addY
if errorlevel 2 GOTO addN

should be

if errorlevel 2 GOTO addN
if errorlevel 1 GOTO addY

since errorlevel-checks actually work "if errorlevel 2 or less" instead of "if errorlevel exactly 2". Therefor you need to go down the values, whatever the values you are checking (in case of multiple values being checked, as here 1 and 2)


Report •

#2
March 18, 2010 at 04:07:55
if errorlevel x *OR MORE*


=====================================
Helping others achieve escape felicity

M2


Report •

#3
March 18, 2010 at 04:23:12
ha! now i know why he react in any else key ;)

Report •

Related Solutions


Ask Question