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
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


0