Solved

Read part of file...

Posted on 2003-11-16
13
508 Views
Last Modified: 2010-05-01
I need some VB code to read only part of a file, it's been a while since I worked with VB but I know it can be done, just don't know how anymore.

Example, I know the last time I imported a file it was 77,600 kb upon current import I know the file is 78,777 kb so I know I only want to read the difference ie. 1,177 kb

How to read the 1,177 kb last part of a file in VB?

FYI: I will be using this in a DTS package.

Then how to loop over this 1,177 kb of data within the DTS package? I need to do the following comparison for each line

IF LEFT(currentRow, 3) IS "***"
Then do something here
otherwise
Just import the row.
0
Comment
Question by:Tacobell777
[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
13 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9761365
You can use the seek functin to move the file pointer once the file is open.

Seek #1, 77600

Idle_Mind
0
 
LVL 17

Author Comment

by:Tacobell777
ID: 9761399
That means I need to read the whole 80 Mb or in some cases 300 Mb...
0
 
LVL 7

Expert Comment

by:_agj_
ID: 9761431
'seek' will get u to the position u want. So u wont have to read the whole 80Mb or so as u say.
After doing a seek, jus read in the rest of the file.
0
Independent Software Vendors: 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!

 
LVL 17

Author Comment

by:Tacobell777
ID: 9761511
Would any code samples be possible? Maybe I did not make it clear, but I am no VB programmer, I used it for a little while and now I need to use it in a DTS package...
0
 
LVL 2

Accepted Solution

by:
CD-Softy earned 100 total points
ID: 9762264
This will do what you want...

Const FileName As String = "C:\test.txt" 'path and name of your file
Const ReadFrom As Long = 77600  ' this can be changed as needed
Dim DTSFile As Integer
Dim LatestData As String
Dim sBuffer As String
Dim arrData() As String
Dim iCurrentLine As Long

'open the file & import the data you want
DTSFile = FreeFile
Open FileName For Input As DTSFile
Seek #DTSFile, ReadFrom
While Not EOF(DTSFile)
    Line Input #DTSFile, sBuffer
    LatestData = LatestData & sBuffer & vbCrLf
Wend
Close #DTSFile

'place the data into an array for easy looping
arrData = Split(LatestData, vbCrLf)

'loop thro the data getting what you want
For iCurrentLine = LBound(arrData) To UBound(arrData)
    If Left$(arrData(iCurrentLine), 3) = "***" Then
        'found the line you want - your code here
        Debug.Print arrData(iCurrentLine)
    Else
        'discard unwanted data or save to file here
    End If
Next


CD
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 100 total points
ID: 9762374
or this
You could use this to go through the file
 
    Dim MyRecord As Byte ' or whatever. You may already have an appropriate user type defined in a module
    'Dim MyRecord as String * 10 if you use a string type, it's best to fix its size
   
    Dim Start As Long
    Dim l As Long
    Dim f As Integer
   
    Start = 100
    f = FreeFile
    Open "c:\scandisk.log" For Binary As #f
        l = Start
        Do Until EOF(f)
        Get #f, l, MyRecord ' Read l'th record
        'Process the record
        l = l + 1
        Loop
    Close #f
0
 
LVL 17

Author Comment

by:Tacobell777
ID: 9809567
Guys,

I have been trying and trying but none of these scripts work within a MS SQL DTS package....
It looks as if the syntax needs to be different...
0
 
LVL 34

Assisted Solution

by:arbert
arbert earned 50 total points
ID: 9815916
Exactly--the above examples are VB.  Why don't you add a text file connection to your DTS package and you can tell it what row to start on.

If you want more control than that, you can setup an ActiveX script task to read in the data using the file system object.  Yet another option, if you configure the Microsoft Text Driver, you can even process queries against the text file to look at the "LEFT()" characters...

You would have got better DTS responses if you posted this in SQL Server topic group...


Brett
0
 
LVL 17

Author Comment

by:Tacobell777
ID: 9816424
I did post this in the MS SQL Server area, but nothing at all there. At least here I am getting somewhere ;-))

What your saying sounds interesting...... but....
How to do that? I mean I worked with DTS before and created connections to text files but have not seenw hat you are refering to, unless you mean "Skip rows"? If so, how do I know which row I last imported?
0
 
LVL 34

Expert Comment

by:arbert
ID: 9817815
Ya, I was talking about skip rows.  You could write the last row imported to a text file (ini file) and then use the dynamic properties task to read it and update your "skip rows" property.

Also, sounds like the Microsoft Text file driver could be a good option.  I'd set it up with the format of your file and try it.

Sorry didn't see your original post in SQL Server group....

Brett
0
 
LVL 34

Expert Comment

by:arbert
ID: 9817821
You deleted your question in the SQL group--how long did you leave it open???
0
 
LVL 17

Author Comment

by:Tacobell777
ID: 9853841
I did not get what I was after (which could have been my own fault, not asking the question in the right category) I split the points up anyway.

thanks
0
 
LVL 34

Expert Comment

by:arbert
ID: 9854044
You could follow up on the above suggestions and note that you didn't get what you wanted.  We can't read minds and know you're not getting what you want....
0

Featured Post

Independent Software Vendors: 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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

688 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