Solved

VB script to merge logs files together

Posted on 2006-06-25
6
254 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
[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
  • 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
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!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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.
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.
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…
Suggested Courses
Course of the Month2 days, 18 hours left to enroll

622 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