0
$\begingroup$

I am looking to import multiple .txt files. So far I have been importing them individually like this:

file1 = Import["C:\\file1.txt", "Table"];

I'd like to be able to do files[1], files[2], etc., instead of assigning individual file names to each import.

I tried doing the method described in the top answer of this post: Importing multiple files using a for-loop

But typing the following, I get "file not found during input"

testlist = 
  Table[
    Import[
      "C:\\Users\\joep\\Documents\\Scenario 2\\Climb\\Climb 90000 0.78.txt" <> 
        ToString[i] <> ".txt,", 
      "Data"], 
    {i, 4}]

 FileNames["*.txt", "C:\\Users\\joep\\Documents\\Scenario 2\\Climb"]
{"C:\\Users\\joep\\Documents\\Scenario 2\\Climb\\Climb 100000 0.78.txt", 
 "C:\\Users\\joep\\Documents\\Scenario 2\\Climb\\Climb 100000 240.txt", 
 "C:\\Users\\joep\\Documents\\Scenario 2\\Climb\\Climb 100000 300.txt", 
 "C:\\Users\\joep\\Documents\\Scenario 2\\Climb\\Climb 90000 0.78.txt", 
 "C:\\Users\\joep\\Documents\\Scenario 2\\Climb\\Climb 90000 240.txt", 
 "C:\\Users\\joep\\Documents\\Scenario 2\\Climb\\Climb 90000 300.txt"}
$\endgroup$

closed as off-topic by Niki Estner, m_goldberg, LCarvalho, rcollyer, bbgodfrey Nov 18 '17 at 2:51

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Niki Estner, m_goldberg, LCarvalho, rcollyer, bbgodfrey
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Can you talk about what your scheme for the filenames is? $\endgroup$ – J. M. is away Nov 14 '17 at 11:47
  • $\begingroup$ Are your files really named 0.78.txt.123.txt for i=123? $\endgroup$ – Niki Estner Nov 14 '17 at 11:47
  • $\begingroup$ the files are named like Climb 90000 0.78.txt It works for the first method I am using. Should I change the file names? $\endgroup$ – JoepM Nov 14 '17 at 11:52
  • $\begingroup$ Change the Table[ so it returns the list of filenames instead of calling Import. Then change the string expression until it returns the right file names. $\endgroup$ – Niki Estner Nov 14 '17 at 12:13
  • $\begingroup$ That is still a bit vague. Please evaluate FileNames["*.txt", "C:\\Users\\joep\\Documents\\Scenario 2\\Climb"] and paste in the results of that in your question. $\endgroup$ – J. M. is away Nov 14 '17 at 12:33
1
$\begingroup$

Get all the file names and then import them.

allFiles = FileNames["*.txt","C:\\Users\\joep\\Documents\\Scenario 2\\Climb"];
allData = Import[#, "Table"] & /@ allFiles

Now you can access the individual data pieces by indexing into allData, for instance:

allData[[n]]

gets the nth set.

$\endgroup$
  • $\begingroup$ This worked for me thank you very much $\endgroup$ – JoepM Nov 15 '17 at 12:32

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