Solved

VB script to merge logs files together

Posted on 2006-06-25
6
245 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
Comment Utility

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
Comment Utility

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
Comment Utility
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
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

 
LVL 13

Expert Comment

by:Mark_FreeSoftware
Comment Utility

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
Comment Utility
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
Comment Utility

thanks for the points
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

763 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

7 Experts available now in Live!

Get 1:1 Help Now