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?
Answer
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).
Comments
Post a Comment