Solved

pulling info from a text file

Posted on 2003-10-28
10
236 Views
Last Modified: 2010-04-17
Hello all
I am trying to write a script that will execute a command on a server using Wshell.run piping it to a text file then reading the text file. Compare the output of the text file to a table in a database if they are not the same pull it out of one table and insert it into another table.

Hoe do I go about reading the text file and setting variables for it

The text file looks like the below
SESSIONNAME           CLIENTNAME          TRANSPORT  ADDRESS
console                        0                        Conn              wdcon
ica-tcp                        65536                    Listen          wdica
rdp-tcp                       65537                   Listen            rdpwd
>ica-tcp#37             magnus.hjorleifsson    5                 Active rdpwd
                                                                  3                  Idle
                                                                 2                    Idle            

I need to pull any instance of ica-tcp#xx along with the username and compare that with a table to see if the ica-tcp#xx and username match the text fileresults if so do nothing else run something

Thanks in advance
Magnus
0
Comment
Question by:magsdtev
  • 3
  • 3
10 Comments
 
LVL 3

Expert Comment

by:monkesdb
ID: 9635769
a simple solution is as follows

*UNTESTED*

#include <vector>
#include <string>

typedef struct tag_details {
  string session_name;
  string client_name;
  string transport;
  string address
} details;


...

vector<details> info;
#define i   info.size() - 1       //I agree this is not fastest but it's easier to understand the code

fstream theFile("TheFile.txt");

while(!theFile.eof() && !theFile.fail())
{
  info.push_back(details());
  theFile >> info[i].session_name >> info[i].client_name >> info[i].transport >> info[i].address
}

if( theFile.fail() )
 // file not properly formatted.

#undef i

...

that should fill 'info' with all the information from the file you can then access it using

info[record_number].field;

remember that record 0 will contain the top line which are only labels so start at 1.
0
 
LVL 3

Expert Comment

by:monkesdb
ID: 9638654
oh, and don't forget (like i did) to close the file when you're finished

theFile.close();
0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9640415
first tokenize using a tokenize function at vbcrlf
then check each token's 7 left characters if they match "ica-tcp" extract the username and run your if condition.

a tokenize function which returns an array is at
http://www.a1vbcode.com/coderedirect.asp?type=snippet&ID=2095
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:magsdtev
ID: 9651264
monkesdb
What language is your post in?  It looks like perl.  Do you have it in VBS?

bhagyesht
Do you have an example of this
0
 
LVL 9

Accepted Solution

by:
bhagyesht earned 250 total points
ID: 9655334
magsdtev: find example below
the code was written in vb so make any changes if required for vbs ie the variable declaration etc

Private Sub Command1_Click()
    Dim mytokens()
    Dim i
    Dim fileData
    fileData= ReadAllTextFile(<myfilename>)
    mytokens = tokenise(fileData, vbCrLf)
    For i = LBound(mytokens) To UBound(mytokens)
        If Len(mytokens(CInt(i))) > Len("ica-tcp#xx") Then
            If Mid(mytokens(CInt(i)), 3, Len("ica-tcp#xx") - 2) = "ica-tcp#" Then
                'your if condt
                MsgBox "true"
            Else
                ' your else condt
            End If
        End If
    Next i
End Sub


Public Function tokenise(strToTokenise As String, strUsingWhat As String) As String()
    Dim i
    Dim j
    Dim mystr()
    Dim strLen
    strLen = Len(strToTokenise)
    If strLen > 0 Then
        ReDim Preserve mystr(0)
    End If
    i = 1
    j = 0
    While i <> 0
        j = InStr(i, strToTokenise, strUsingWhat)
        If j <> 0 Then
            mystr(UBound(mystr)) = Mid(strToTokenise, i, j - i)
            i = j + 1
            ReDim Preserve mystr(UBound(mystr) + 1)
        Else
            mystr(UBound(mystr)) = Mid(strToTokenise, i)
            i = j
        End If
    Wend
    tokenise = mystr
End Function


Function ReadAllTextFile(strFilename)
   Const ForReading = 1
   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.OpenTextFile(strFilename, ForReading)
   ReadAllTextFile = f.ReadAll
   f.Close
   Set f = Nothing
   Set fso = Nothing
End Function
0
 
LVL 3

Expert Comment

by:monkesdb
ID: 9661461
hmm you could do that..

or you could be more sensible and use a function that does the same thing... Split.

myArray = Split(strToTokenise, strUsingWhat)

what could be more simpler.

and the language was C++, you know that obscure language that nobody uses.

doofus. chickenhead.

0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9668705
monkesdb : myArray = Split(strToTokenise, strUsingWhat)

isnt this quite similar to the tokenize function i wrote?

Public Function tokenise(strToTokenise As String, strUsingWhat As String) As String()

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question