Solved

pulling info from a text file

Posted on 2003-10-28
10
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

690 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