Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write this Batch file In the following exercises remember to: Use comments at the start of the batch according to the SET standard for header

Write this Batch file In the following exercises remember to: Use comments at the start of the batch according to the SET standard for header comments. Create a temporary folder (e.g. C:\tmp) to simplify the creation, editing and testing of your batch file(s). Create, save and execute your batch files from this directory. Assume that all batch files run on the C: drive Once your batch file has been fully developed and is debugged remember to turn echo off in the final version See my notes on the last page of this exercise about accessing, using and manipulating variables within your batch files and command scripts

1.This exercise has you creating a more encompassing command script that will provide a simple menu system. Here are the specific requirements for this script: a. The batch file will be called ProcessText.bat b. The batch file will take 1 parameter at the command line. This parameter represents the pathname of a text file to process c. If the parameter (i.e. the text file) is not provided, your script should display an appropriate error message and end the batch file d. If the text file is provided, your script should check to make sure that the file exists. If not, display an appropriate error message and end the batch file e. If the text file exists, then your script should clear the screen and display a menu with the 4 options as follows: 1.Open in Notepad 2.Open in Word 3.Open in Notepad++ 4.Printf. You will need to develop a simple C program called myChoice.exe. This program will: i. Prompt the user for some input ii. Verify that whatever the user entered is numeric (i.e. they entered a number) If they didnt enter a number then myChoice.exe will return a value of -1 If they entered a number, then myChoice.exe will return this value iii. Please note that this program does not display the menuto the user nor does it check for the valid range of menu options iv. The only thing that this program can prompt the user with is the phrase Please make menu choice g. After the menu in step 5 above is displayed, your script will execute the program called myChoice.exe i. You can assume that your myChoice.exe executable is in the same directory as ProcessText.bat ii. Your script will need to check the ERRORLEVEL variable when the program exits in order to get the return value from the main() function in myChoice.exe h. Depending on the value returned from the program your script must invoke the appropriate action i. Open the correct editor or print the file if the returned value is in the range 1 to 4 Please use the lab computers for the path / installation locations of these editors and launch the programs using this path. Make sure that you have found the actual executable and are using that path information rather than a shortcut or link to the application. You may use Notepads ability to print the text file as demonstrated in the other batch files After this action is taken, the batch file will exit i. If the choice is outside of the allowable range then your script should display an appropriate message to the user and allow them to read it. In this case, the menu should be redrawn and allow the user to choose a valid selection.

Accessing, Using and Manipulating Variables within Batch Files (Reminder)Within a batch file there are several different ways that you access and use variables: 1.To access an ENVIRONMENT VARIABLE (pre-existing) within your batch file A pre-existing environment variable is one that already exists within the command prompt that you are using to run your batch file. You can see all of these environment variables by typing set at the command prompt. An example pre-existing environment variable is USERNAME In order to access the value currently held in this variable you use the syntax %USERNAME% within your batch file This syntax is also used to access environment variables that are created within your command prompt while your batch file is running. For example the ERRORLEVEL environment variable that is used to communicate between a program and the OS 2.Your batch files can also create variables to use if you like For example, if a batch file I am developing needs to create a variable to temporarily hold a value (e.g. myValue=2) which will be used later on in the batch. I create the variable with the following line in the batch file :SET myValue=2 Later on when I want to access and use the variables value I can access it a number of different ways by either accessing %myValue or %myValue%. I suggest that you access it using %myValue% 3. As you know, your batch file also is capable of taking command-line arguments (i.e. arguments entered at the command line along with the name of the batch file in order to launch it). You access these variables only using the syntax %1 (for the first command line argument), %2, %3 ... all the way up to the last allowable command line argument %9 When it comes time to use these variables values in IF statements (or any other comparison statement): You need to remember that the batch file / command script interpreter is a strings-basedinterpreter meaning that it cannot do numerical comparisons. It can only do string comparisons. O So if I have a variable called myValue (and its been set to a value of 2) O And I need to check in my batch file if the current value in the variable is 3 or not, I cannot use the line: IF myValue==3 O Instead I need to execute the following line for the logic to work IF myValue==3 Doing this string comparison also comes in handy when I need to check if a variable has a value assigned to it at all (to make sure it is not empty or blank) IF someVariable== Here is an explanation of the variable naming that is used in a FOR ... DO statement: As you saw in the example, a sample FOR statement might look like: FOR %%f in (*.txt) DO So whats with the %%f?!? O You need to appreciate the fact that there is a variable called f which is used to hold a filename (of the pattern *.txt) each time through the loop O In order to get the value of the f variable (i.e. the filename) then we are using the %f scheme to gain access to this local variable O The extra % in the FOR statement effectively works as a pointer to the %f value which allows the FOR statement to reuse the f variable each time through the loop O Within the FOR loop whenever we want the current value of the f variable we use %%f

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions