VBScript Split Function
The VBScript split function makes easy work out of searching text files for information. This function separates each line by a character, chosen by the programmer, and places each word in the line as an item in an array.
For example, the comma is commonly used to separate csv files. Each line in the csv file can be separated and placed in an array. Any other method within vbscript would require calling out a number of characters from the left or right or looking for specific strings within the line.
The split function in VBScript is a handy function that should be considered by programmers who want to create an array out of a text file which is separated by comma, colon, or even spaces. All can be handled with the split function.
split VBScript Code
Animal Text File
Create Text File
Go to Start-->All Programs-->Accessories-->Notepad. Go to File-->Save As and save the file as “C:\Temp\animals.csv”. Copy the line containing the animals into the animals.txt file by selecting the line, pressing “Ctrl” + “C”, then selecting in the animals.txt file and pressing “Ctrl” + “V” . dog,cat,horse,cow,pig,goat,sheep,chicken,bird,mouse
Select File-->Save within Notepad. Exit Notepad
Create Split Function VBScript
Go to Start-->All Programs-->Accessories-->Notepad. Go to File-->Save As and save the file as “C:\Temp\split.vbs”. Adding the .vbs file extension creates an executable VBScript file. Copy the following code into the split.vbs file by selecting the code just to the left of “Option Explicit”, dragging the cursor just to the right of “WScript.Quit(), press “Ctrl” + “C”, select within the split.vbs file, then press“Ctrl” + “V”. Select File-->Save within Notepad. Exit Notepad
Set fso = CreateObject("Scripting.FileSystemObject")
Set wshShell = CreateObject("WScript.Shell")
Set objTextFile = fso.OpenTextFile ("C:\temp\animals.txt",1,false)
'Read animals.txt file.
strLine = objTextFile.Readline
'Setup array using "split".
animalArray = split(strLine,",")
For i = 0 to Ubound(animalArray)
wscript.echo "I ride my " & animalArray(2) & " and milk my " & animalArray(3) & "."
'Close animals.txt file
Set fso = Nothing
Set wshShell = Nothing
Set objTextFile = Nothing
Numbered Animals Text File
Run the VBScript to demonstrate the split function.
Navigate to “C:\Temp\” and double click on the split.vbs file. A message will pop up stating that I ride my horse and milk my cow. The VBScript is reading the text file, dividing the line by commas and making each word an item in the array. Since arrays start with 0, the third item in the array is labeled with a 2. The picture to the right actually shows how the items are numbered by the array.
More by this Author
Don’t overlook the power of this scripting language! Administrators, developers, analysts and others have all discovered the power of Microsoft VBScript. VBScript, sometimes called “VBScripting” is a...
VBScript has a few powerful methods to search through different types of text files to acquire information. This information can be passed on to other scripts, used to make business decisions or even used to identify...
Learn how to get rid of the musty, mildew-y smell coming from your window AC unit and stop the stink!
No comments yet.