Solved

Scripting

Posted on 2013-10-23
4
489 Views
Last Modified: 2013-11-02
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
0
Comment
Question by:meade470
  • 2
4 Comments
 
LVL 30

Accepted Solution

by:
Zoppo earned 500 total points
ID: 39596541
Hi meade470,

well, I guess you know we're not allowed to do your homework, but we can give you hints what's wrong with your code and we're allowed to point you to the right direction when you're stuck.

So here are some hints about some generic things:

- to clear the screen you can use CLS command
- the index you use with CHOICE is wrong, if the CHOICE isn't cancelled the ERRORLEVEL is set to the 1-based index, so your IFs should check for values 1-10 instead of 0-9
- to let the user input some data (for functions 2, 4 and 5) you can use SET /P (check it's help)
- to break the output of commands to pages you can use MORE (check it's help)
- if I understand it correctly the script should not terminate after a command was executed, so you need to jump to the echo please choose an option instruction after the command was executed. This can be done using GOTO (check it's help)
- your dummy statements like IF ERRORLEVEL 4 produce syntax errors so you can't test anything below this line. To avoid this you can simply add i.e. a REM like this:
   > IF ERRORLEVEL 4 REM
- the command you use to display printers doesn't show the installed printers, instead it shows the printers shared from the hardcoded PC "zach-pc". I.e. here I found a way to retrieve the list of installed printers using WMIC, maybe you can use this: http://community.spiceworks.com/topic/365018-list-network-printers-via-batch-file
- IMO you should filter the output in some cases (i.e. to display only DNS-Servers from output of IPCONFIG)

Beside these I think you need to find the missing commands to retieve info about drivers, tasks, drivers ...

I hope this helps,

ZOPPO
0
 
LVL 2

Author Comment

by:meade470
ID: 39597137
Haha yeah I figured it was worth a shot..  Thanks for the help though this did help a lot!
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 39597183
Great, I'm glad I could help ...

Have a nice day,

best regards,

ZOPPO
0
 
LVL 38

Expert Comment

by:BillDL
ID: 39618642
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.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now