Solved

pulling info from a text file

Posted on 2003-10-28
10
233 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now