?
Solved

Read the first line from every file in a folder

Posted on 2009-05-12
4
Medium Priority
?
258 Views
Last Modified: 2012-05-06
Hi Guys,

I am trying to figure out a way to read the first line of all the text files in a certain folder and write the results into another file for analysis.  The files are all datetime stamped and vary in their names and the first line of each file has a broad summary.  What I really want is to get the name of the file and its correspoding log summary into say a tab, comma or any other delimted file so that I can compare and archieve all the files.

I greatly appreciate all the help.

Cody
0
Comment
Question by:codysheilds
[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
  • 2
4 Comments
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24366747
Try this.
@ECHO OFF
Set Folder=C:\Folder
Set Results=C:\Results.csv
 
(for /f "tokens=*" %%a in ('dir /a-d /b "%Folder%\*.*"') do for /f "tokens=1,* delims=]" %%b in ('find /v /n "" %%a ^| find "[1]"') do echo "%%a","%%c")>%Results%

Open in new window

0
 
LVL 21

Accepted Solution

by:
AmazingTech earned 2000 total points
ID: 24366755
Opps. Forgot the folder.
@ECHO OFF
Set Folder=C:\Folder
Set Results=C:\Results.csv
 
(for /f "tokens=*" %%a in ('dir /a-d /b "%Folder%\*.*"') do for /f "tokens=1,* delims=]" %%b in ('find /v /n "" "%Folder%\%%a" ^| find "[1]"') do echo "%%a","%%c")>%Results%

Open in new window

0
 
LVL 38

Expert Comment

by:Shift-3
ID: 24366850
Paste the script below into a text file with a .vbs extension.  Customize the value of the strFolder variable on line 5 with the location of the folder containing the files.  Customize the value of the strExt variable on line 6 with the file extension to search for.  Running the script will create a comma-delimited text file containing each file's name and first line.


Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
 
strFolder = "c:\files"
strExt = "txt"
strOutput = "output.csv"
 
On Error Resume Next
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutput = objFSO.OpenTextFile(strOutput, ForWriting, True)
 
objOutput.WriteLine "File Name,First Line"
 
Set objFolder = objFSO.GetFolder(strFolder)
 
For Each objFile in objFolder.Files
    strFileExt = objFSO.GetExtensionName(objFile.Path)
    
    If LCase(strFileExt) = LCase(strExt) Then
        Set objTextFile = objFSO.OpenTextFile(objFile.Path, ForReading, False, TriStateUseDefault)
        strLine = objTextFile.ReadLine
        objTextFile.Close
        
        objOutput.WriteLine Chr(34) & objFile.Name & Chr(34) & "," & Chr(34) & strLine & Chr(34)
    End If
Next
 
objOutput.Close

Open in new window

0
 

Author Closing Comment

by:codysheilds
ID: 31580616
I ran it on close to 200 files (80MB) and it works exactly the way I wanted it to.  Thank you so very much!!  Its now time to run it on 5 Gig worth of files.  Thanks again.

Cody
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

719 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