Link to home
Start Free TrialLog in
Avatar of meade470
meade470

asked on

Scripting

Need help creating a bat file!?

Hi I am in a scripting class and I just do not get it. I could use any guidance. First I will post the assignment then What I have so far

Assignment

You are to develop a command script that displays a list of options to the user, prompts for input, and
runs the function selected. The menu interface should have the following characteristics:
¿ At command file startup, the color should be set to white text on a blue background and the
screen should be cleared
¿ The menu options should be centered on the screen
¿ A title for your utility should be displayed at the top of the menu
¿ The date and time should be displayed at the top of the menu directly after or under the title
¿ The screen should be cleared before displaying the menu after commands are run
The menu should include the following options:
1. List drivers installed on PC
2. Get file security info (prompt user for file name)
3. List current running tasks
4. Kill task (prompt user for task name)
5. Display a text file on the screen (prompt user for file to display)
6. List installed printers
7. Show Hostname
8. Show IP addresses
9. Show DNS servers
10. Show Default gateway
Other considerations:
¿ Your script should include appropriate comments documenting your code
¿ You may rearrange the menu options in any way that you wish
¿ You should display a prompt for the user to input an option
¿ Output that runs more than the length of the command window should be paged with the user
prompted to press a key to view the next screen.
¿ The user should be given time to examine the output of their selection before the menu is
redisplayed.

What I have so far

@echo off
REM Color Change
color 1f
REM clear screen
REM menu choices
TITLE network menu
echo please choose an option
ECHO 0) list drivers installed on PC
ECHO 1) get file security info
echo 2) list current running tasks
ECHO 3) kill task
ECHO 4) display a text file on the screen
ECHO 5) list installed printers
ECHO 6) show hostname
ECHO 7) SHOW IP addresses
ECHO 8) Show DNS servers
ECHO 9) show default gateway
choice /c:0123456789
IF ERRORLEVEL 9 ipconfig
IF ERRORLEVEL 8 nslookup
IF ERRORLEVEL 7 ipconfig /all
IF ERRORLEVEL 6 hostname
IF ERRORLEVEL 5 net view zach-pc | find /i "print">printers.txt
IF ERRORLEVEL 4
IF ERRORLEVEL 3
IF ERRORLEVEL 2
IF ERRORLEVEL 1
IF ERRORLEVEL 0
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of meade470
meade470

ASKER

Haha yeah I figured it was worth a shot..  Thanks for the help though this did help a lot!
Great, I'm glad I could help ...

Have a nice day,

best regards,

ZOPPO
Hi meade470.  I realise that this question is closed, and that it was answered as well as is possible within the constraints of the "homework" rule by Zoppo, but I thought that I might just add a couple of pointers should you ever need to create another batch file.

The CHOICE program in Windows 95, 98, ME, 2000 with Support Tools or Resource Kit  installed, was removed in XP, then reintroduced with changes in 2003, Vista, and 7 (http://ss64.com/nt/choice.html).  Some people obtained the choice.exe from Win2000 and put it in their C:\Windows\System32 folder in XP rather than having to change all their batch files to use the alternative SET option (http://ss64.com/nt/set.html):

SET /P INPUT=Prompt String To Display.

This prompts for input and sets the input string to a variable named in the command.  In the above example the variable name is INPUT. The value stored in the variable can then be echoed, tested, etc. using %INPUT%.  The variable will contain the same values until overwritten by some other string from another SET command.  Using SET /P you don't have to test the %ErrorLevel% return which can be confusing (http://ss64.com/nt/errorlevel.html)

You can use the IF command to test using different methods (http://ss64.com/nt/if.html):

if "%INPUT%"=="SomeValue"
if not "%INPUT%"=="SomeValue"

if %INPUT% EQU SomeValue
if %INPUT% NEQ SomeValue

and can add the /I switch so that it ignores the case of the value when using alphabetic characters.

When running another command from a batch file you can do this in a couple of different ways:

You can CALL it so that control is passed to it and it then returns control back to the same place in the batch file that called it once the other process finishes.  You can CALL an external EXE, BAT, CMD, or other executable program file, you can CALL a recognised internal "DOS" command, and you can CALL a :LABEL within the same batch file. (http://ss64.com/nt/call.html)

As Zoppo states, the GOTO LABELNAME is most commonly used for this type of thing (http://ss64.com/nt/goto.html).  The  echo immediately followed by a dot/period/full-stop just echoes a blank line to the screen and can be handy for spacing things out.  

:MENU
cls
echo.
echo.
echo A. Some Menu Option Here
echo B. Another Menu Option Here
echo The other menu options listed here
echo H. Help
echo Q. Quit
echo.
SET /P INPUT=Type your choice here:

REM All the IF tests here
IF /I %INPUT% EQU Q GOTO :EOF
IF /I %INPUT% EQU H GOTO :HELP

REM etc, etc, etc

:HELP
echo.
MORE < helpfile.txt
echo.
pause
GOTO MENU

Note that you do not need a label named  :EOF.  It just means "end of file" and simply goes there and exits.  You can instead have an  :END  label, and use that as your "Quit" label, and have a few cleanup commands there before it closes the batch file if required.  

The easiest way to test a menu and your :Labels is to use ECHO instead of the program command and pause processing like this:

:DNSCHECK
REM cls
echo This is the DNSCHECK Label
pause
goto MENU

:LISTTASKS
REM cls
echo This is the LISTTASKS Label
pause
goto MENU

Start with a framework like that, and then add your program commands and remove the pauses once you know that your menu is jumping to the correct labels and then returning to the menu again.

I hope this helps.