Visual Basic para Aplicaciones - Read a specific line of a txt file using vba not using line by line, Leer una Linea especifica

Life is soft - evento anual de software empresarial
 
Vista:

Read a specific line of a txt file using vba not using line by line, Leer una Linea especifica

Publicado por ANONIMO (1 intervención) el 14/04/2015 21:07:51
Buenos días - Good morning

Mi pregunta la escribiré en español y en ingles y espero alguien tenga la solución.

I'll write my question in Spanish and English and I hope someone has the solution.

SPANISH

Creé una Macro en VBA que lee mas de 200.000 archivos de texto (.txt) los cuales tienen diferentes tamaños y cantidad de información dentro de ellos, de cada archivo necesito extraer la información de las 3 primeras lineas y las ultimas 10 lineas, las 3 primeras lineas es fácil ya que uso un "READLINE" 3 veces y voy capturando la información de las tres primeras lineas, para las ultimas 10 lineas las cuales están identificadas por un código inicial hago un ciclo que lee linea por linea hasta que encuentra el numero 3 en un espacio especificado de la linea y de ahí captura la información de cada linea de texto que serian las 10 ultimas, el problema que tengo es que la cantidad de archivos y de información dentro de los archivos aumenta considerablemente cada día y por tener el ciclo que lee cada una de las lineas de cada archivo de texto la Macro se demora cada vez mas a medida que aumenta la información y consume cada vez mas memoria de la maquina.

Necesito un método diferente que no lea linea por linea y que yo le pueda decir lea la linea (renglón) numero 1000 (ejemplo) y capture la información a partir de ahí para ahorrar algo de tiempo, o saber cuantas lineas de texto tiene el archivo .txt y decirle que me lea a partir de (numero total de lineas del txt -(menos) 10 lineas) así me leería las 10 ultimas.

ENGLISH

I created a Macro in VBA that reads more than 200,000 text files (.txt) which have different sizes and quantity of information within them, each file need to extract the information from the first 3 lines and last 10 lines, 3 first lines is easy as using a 'READLINE "3 times and am capturing information from the first three lines, for the last 10 lines which are identified by an initial code I make a loop that reads line by line until it finds the number 3 in a specified area of ​​the line and then capture the information of each line of text that would be 10 last, the problem that I have is that the number of files and information within files significantly increases every day and having the loop that reads each line of each text file is delayed Macro increasingly as more information and consume more and more memory on the machine.

I Need a different method not read line by line and I can tell you read the line (row) number 1000 (example) and capture the information from there to save some time or know how many lines of text have the file .txt and tell me read from (total number of lines of txt - (minus) 10 lines) so I would read 10 last.

Here is the code I currently use
Dejo el código que uso actualmente
Dim fs, f, ts, REG
Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
sFilePath = strFolder & "\" & strFileName

Set f = fso.getFile(sFilePath)

Set ts = f.OpenAsTextStream(1, 0)
REG = ts.READLINE

Ciclo para llegar hasta las ultimas 10 lineas

Cycle to reach the last 10 lines

For J = 1 To 200000
REG = ts.READLINE

If Val(Mid(REG, 6, 1)) = 3 Then
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Read a specific line of a txt file using vba not using line by line, Leer una Linea especifica

Publicado por nn (1 intervención) el 22/04/2015 22:43:40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Sub read_last_10()
Dim hfile%, lSize&, s$, n&, i&, c$
hfile = FreeFile
Open ThisWorkbook.Path & "\test.txt" For Input As #hfile
n = 1
lSize = VBA.LOF(hfile)
For i = lSize To 1 Step -1
    Seek #hfile, i
    c = Input(1, #hfile)
    If c = VBA.Chr(13) Then
       n = n + 1
       If n = 11 Then
          Seek #hfile, i + 2
          Exit For
       End If
    End If
Next
 
Do While Not EOF(hfile)
    Line Input #hfile, s
    MsgBox s
Loop
 
Close #hfile
End Sub
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar