I have a text file which has a header with several lines. Is there any to skip the lines, then read the rest of the file to a table? I have a sample file but don't know how to attached it.
Thank you in advance
Thank you in advance
Or delete the lines you want to skip before importing the text file.
One of the easiest ways is to strip the headers off in a DOS command file before importing. Use the FOR command. The /F option has a Skip parameter to skip a specified number of lines.
The FOR /F command can parse a file line by line on defined delimiters and use ECHO to write the selected parts in any order with any delimiter to another file. I have used it to strip page headers by recognising the strings on those lines.
A single line with two nested FOR commands can selectively parse a whole folder of text files and combine them into one output file for import. The same task in VB wouold take dozens of lines.
DOS commands and batch files can be run from VBA with the Shell command.
The FOR command is just one of many ways to deal with importing text files that don't completely fit the Access import spec system. As a DOS devotee from the days when that is all we had, I used it until I learnt how to read and parse text files as Dave's option (b). Dave's option (a) is good if the structure of the file allows it.
I have some quite complex imports that include steps like identifying the report version from the header and processing accordingly. Some require reconnecting wrapped lines, adding extra fields, converting values etc. So I read the text file line by line, parse it and manipulate the values in code as they are written to a recordset.
Tables store the conditions which are applied to accept or reject lines with certain strings in particular locations, strings that incidate where to start parsing, when to stop parsing etc. This is a very powerful way to manage converting text to fields.
It was complex system to set up but it supports everything from the simplest to the most complex so I use it for everything in preference to the Access spec system. Moreover it supports imports to SQL server too.
I most cases I don't actually import the values to tables but load them into fabricated ADO recordsets for display in a form or report or export to Excel workbooks. (Where I work, all the reports coming from the Unix system are stored as text files and staff need them more conveniently presented.)
The recordset field specifications and value conversion information is also held in tables.
Most Sys Admins would use a scripting language like Perl (for example) which handles file parsing much more effeciently.
Just keeping the options open GalaxiomAnd I bet it won't beat Perl's parsing techniques.
![]()
findstr /X ^.............................................40.* input.txt >> output.txt
It tries... some regex.In the same spirit.
DOS can use a regular expression to parse on position.
I'm sure it is.DOS is marvellous at doing stuff with one line commands.
I wouldn't want you losing a life now would I. Unless you have 9 lives.And I wouldn't bet my life on Perl winning a race against it, especially if you count the time taken to write the script.