Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 24 down vote accepted

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

share|improve this answer
    
Ya, I figured the rest before the edit myself, thanks. I now ended up with: FileNames["*.tex", "MY PATH"]; Part[%, 1 ;; 3]; Import[#, "Text"] & /@ % –  NiftyKitty95 May 6 '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 6 '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 6 '12 at 15:44
    
@YvesKlett: is there a certain reason to use Import[#]&/@files instead of the simpler Import/@files? –  Albert Retey May 7 '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 7 '12 at 20:03
show 1 more comment

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.

Just a snapshot:

Mathematica graphics

This solution is only tested in Windows Vista 32bit and Mathematica 8.0.4, I'm not sure if it will work with other versions and operation systems.

share|improve this answer
    
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
    
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 9:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.