Solved

VB script to merge logs files together

Posted on 2006-06-25
6
249 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Industry Leaders: 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!

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

726 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