Solved

VB script to merge logs files together

Posted on 2006-06-25
6
246 Views
Last Modified: 2010-04-07
Hi,
We've got several 100 .log files from an auditing script/program & we want to merge them into one continueous file.
each log file is named [machinename].log and contains a single line with the audit info in.

The output file would have each of these single lines, so that it is easy to import into excel/etc

My first up attempt at a script goes like this:

Dim fso

Const FOR_READING = 1, FOR_WRITING = 2

Set fso = CreateObject ("Scripting.FileSystemObject")
Set folders = fso.GetFolder("c:\audit")
set files = folders.files
Set outputFile = fso.openTextFile("c:\audit\logs.txt", FOR_WRITING)

For Each file In files
  IF right(file.name,4) = ".log" THEN
    Filename = "C:\audit\" & file.name
    inputFile = fso.OpenTextFile(filename, FOR_READING)
      Do While inputFile.AtEndOfStream <> True
         retstring = inputFile.ReadLine
         'wscript.echo restring
         outputFile.WriteLine(retstring)
      loop
    inputFile.close
  END IF
Next

outFile.close


The logic behind the script is
- look in c:\audit
- for each file in that folder
-- open that file
-- read in contents
-- output line to single output file


but it fails on the opening of the inputFile with
Microsoft VBScript runtime error: Object doesn't support this property or method: 'inputFile'


If VBS doesn't let you assign and use variables in place of the filename in OpenTextfile methods, then how should be script do the logic stated above?

This is probably fairly easy - I'm only just starting out teaching myself VBS - but its urgent, so I'm going to assign max points for it! ;)
Cheers.


Update: I've put "set xyz =" and am gettign a bit further.
Fails with a Microsoft VBScript runtime error: Object required: '[string: "C:\audit\1318AKGCR.l"]'
(where "C:\audit\1318AKGCR.l" is most fo teh filename of one of my log files (shoudl be C:\audit\1318AKGCR.log)

0
Comment
Question by:RinceNZ
  • 4
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
Mark_FreeSoftware earned 500 total points
ID: 16980844

try this one:


Const FOR_READING = 1, FOR_WRITING = 2


Dim fso
Set fso = CreateObject ("Scripting.FileSystemObject")
Dim ffolder
Set ffolder = fso.GetFolder("c:\audit")
Dim ffiles
Set ffiles = ffolder.files
Dim ffile
Dim sFileName
Dim sInputFile
Dim retString
Dim sOutPutFile
Set sOutPutFile = fso.OpenTextFile("c:\audit\logs.txt", FOR_WRITING)


For Each ffile In ffiles
  If Right(ffile.Name, 4) = ".log" Then
    sFileName = "C:\audit\" & ffile.Name
    Set sInputFile = fso.OpenTextFile(sFileName, FOR_READING)
      Do While sInputFile.AtEndOfStream <> True
         retString = sInputFile.ReadLine
         'wscript.echo restring
         sOutPutFile.WriteLine (retString)
         retString = ""
      Loop
    sInputFile.Close
  End If
Next

sOutPutFile.Close
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16980853

as you can see i've used some different var names (ffiles for example)
that is because i tested this code in vb
and vb bombs out when reserved words are used
0
 

Author Comment

by:RinceNZ
ID: 16981303
Hi Mark,
Thanks for that - thats got it.... you've got the points! ;)
(Looks like it was a reserved words issue.... I told you I was teaching myself!)

But if you could help with one more little thing, it'd be really appreciated.
The log files are sort of in a xml-type format, so it's a bit messier than I first thought.
eg all on a single line, I have entries like "<hostname>1544wnpc</hostname>" and what I'm ultimately heading towards is a single file (tab/comma delimited) and one of the columns is "hostname"

I've managed to find the original audit script, and I'm right re the XML - its generated via (to continue using "hostname" as the example):
objXMLDoc = CreateObject("Microsoft.XMLDOM")
hostnameTag = objXMLDoc.createElement("hostname")

so is there some way to re-engineer XML elements back into "pure" text? Even just a hint on the items to google for will be fine! (like I said, you've doen 99.9% enough to be rewarded the assigned points!)
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.

 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16982542

normally i don't use the microsoft.xmldom stuff,

i always go for the pure vb Mid, Left, right and instr functions.


so i can't help you with that xml api's,

i can (if you want)

write a script that will convert one document to another



but to write it, i need some pieces text to practise with
0
 

Author Comment

by:RinceNZ
ID: 16982816
ahhh - thats what I thought....
I'm pretty sure I could nut something out with mid/left/right/instr as well
might try a bit of a google first though to get my head around xmldom


cheers. points awarded.
0
 
LVL 13

Expert Comment

by:Mark_FreeSoftware
ID: 16982835

thanks for the points
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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

863 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

26 Experts available now in Live!

Get 1:1 Help Now