The command prompt window

The Command Prompt
The command prompt window
The command prompt (sometimes referred as DOS window) is the command line non-graphic environment. In this course we will be using the command prompt for most of our exercises. To access command prompt, go to the search window, type CMD or prompt, select command prompt and press enter. The window command will open. Type CScript and press enter, you should see list of options for the CScript.
The command prompt commands
In the command prompt commands, you should be familiar with the basic commands, here are the most used command.
>Dir “You type Dir, the “>” before Dir to indicate that it is from the command prompt”
>Dir To list the content or the Folder / Directory.
>cls To clear the content of the screen
>MD To Make directory
>CD To change directory
>Dir > myresults.txt The “>” after Dir to direct the results into a file named myresults.txt
>Type myresults.txt To see the content of file myresults.txt
To Do:
From the command prompt
>CD\ Press enter to go to the root folder
>MD entd261 Press enter to create a folder name entd261, this is going to be your working folder for this course
>CD entd261 Press enter to change to entd261 folder.
For more commands visit: 21 CMD Commands All Windows Users Should Know
The Editor
You need an editor to write your code. You can use Notepad.exe, but it is preferable an editor with a line number. When you run the code, if you have an error, then engine will refer you to the error by the line number. I recommend NotePad++ or TextPad both are available to download for free
To Do: (Please complete the code below! You’ll want to practice BEFORE completing the weekly assignment.)
Open a new file in the editor you selected and type the following
WSH.Echo “Hello ENTD261” à type the code, do not copy and paste.
Save the code as MyVBScript01.vbs in \ENTD261 folder
ENTD261>Script MyVBScript01.vbs
entd261>cscript vb01.vbs
You will see
Hello ENTD261
Now you are ready to do programing

VBS – Variables
Variable is a temporary place in memory used to store values
We are going to create a simple calculator to demonstrate the programing elements
fnum = 5 ‘ fnum is a variable contain number
snum = 7 ‘ snum is a variable contain number
Msg = “Thank you” ‘ is a variable contain string.
WSH.Echo (“The total =” & total) ‘ & operator is concatenation operator to print number and strings together You can also do it like this WSH.Echo “The total =”, total
To Do: (Please complete the code below! You’ll want to practice BEFORE completing the weekly assignment.)
Open a new file and type the following
We are going to create a simple calculator to demonstrate the programing elements
fnum = 5 ‘ fnum is a variable contain number
snum = 7 ‘ snum is a variable contain number
total = fnum + snum ‘ total is a variable
Msg = “Thank you” ‘ is a variable contain string.
WSH.Echo (“The total =” & total) ‘ & operator is concatenation operator to print number and strings together the & is like converting numbers to string automatically.
WSH.Echo (Msg)
Note the { ‘ } single quote is a comment, the engine will ignore any writing right to the single quote.
Save the above code in ENTD261\ as MyVBScript02.vbs
Run the code
ENTD261> CScript MyVBScript02.vbs
The above code is not useful, because the values are hard coded, we want to be able to pass values to the script without changing the code. The solution is use argument from the command line. To understand the arguments, copy the following code in MyVBScript03.vbs;
The command to capture an argument in VBScript is WScript.arguments(0) for the first argument, WScript.arguments(1) for the second argument, WScript.arguments(2) for the third argument and so on. “cint” is a function to convert string to integer. The arguments by default are string.
fnum = cint(WScript.arguments(0)) ‘fnum is a variable contain number from first argument
snum = cint(WScript.arguments(1)) ‘snum is a variable contain number from second argument
total = fnum + snum ‘total is a variable
WSH.Echo (“The total =” & total) ‘ & operator is concatenation operator to print number and strings together
To run the above code
ENTD261> CScript MyVBScript03.vbs 12 15
You will get
The total =27
Try it with different variables
VBS – Loops
Loops are mechanism to repeat the same command(s) several times. In the code below, we will loop from a value to a value using the For-next loop. Below is the general structure for the for Next-loop
For I = F to T
Commands to repeat
Next
To Do: (Please complete the code below! You’ll want to practice BEFORE completing the weekly assignment.)
To understand the for loop, copy the following code into MyVBScript04.vbs
The & operator is the concatenation operator, it is used in the print (Echo) function to concatenate numbers and strings.
fnum = cint(WScript.arguments(0))
snum = cint(WScript.arguments(1))
For I = fnum to snum
WSH.Echo (“I =” & I) ‘ & operator is concatenation operator to print number and strings together
Next
To run the above code, do this
ENTD261> CScript MyVBScript04.vbs 2 5
VBS – If-then Structure
The if-then or the decision-making structure is an evaluation of an expression using If-then. If the value is True, the statements under Then condition(s) are Executed. If the Condition is False, the statements under Else Part would be executed.
To Do: (Please complete the code below! You’ll want to practice BEFORE completing the weekly assignment.)
Create the following code and run as you did before.
‘ This is comment block
‘ This program will accept two arguments from the command line
‘ The program will run the loop only if the first number is greater than the second number
fnum = cint(WScript.arguments(0))
snum = cint(WScript.arguments(1))
If snum > fnum then
For I = fnum to snum
WSH.Echo (“I =” & I) ‘ & operator is concatenation operator to print number and strings together
Next
Else
WSH.Echo (“ Second number must be greater than first number”)
End if
VBScript Objects
VBScript has a very rich object library that allows you to do many tasks around the system. For example if you want to work with file systems such as reading / writing files, checking folders and so on you need to use a library named “Scripting.FileSystemObject” to use this library, you need to use the command “set”. Here is an example
fn=”xyz”
Set fs = CreateObject(“Scripting.FileSystemObject”)
If fs.FolderExists(fn) then
msgbox “good folder”
else
msgbox “bad file”
end if
Set folder = fs.GetFolder(fn)
wscript.echo “Folder Results File Created”
Set files = folder.Files
For Each file in files
wscript.echo “Name: ” & (file.name)
wscript.echo “Size: ” & (file.size)
wscript.echo “Date: ” & (file.datecreated)
Next
Create a program from the above code as you did in prior exercises and run it.
fn is variable to hold folder name, change xyz to a folder/directory name you have on your system
set fs, fs is an object name (you can name it whatever) for the file system
fs.FolderExist(fn), is how to check the existing of a folder FolderExist is a function in the library “Scripting.FileSystemObject” that takes the folder name (fn) as parameter.
Set folder = is another variable to hold folder information
fs.GetFolder(fn), GetFolder is a function to get the folder content
Set files = is a variable to hold file information from the folder object
folder.Files
For more information about the FileSystemObject Introduction
What Is a FlowerBox?
‘—————————————-‘
”’This is a flowerbox.”’
”’You’d generally have comments here with the program name, your name,
”’assignment information and program description
‘—————————————-‘
Note: You’ll have to modify this slightly for each language per how comments are achieved. See this example in PERL : flower box comments

Requirements Points
Comment block (flowerbox) with Instructions on how to run the code with examples. 20
Code documentation and comments. 10
Assignment code including creating command line parameter 70
TOTAL POINTS 100