Solved

pulling info from a text file

Posted on 2003-10-28
10
234 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A short article about problems I had with the new location API and permissions in Marshmallow
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

914 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

15 Experts available now in Live!

Get 1:1 Help Now