Sign in to follow this  
Followers 0
MattX

Read top line on txt files from folder

18 posts in this topic

I have written a basic script which collects some basic data on PCs everytime they logon - [ it's a quick re-hash of something I did ages ago in an old job ]

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=office details.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

Dim $sLogPath = 'c:\windows\' & @ComputerName & ".txt"
   Dim $sLogMsg = "User: "
   Dim $objWord = ObjCreate("Word.Application")
If IsObj($objWord) Then
    $OfficeVerison = $objWord.Version
    Else
    $OfficeVerison = "Not Installed"
EndIf
   _FileWriteLog($sLogPath, $sLogMsg)

   Func _FileWriteLog($sLogPath, $sLogMsg)

   Local $sDateNow
   Local $sTimeNow
   Local $sMsg
   Local $hOpenFile
   Local $hWriteFile
   Local $user
   Local $cname
   Local $ip

   $osversion = @OSVersion
   $servicepack = @OSServicePack
   $sDateNow = "On " & @YEAR & "-" & @MON & "-" & @MDAY
   $sTimeNow = @HOUR & ":" & @MIN & ":" & @SEC
   $user = @UserName
   $cname = @ComputerName
   $ip = @IPAddress1

    Switch $OfficeVerison
    Case "7.0"
        $OfficeVerison = "97"
    Case "8.0"
        $OfficeVerison = "98"
    Case "9.0"
        $OfficeVerison = "2000"
    Case "10.0"
        $OfficeVerison = "2002"
    Case "11.0"
        $OfficeVerison = "2003"
    Case "12.0"
        $OfficeVerison = "2007"
    Case "14.0"
        $OfficeVerison = "2010"
    Case Else
        $OfficeVerison = "Not Installed"
EndSwitch

   $sMsg = $cname & "," & $osversion & "," & $OfficeVerison

   $hOpenFile = FileOpen($sLogPath, 1)

   If $hOpenFile = -1 Then
      SetError(1)
      Return 0
   EndIf

   $hWriteFile = FileWriteLine($hOpenFile, $sMsg)

   If $hWriteFile = -1 Then
      SetError(2)
      Return 0
   EndIf

   FileClose($hOpenFile)
   Return 1
EndFunc

 FileCopy('c:\windows\' & @ComputerName & ".txt", '\\server1\logs',1)

This is working great and I have a folder full of txt files with the info I want [ name, OS and office ver ] each seporated with a comma. What I need to do now is read the top line of every .txt file [ there are almost 500 of them in the folder ] and then dump that into a new file [ csv ] - can someone give me some pointers on how to do this ? Thanks.

Share this post


Link to post
Share on other sites



See FileReadLine function :

FileReadLine ( "filehandle/filename", 1 ) ; 1 for the first line


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

See FileReadLine function :

FileReadLine ( "filehandle/filename", 1 ) ; 1 for the first line

Sorry, should make myself clearer - was aware of that function but I'm wondering how to get a script to read the top line, copy that into a csv then read the top line of the next txt file and copy that into the csv etc. I'm not very good at coding arrays which I think is what's needed as I have around 500 txt files to get the top lines out of.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Try a _FileListToArray, then that array will contain the filename of all the text files in that folder.

Then write a loop:

For $i = 1 to ubound($array)-1

(or for $i = 0 to $array[0])

Read the first line of every $i file (file read line)

Insert that line in a file called CSV (filewrite function)

Make sure the line ends with a @CRLF

Put commas/semi comma where you need

Save file, open in excel.

Edited by PowerCat

Share this post


Link to post
Share on other sites

Try a _FileListToArray, then that array will contain the filename of all the text files in that folder.

Then write a loop:

For $i = 0 to ubound($array)-1

(or for $i = 0 to $array[0])

Read the first line of every $i file (file read line)

Insert that line in a file called CSV (filewrite function)

Make sure the line ends with a @CRLF

Put commas where you need

Save file, open in excel.

Sounds complex [ for someone like me ] but I'm willing to give it a go - I'll make a start but may be back with some code when I can't figure out whats gone wrong !!

Share this post


Link to post
Share on other sites

Sounds complex [ for someone like me ] but I'm willing to give it a go - I'll make a start but may be back with some code when I can't figure out whats gone wrong !!

Paste me a few lines of the output of your script, I'll see what I can whip up :unsure:

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Think of an array as a set of shelves. You can lay out any number of items (items = anything. variable, value, object, etc etc) next to each other on those shelves.

Now each item has a specific position on your shelf, from left most item 1, to however many you have.

Let's say your array (shelf) is called $AnArray, just add a number in bracket to its right to access that position. (tricky: they start at 0!)

$AnArray[0] is the left most value on your array.

$AnArray[1] is the second one, and so on.

Now arrays can be 3 dimensional, but let's not go there now. It won't be necessary for this script.

Have you experimented with #include <array.au3> and using the _ArrayDisplay function?

It gives a great visual look on your array, which increases understanding by 1000%

Edited by PowerCat

Share this post


Link to post
Share on other sites

Paste me a few lines of the output of your script, I'll see what I can whip up :unsure:

I want to have a crack at it myself first [ best way to learn ] - when I get stuck I'll be back - don't worry !!

Share this post


Link to post
Share on other sites

I've made a start on this but I'm now stuck. I've managed to get the list of text files in an Array and as a test I just want to see if I can pull the first line from the first text file [ The msg box ] - once I'm happy with that then I will try and finish the script off by inserting the line into a new file etc. This runs but does not display the msg box - can someone give me a clue as to where I have gone wrong ?

#Include <File.au3>
#Include <Array.au3>

Dim $output = 'd:\output.csv'
Dim $array

$FileList=_FileListToArray('d:\logs')
If @Error=1 Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf

; _ArrayDisplay($FileList,"$FileList")

for $i = 0 to $array[0]
Next

$line = FileReadLine($i)

MsgBox(0, "Line read:", $line)

Share this post


Link to post
Share on other sites

You now just have to complete file output part...

#Include <File.au3>
#Include <Array.au3>

Global $output = 'd:\output.csv'
Global $line

$FileList=_FileListToArray('d:\logs', '*.log', 1)   ; filter only wanted extensions, and don't list folders, only files (option=1)
If @Error Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf

; _ArrayDisplay($FileList,"$FileList")

; here's time to open output file and get it's handle
;;;;;;;;;;;;;;;Global $hdlOut = FileOpen($output, 1 + 8)    ; may require some other option value (see help)

For $i = 0 To $FileList[0]
    $line = FileReadLine($FileList[$i], 1)
    MsgBox(0, "Line read:", $line)      ; this will quickly get boring!
    ; now write it to the output file, using its handle
Next

; now close the output file

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Thanks for that - it certainly helps but for some reason the msg box is popping up but with a null values [ nothing but all the txt files at least have one line of stuff in ] which is odd - what have I missed ?

#Include <File.au3>
#Include <Array.au3>

Global $output = 'd:\output.csv'
Global $line

$FileList=_FileListToArray('d:\logs', '*.log', 1)   ; filter only wanted extensions, and don't list folders, only files (option=1)
If @Error Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf

; _ArrayDisplay($FileList,"$FileList")

; here's time to open output file and get it's handle
;;;;;;;;;;;;;;;Global $hdlOut = FileOpen($output, 1 + 8)    ; may require some other option value (see help)

For $i = 0 To $FileList[0]
    $line = FileReadLine($FileList[$i], 1)
    MsgBox(0, "Line read:", $line)      ; this will quickly get boring!
    ; now write it to the output file, using its handle
Next

; now close the output file

Edited by MattX

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Do the log files have .log extension?

Do they start with a blank line?

What does if you uncomment the _ArrayDisplay?

My bad, I often get bitten there: try this instead!

#Include <File.au3>
#Include <Array.au3>

Global $inputpath = 'd:\logs'
Global $output = 'd:\output.csv'
Global $line

$FileList=_FileListToArray($inputpath, '*.log', 1)  ; filter only wanted extensions, and don't list folders, only files (option=1)
If @Error Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf

; _ArrayDisplay($FileList,"$FileList")

; here's time to open output file and get it's handle
;;;;;;;;;;;;;;;Global $hdlOut = FileOpen($output, 1 + 8)    ; may require some other option value (see help)

For $i = 0 To $FileList[0]
    $line = FileReadLine($inputpath & '\' & $FileList[$i], 1)
    MsgBox(0, "Line read:", $line)      ; this will quickly get boring!
    ; now write it to the output file, using its handle
Next

; now close the output file

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Do the log files have .log extension?

Do they start with a blank line?

What does if you uncomment the _ArrayDisplay?

They have a .txt extension which I changed in the script. They don't start with a blank line - example is:

LAX-SRV6,WIN_2003,2002

When I uncomment the _ArrayDisplay I get a list of the txt files that are in the array. Is the problem the fact there is no blank line ? I chanegd the line setting to 2 but it made no difference.

Share this post


Link to post
Share on other sites

See edited post above (sorry for that)


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Thanks - can you explain why the '\' was added in the filereadline ? Trying to get my head around that.

Share this post


Link to post
Share on other sites

The array of file contains only the filenames, not the path. A backslash is needed between them as well.

Now another mistake I left in: read from 1 in the array (For $i = 1 To ...). The first (0th) element is the number of entries in the array.

Putting it all together you should get that:

#Include <File.au3>
#Include <Array.au3>

Global $inputpath = 'd:\logs'
Global $output = 'd:\output.csv'
Global $line

$FileList=_FileListToArray($inputpath, '*.txt', 1)  ; filter only wanted extensions, and don't list folders, only files (option=1)
If @Error Then
    MsgBox (0,"","No Files Found.")
    Exit
EndIf

Global $hdlOut = FileOpen($output, 1 + 8)   ; may require some other option value (see help)

For $i = 1 To $FileList[0]
    $line = FileReadLine($inputpath & '\' & $FileList[$i], 1)
    MsgBox(0, "Line read:", $line)      ; this will quickly get boring!
    FileWriteLine($hdlOut, $line)
Next

FileClose($hdlOut)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Thanks for your help and the explanation - It works perfectly. I will now be able to use it for other logs I have planned too.

Share this post


Link to post
Share on other sites

Again sorry for having posted erroneous code. I keep being distracted at an unpleasant rate.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0