46

I know how to import one textfile by calling its name

filestring = Import["myfile.tex", "Text"];

Then "filestring" is a string with the myfile content.

How do I import all N text files in one folder? So that I get, for example, a list with N strings.


Bonus: Can I maybe also not only import all at once but specify "get the last three text files" or "get the first ten" out of that folder into my list?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Nikolaj-K
  • 1,485
  • 1
  • 10
  • 14

6 Answers6

70

Have a look at FileNames:

files=FileNames["*.pdf", NotebookDirectory[]]

{"a.pdf","b.pdf","c.pdf"}

will get you a list of all files in the directory where your notebook resides (of course you can choose any path) that match "*.pdf". You can then import the files like this:

Import[#]&/@files

or if you want certain files (look at the help for Part and Span):

Import[#]&/@files[[-3;;-1]] (*last three files*)
Import[#]&/@files[[1;;10]]  (*first ten files*)

If you want to use more arguments with Import like in your question then you can add them after the #, e.g. like this: Import[#,"Text"]&/@files. Otherwise you can save typing effort by choosing the the shorter version Import/@files (as pointed out by @AlbertRetey).

Yves Klett
  • 15,383
  • 5
  • 57
  • 124
  • Ya, I figured the rest before the edit myself, thanks. I now ended up with: FileNames["*.tex", "MY PATH"]; Part[%, 1 ;; 3]; Import[#, "Text"] & /@ % – Nikolaj-K May 06 '12 at 15:39
  • 1
    Consider also using Select, as you can weed out directories via Select[ FileNames[...], !DirectoryQ[#]&], or if they wish to exclude hidden files (i.e. starting with a "." on a unix/linux based system - mac included), the predicate can be expanded to !(DirectoryQ[#] || StringMatchQ[#, "." ~~ ___])&. – rcollyer May 06 '12 at 15:41
  • 5
    @NickKidman sidenote: using % (aka Out) can result in confusion if you don´t take care of the evaluation order (e.g. if you do not put all depending code into one cell). – Yves Klett May 06 '12 at 15:44
  • @YvesKlett: is there a certain reason to use Import[#]&/@files instead of the simpler Import/@files? – Albert Retey May 07 '12 at 19:59
  • @AlbertRetey I just wanted to show the way if the OP wanted to use Import with additional arguments or options. – Yves Klett May 07 '12 at 20:03
  • @YvesKlett, I see, he even used that case in his example, as I've just seen. Probably it would make sense to explain that, but the answer is definitely good enough to be accepted as it is :-) – Albert Retey May 07 '12 at 20:14
5

Well, I dug out this question because I accidentally found another way for getting the paths of files today and I can't help posting it as an answer somewhere XD.

It should be mentioned that, this help page has already told us some ways to insert paths without any Mathematica code while it doesn't tell us that there's a even simple way to get the paths of the files, that is:

Ctrl+C and Ctrl+V.

Now the whole process will be quite simple, first input this incomplete code in the notebook:

Import /@ {}

Then find the files you suppose to import and select them with Ctrl+A, Ctrl+click, Shift+click, etc. Press Ctrl+C and move to the code above, place the cursor between the {} and Ctrl+V, run the code and you'll get the expected result.

I think this solution is as fast as, or even faster than the solutions with Mathematica codes especially when we only want to input a single file or the name of the target files are not so regular.

Here's a screencast:

enter image description here

I've tested this solution in v8 and v9 on Windows platform. Not sure if it will work with other versions and operation systems.

xzczd
  • 65,995
  • 9
  • 163
  • 468
  • 1
    Doesn't work on a mac. Also, when you paste it, it inserts it as a symbol, not a string and doesn't place a comma between multiple strings. So you'll have to manually insert the quotes and commas to use this... waay too much work. – rm -rf Nov 16 '12 at 14:49
  • 1
    This also doesn't work right on Windows 7 with v7, as the strings are inserted without properly escaped backslashes and without commas separating, BUT it works if using Szabolcs's Paste Tabular Data palette. (Table button.) – Mr.Wizard Nov 16 '12 at 14:58
  • @rm-rf I supplied the version information of my Mathematica and a snapshot: those commas and backslashes and quotes are not added by me. – xzczd Nov 17 '12 at 09:33
  • @xzczd Works as described on 10.0.2 on Win 7. Doesn't work with 10.0.2 on OS X Mavericks. – Gordon Coale Jan 13 '15 at 17:36
  • What about using something like Import[SystemDialogInput["FileOpen"]] (or the version with Map[] if you expect to import multiple files)? – J. M.'s missing motivation Jan 08 '17 at 10:11
  • @J.M. I think it's a bit less flexible, because SystemDialogInput will always open Directory[]. – xzczd Jan 09 '17 at 02:12
3

I actually always use the FileNames approach as Yves suggested which gives you better control about which files to import. For completenes I just wanted to add that Import itself knows about directories. The following will show all files from a directory (warning: this traverses all subdirectories recursively!):

 Import[dirname]

and this will import the contents of all text files in a given directory (and all subdirectories):

 Import[dirname,"*.txt"]

see the documentation in "ref/format/Directory" for more details.

Albert Retey
  • 23,585
  • 60
  • 104
1

The way how I do it is to firstly generate the name of the input files into a table of strings, here's an example

nameInput = Table[StringTemplate["/Users/Projekt/output/input/b5_t`t`_a`a`.dat", InsertionFunction -> (ToString@NumberForm[#, {2, 2}] &)]@<|
 "t" -> 0.01 k, "a" -> 0.01 l|>, {k, 1, 3}, {l, 0, 4}];

which produces the following

  {{"/Users/Projekt/output/input/b5_t0.01_a0.00.dat", 
  "/Users/Projekt/output/input/b5_t0.01_a0.01.dat", 
  "/Users/Projekt/output/input/b5_t0.01_a0.02.dat", 
  "/Users/Projekt/output/input/b5_t0.01_a0.03.dat", 
  "/Users/Projekt/output/input/b5_t0.01_a0.04.dat"}, 
  {"/Users/Projekt/output/input/b5_t0.02_a0.00.dat", 
  "/Users/Projekt/output/input/b5_t0.02_a0.01.dat", 
  "/Users/Projekt/output/input/b5_t0.02_a0.02.dat", 
  "/Users/Projekt/output/input/b5_t0.02_a0.03.dat", 
  "/Users/Projekt/output/input/b5_t0.02_a0.04.dat"}, 
  {"/Users/Projekt/output/input/b5_t0.03_a0.00.dat", 
  "/Users/Projekt/output/input/b5_t0.03_a0.01.dat", 
  "/Users/Projekt/output/input/b5_t0.03_a0.02.dat", 
  "/Users/Projekt/output/input/b5_t0.03_a0.03.dat", 
  "/Users/Projekt/output/input/b5_t0.03_a0.04.dat"}}

This works well especially when your input files follows a pattern. Then you can simply import your data by importing the table

data = Table[Import[nameInput[[i, j]]], {i, 1, 3}, {j, 1, 4}];

hope this helps!

Gvxfjørt
  • 183
  • 7
1

Make a super simple program:

importdat := (# // Import[#, "Table"] &) &

and then use:

FileNames["*"] // importdat /@ # & // (nameofdataset = #) &;

you can narrow down the dataset to one with specific string by playing with *

ratt4
  • 31
  • 5
  • Might be better to use FileNames["*.txt"] or whatever other file type you are trying to import. – bbgodfrey Jan 08 '17 at 16:55
  • 1
    This is not super simple at all. Your importdat definition in unnecessarily complicated. You can replace it with importdat := Import[#, "Table"] & and it will do the same thing. – Gustavo Delfino Feb 09 '17 at 13:38
0

For some basic data files, imported in such a way that each can be plotted, consider this sort of delimited file:

{0, 0}
{1/4, 0.05262298820443631}
{1/2, 0.0708342218540585}
{3/4, 0.0828484992154531}

Then, use, with the file stored in fold1 (in the right directory on your computer, here this is home/Downloads/data/),

files = FileNames["*.txt", "/home/Downloads/data/fold1"]
datlist = ToExpression[Import[files[[#]], "List"]] & /@ Range[Length@files]

to get at all the files, then e.g.

ListPlot[datlist[[1]], Joined -> True]

enter image description here

apg
  • 2,075
  • 10
  • 13