Friday, May 02, 2014

Lesson 7 : Working with Text files

In our routine as an administrator, we routinely work with text files. Let us try to understand how to work with them.

A usual logic would be

- Open the file, Loop line by line, display the contents (or process them)

Example 1:

In this example, we try to display the contents of the file that is passed as a parameter at the command line. I have not done validations and assume that a file name is already there. You can add three validations, first check if there is an argument. Second check if the file exists. Third check if it is a text file.  I have a sample file called “sample.txt” in the same folder as the program.

Const ReadMode=1
dim fso
dim fstext
dim txtline

set fso=CreateObject("Scripting.FileSystemObject")
set fstext=fso.OpenTextFile(wscript.arguments(0),ReadMode)

do until fstext.AtEndOfStream
    txtline=fstext.ReadLine
    wscript.echo txtline
loop

Let us check the output

C:\scripts\file-io>01-show-contents.vbs sample.txt //nologo
The quick fox jumped over
the lazy dog. The hare and the
turtle were friends in the forest.
Early bird gets the worm.

C:\scripts\file-io>

Let us make it more useful. At the end, let us say we want to display the total lines in this file.

Const ReadMode=1
dim fso
dim fstext
dim txtline
dim lines
lines=0
set fso=CreateObject("Scripting.FileSystemObject")
set fstext=fso.OpenTextFile(wscript.arguments(0),ReadMode)

do until fstext.AtEndOfStream
    txtline=fstext.ReadLine
    wscript.echo txtline
    lines=lines+1
loop
wscript.echo "--------------------------------------end of file"
wscript.echo "Lines in the file " & lines

  

C:\scripts\file-io>01-show-contents.vbs sample.txt //nologo
The quick fox jumped over
the lazy dog. The hare and the
turtle were friends in the forest.
Early bird gets the worm.
--------------------------------------end of file
Lines in the file 4

C:\scripts\file-io>

What if we needed the lines and words.

a. Use the split function, it splits a string and returns an array

b. use the ubound function to check how many elements are there in the array, and add 1

Const ReadMode=1
dim fso
dim fstext
dim txtline
dim lines, words
dim strword
lines=0
words=0
set fso=CreateObject("Scripting.FileSystemObject")
set fstext=fso.OpenTextFile(wscript.arguments(0),ReadMode)

do until fstext.AtEndOfStream
    txtline=fstext.ReadLine
    strword=split(txtline)
    words=words+ubound(strword) +1
    wscript.echo txtline
    lines=lines+1
loop
wscript.echo "--------------------------------------end of file"
wscript.echo "Lines in the file " & lines
wscript.echo "Number of words   " & words

Sample Output of the same file

C:\scripts\file-io>01-show-contents.vbs sample.txt //nologo
The quick fox jumped over
the lazy dog. The hare and the
turtle were friends in the forest.
Early bird gets the worm.
--------------------------------------end of file
Lines in the file 4
Number of words   23

C:\scripts\file-io>

Let us make it more complete. We do the following changes, we pass the parameter filename with a number, 1=show file, 2=show lines, 3=show words, 4=show all

Code

Const ReadMode=1
dim fso
dim fstext
dim txtline
dim lines, words
dim strword
lines=0
words=0
dim Oput
oput=int(wscript.arguments(1))
set fso=CreateObject("Scripting.FileSystemObject")
set fstext=fso.OpenTextFile(wscript.arguments(0),ReadMode)

do until fstext.AtEndOfStream
    txtline=fstext.ReadLine
    strword=split(txtline)
    words=words+ubound(strword) +1
    if oput=1 or oput=4 then
        wscript.echo txtline
    end if
    lines=lines+1
loop
if oput=1 or oput=4 then
    wscript.echo "--------------------------------------end of file"
end if
if oput=2 or oput=3 or oput=4 then
    wscript.echo "Lines in the file " & lines
end if
if oput=3 or oput=4 then
    wscript.echo "Number of words   " & words
end if

Various Runs

C:\scripts\file-io>01-show-contents.vbs sample.txt 1 //nologo
The quick fox jumped over
the lazy dog. The hare and the
turtle were friends in the forest.
Early bird gets the worm.
--------------------------------------end of file

C:\scripts\file-io>01-show-contents.vbs sample.txt 2 //nologo
Lines in the file 4

C:\scripts\file-io>01-show-contents.vbs sample.txt 3 //nologo
Lines in the file 4
Number of words   23

C:\scripts\file-io>01-show-contents.vbs sample.txt 4 //nologo
The quick fox jumped over
the lazy dog. The hare and the
turtle were friends in the forest.
Early bird gets the worm.
--------------------------------------end of file
Lines in the file 4
Number of words   23

C:\scripts\file-io>

We build more on this in the subsequent chapters.

No comments: