p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Access VBA (http://p2p.wrox.com/forumdisplay.php?f=80)
-   -   need sample code for read txt file (http://p2p.wrox.com/showthread.php?t=60137)

lawsoncobol July 16th, 2007 04:44 AM

need sample code for read txt file
 
Hi all,

     I want a sample code, which can read data from txt file and store it in a table , actually my requirement is to count lines of code i have written in a txt file ( total lines of code - comments lines (* at seventh column )

Please help
mail id : sparsh_mba2003@yahoo.com

Thanks and Regards
Lawson, COBOL

mmcdonal July 16th, 2007 07:03 AM

Can you post a couple lines from the text file? This will help in knowing how to capture the data. Then post what the table will look like that you want to store the data in, WITH some sample data. For example, if your file is:

SampleTextFile001.txt
- line of code
- line of code
- //comment line
- line of code

And your table is:

tblCodeFile
CodeFileID - autonumber PK
FileNameAndPath - text
LinesOfCode - number

And your sample data is:

tblCodeFile
1
C:\SampleData\SampleTextFile001.txt
3


then it will be easier to write knowing this structure and outcome.

To get started, you will want to use Scripting.FileSystemObject, which is documented on MSDN.



mmcdonal

lawsoncobol July 16th, 2007 07:52 AM

Text file name - C301PD ( it does not have extension as txt )

location C:\c

- line of code

000500 C301S1-TRANSACTION SECTION 10.
000600******************************************** ********************
000700 C301S1-START.
000800
000900 PERFORM 200-EDIT-TRAN
001000 THRU 200-END.
001100
001200 IF (NO-ERROR-FOUND)
001300 PERFORM 400-PROCESS-TRAN
001400 THRU 400-END.
001500
001600 GO TO C301S1-TRANSACTION-END.
001700
001800******************************************** ********************
001900 200-EDIT-TRAN.
002000******************************************** ********************
002100
002200 PERFORM 210-EDIT-ACCESS
002300 THRU 210-END.
002400
002500 IF (ERROR-FOUND)
002600 GO TO 200-END.


//commented line

where ever there is * in seventh column( place) starting from left that is commented line
example

001800******************************************** ********************

is a commented line above


My Table defination would be

1.field1-line number in the file (Primary key )
2. field2-char in 7th column of file

ignore rest of it

My requirement
To count what is excat number of lines of code
= Total number of lines - commented lines count

so i need to find count of lines which has * at the seventh place in file in each line





Thanks and Regards
Lawson, COBOL

mmcdonal July 16th, 2007 07:58 AM

You can do this without storing every single line in the table. The code counter can be made to do this arithmetic for you. From this sample, it could return and store 19 (22-3=19). Do you want it to store this value? Or do you want it to store every complete line of code, or do you want it to store every line of code at the 7th chraracter, etc? Answer that question and I can post the code.


mmcdonal

mmcdonal July 16th, 2007 08:30 AM

This code returns 19 from your sample text file:

sFile = "C:\SampleCodeFile.txt"


Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(sFile) Then
    Set objFile = objFSO.OpenTextFile(sFile, 1)
    Do Until objFile.AtEndOfStream
        sLine = objFile.ReadLine
        iComment = InStr(sLine, "*")
        If iComment = 0 Then
            iCount = iCount + 1
        End If
    Loop
End If

iCount = 19.

Are you using DAO or ADO to add this to a table? I use ADO so would do this:

sSQL = "SELECT * FROM tblMyTextCounterTable"

Set rs = New ADODB.Recordset
rs.Open sSQL, CurrentProject.Connection, adOpenDynamic, adLockOptimistic

rs.AddNew
rs("FileNameAndPath") = sFile
rs("LinesOfCode") = iCount
rs.Update

rs.Close




mmcdonal


All times are GMT -4. The time now is 10:43 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.