# Importing selected data from text files

I have a large number of text files in a folder with different names. These are the output data files from an experiment. Each text file has first 50 lines of text, followed by four columns of numerical data. For some analysis, I want to import the data in all these files to Mathematica in form of tables. However, there are two problems I am facing, will appreciate any help in this regard.

First, the names of these large number of text files are randomly chosen, and I don't want to mention them manually. Is it possible to tell Mathematica to import all the text files in a folder irrespective of their names?

Second, while importing the data from each file, I want to crop first 50 rows and two columns (2 & 3). Basically I need numerical data in columns 1 and 4 only, from row 51 to end. How to do it? Each set of these two columns (2Xn table) can be named as the name of the text file from which they are taken.

thanks

• I think this answers your fist question: mathematica.stackexchange.com/a/159922/1783 For the second it will depend on how exactly the data appears in the file. May 12, 2021 at 14:03
• Yes, but issue remains of giving the file names. The text files which I want to import, are named randomly, I cant define them by any string. I want a method by which I give the path for the folder containing them, and all the files in this folder are imported. May 12, 2021 at 15:43
• That is exactly what the answer above does.. it finds all the .txt files in a directory and then you can read them in. May 12, 2021 at 17:51

I assume you have the data in comma separated files. Otherwise you will have to adjust the "Import" statement. The following will import all files and return an array with each elements the requested data of a single file:

names = FileNames[All, "path to your directory"];
(data = Import[#, "Data"]; data = data[[51 ;;, {1, 4}]]) & /@ names


If you want to do some work on the data sequentially, you may write:

(data = Import[#, "Data"]; data = data[[51 ;;, {1, 4}]]; ==>Here statements for work on data) & /@ names

• Thanks. First part works perfect. However, still couple of issues in the second part. One, the text files have data separated by space in rows. How to make sure that the data getting imported has comma between numbers for each row. Second thing which I noticed, the data I want to import does not necessarily starts from row 51 always. It varies from 51-53 in different files. However, one thing is common in all the files. The row just above the required data always has first word "Point". Can we tell Mathematica to import data from the row next to the one in which it sees the word "Point". May 13, 2021 at 2:41

In case you only know that the data starts after a row with "Point" and the data have a form like:

you can proceed as:

names = FileNames[All, "path to your directory"];
(data = Import[#, "Data"];
Position[dat, "Point"][[-1, 1]];
dat = dat[[1 + Position[dat, "Point"][[-1, 1]] ;;]]
dat = dat[[All, {1, 4}]]
) & /@ names