Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

Read the first line from every file in a folder

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
codysheilds
Asked:
codysheilds
  • 2
1 Solution
 
AmazingTechCommented:
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
 
AmazingTechCommented:
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
 
Shift-3Commented:
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
 
codysheildsAuthor Commented:
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now