VBScript IPAddress File Program Lab
Objective
In this lab, students will complete the following objectives:
- create a VBScript program using NotePad++;
- write a two-dimensional array of IP addresses to a text file;
- read the IP addresses text file into a script;
- append new Room, PC, and IP-address data to the text file; and
- use the object Scripting.FileSystemObject.
Lab Overview
Wewill download a start file that contains a two-dimensional array of IP addresses. Rather than accessing this array of IP address using the room and computer index values, we will write the array of IP addresses to a text file named IP_Addresses.csv where each line of the file will contain the Comma-Separated Values (CSV) for room#,computer#,IP_Address. We will then write a separate VBScript program that will append new lines of data to the IP_Addresses.csv file. Last, we will open and read the newly appended IP_Addresses.csv file and display its contents in a meaningful way.
Task 1: Download and Open IP_ArrayFile_start.vbs in NotePad++
- Download the file IP_File_start.zipfrom the Doc Sharing tab in eCollege to your C:\VBScripts folder. Unzip the extracted file (IP_File_start.vbs) and open it in NotePad++.
- Modify the Programmer Headeras needed, and click Save As to save the VBScript file as IP_FileWrite.vbs.
- The line dim ipAddress(5,3) declaresa 6x4, two-dimensional array. The 5 and 3 give the maximum index values. Because indices always start at 0, this is a 6x4 array.
- The lines that follow initialize the array locations with IP addresses. The first index (0..5) represents the rooms 100 through 105. The second index (0..3) represents the four computersin each room.
- The IP address of the third computer in room 104 can be found in the array element or the component ipAddress(4,2). This value is “192.168.10.45.” Look at the array carefully to determine the meaning of the index values.
Task 2: Add the Code to Write the Array Data to a File
- Add the code indicated by the pseudocode shown below to write the array to a text file. Real code lines will be in larger, bold font. Non pseudocode instructions or comments will be in italics.
Define the constants to be used with the CreateTextFile or the OpenTextFile methods.
ForReading = 1
ForWriting = 2
ForAppending = 8
ASCII = 0
Define the variable fileName and initialize it to “C:\VBScripts\IP_Addresses.csv.”
Define the variable ipAddrStr and initialize it to “”.
Set the object identifier fso to the object ScriptingFileSystemObject.
If the file identified by fileNameexists, then delete the file.
endif
Add the following statement. This statement will create a new file object calledipAddrFile. Look at the second and third arguments for theScripting.FileSystemObjectmethod. The second argument can be 1, 2, or 8 (ForReading,ForWriting,ForAppending).Here, we are setting it for writing. The third argument defines the format for the data.The possible values are 0 for 8-bit ASCII characters, -1 for 16-bit Unicode characters, and-2 for default. Here,we will use 0 for standard, 8-bit ASCII characters.
Set ipAddrFile = fso.CreateTextFile(fileName,ForWriting,ASCII)
Add the following comment line.
' Write Room #, Computer # and IP Addresses to File
(Note: The pseudocode is continued on the next page.)
In the pseudocode shown below, nested For loops are used to access the two-dimensional array of IP addresses. The outside loop specifies the room 0..5 (representing rooms 100..105), and the inside loop specifies the computer 0..3 (representing computers 1..4). Two lines of code are in the nested loops. For the first pass through the inside For loop where room=0 and computer=0, ipAddrStris set to the value “100,1,192.168.10.11CrLf.” The values 100 and 1 are calculated from the counter variables room and computer, where the saved room # value is toString(room+100) and the saved computer # valueis toString(computer+1). The toString( )pseudocode indicates the need to convert a numeric value into a string value. In VBScript, all the data must be saved as strings of characters. Because we are saving our IP address data a line at a time, each line must be terminated with a <CarriageReturn+LineFeed(which is commonly referred to as a newline). The second line of code in the inside loop writes the newline terminated-string value of ipAddrStrto the text file. After the file has been written, the file object is used to close the file.
For room = 0 to 5
For computer = 0 to 3
ipAddrStr = toString(room+100) & "," & toString(computer+1) & "," & _
ipAddress(room,computer) & newline
Use ipAddrFile object to write IpAddrStr to file
EndFor
EndFor
Close file using the ipAddrFile object.
- Save your program. Open a Windows CLI as an Administrator. Change the directory to C:\VBScripts, and Run the program using cscript. Use the type command to display the IP_Addresses.csv file. If your Run has any errors or if your file does not display what’s shown below, debug your program and rerun it until you have the correct results.
- When you have achieved the correct results, copy and paste your IP_WriteFile.vbs program from NotePad++ into the specified textbox in your lab-report document. Also, copy the Windows CLI Run and the type IP_Addresses.csv command output into the specified textbox into your lab-report document.
Task 3: Create the IP_Append_Read.vbs Program
The IP_Append_Read.vbs program will create data for a new room with four computers and assigned IP addresses. The program will append these four records (one for each IP address) to the IP_Addresses.csv file. Then, the program will open the IP_Addresses.csv file and display the contents of the file in a one-record-per-line, descriptive format. The new room records that were appended should be displayed.
- Open NotePad++ and create a new file named IP_Append_Read.vbs. Save this new script file in the C:\VBScripts folder. Do not copy and paste the code from your IP_WriteFile.vbs program. This program should be written from scratch.
- Write the code indicated by the pseudocode shown below to write the array to a text file. Real code lines will be in larger, bold font. Non pseudocode instructions, or comments, will be in italics.Where you see a blank line in the pseudocode, put a blank line in your program.
Add an appropriate programmer header
Add the comment line:' Define Constants
Define the following constants
ForReading = 1
ForWriting = 2
ForAppending = 8
Const ASCII = 0
Add the comment line:' Define Program Variables
Define fileNameand initialize it to "C:\VBScripts\IP_Addresses.csv"
Define ipAddrStrand initialize it to ""
Add the comment line:' Define new room Variables
Define newRoomand initial it to "106"
Define comp1_IP and initial it to "192.168.10.59"
Define comp2_IP and initial it to "192.168.10.60"
Define comp3_IP and initial it to "192.168.10.61"
Define comp4_IP and initial it to "192.168.10.62"
Add the comment line:' Define Scripting.FileSystemObject
Set the object identifier fso to the object “ScriptingFileSystemObject”
Add the comment line:' Define String to represent New Room Information
ipAddrStr =
newRoom & ",1," & comp1_IPnewline &
newRoom & ",2," & comp2_IP & newline &
newRoom & ",3," & comp3_IPnewline &
newRoom & ",4," & comp4_IP & newline
Add the comment line:'Append New Room Information to File
If file identified by fileName does not exist Then
Beep Speaker Twice
Display Message:
"File Does Not Exist!!!" & newline &
"You Must Create the File Before You can Read the File !!")
Quit Program
End If
Set File object ipAddrFile using fso.OpenTextFile( )
make sure file is set forAppending and ASCII format
Write ipAddrStr to end of file identified by fileName
Close the file
Add the comment line:' Read and Display IP Address Records from File
Set File object ipAddrFile using fso.OpenTextFile( )
make sure file is set forReading and ASCII format
Add the comment line:
' Read File 1 Line per pass extracting the Room #, Computer # and IP Address
Do UntilipAddrFile is at EndOfStream
room = ipAddrFile.Read 3 characters
ipAddrFile.Skip 1 character
computer = ipAddrFile.Read 1 character
ipAddrFile.Skip1 character
ipAddress = ipAddrFile.Read13 characters
ipAddrFile.Skip newline
Display Message:
"The IP Address in Room " & room & " for Computer " &
computer & " is " & ipAddress
End Loop
Close File
Task 4: Finish the Program and Run it Showing ErrorHandling
- Finish writing your program and save it. Open a Windows CLI as an Administrator. Run your program using cscript. Your Run should look like the following. Note the highlighted records in the run. These are the appended records for room 106.
C:\VBScripts>cscript IP_Append_Read.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
The IP Address in Room 100 for Computer 1 is 192.168.10.11
The IP Address in Room 100 for Computer 2 is 192.168.10.12
The IP Address in Room 100 for Computer 3 is 192.168.10.13
The IP Address in Room 100 for Computer 4 is 192.168.10.14
The IP Address in Room 101 for Computer 1 is 192.168.10.19
The IP Address in Room 101 for Computer 2 is 192.168.10.20
The IP Address in Room 101 for Computer 3 is 192.168.10.21
The IP Address in Room 101 for Computer 4 is 192.168.10.22
The IP Address in Room 102 for Computer 1 is 192.168.10.27
The IP Address in Room 102 for Computer 2 is 192.168.10.28
The IP Address in Room 102 for Computer 3 is 192.168.10.29
The IP Address in Room 102 for Computer 4 is 192.168.10.30
The IP Address in Room 103 for Computer 1 is 192.168.10.35
The IP Address in Room 103 for Computer 2 is 192.168.10.36
The IP Address in Room 103 for Computer 3 is 192.168.10.37
The IP Address in Room 103 for Computer 4 is 192.168.10.38
The IP Address in Room 104 for Computer 1 is 192.168.10.43
The IP Address in Room 104 for Computer 2 is 192.168.10.44
The IP Address in Room 104 for Computer 3 is 192.168.10.45
The IP Address in Room 104 for Computer 4 is 192.168.10.46
The IP Address in Room 105 for Computer 1 is 192.168.10.51
The IP Address in Room 105 for Computer 2 is 192.168.10.52
The IP Address in Room 105 for Computer 3 is 192.168.10.53
The IP Address in Room 105 for Computer 4 is 192.168.10.54
The IP Address in Room 106 for Computer 1 is 192.168.10.59
The IP Address in Room 106 for Computer 2 is 192.168.10.60
The IP Address in Room 106 for Computer 3 is 192.168.10.61
The IP Address in Room 106 for Computer 4 is 192.168.10.62
C:\VBScripts>
- If you did not get the results shown above, you will need to debug your program and Run it again until you get these results. If your IP_Addresses.csv file has become corrupted, rerun the IP_Write.vbs program to recreate this file.
- The .csv extension that we used in our IP_Addresses.csv indicates a comma-separated values file. This kind of file can be opened by Microsoft Excel and Microsoft Access. The file can be easily edited using Excel,andit can even be made into a database in Access by adding some field names. Below is our IP_Addresses.csv file opened in Excel (left) and in Access (right).
- Copy and Paste your completed IP_AppendRead.vbs program sourcecodefrom NotePad++ into the program textbox specified in your lab-report document. Also, copy yourRun from the Windows CLI into the textbox specified in your lab-report document. Submit your completed lab-report document to the iLabDropbox for this week.
1
IP_File_Lab.docx Revision Date: 1103