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.

Let me just start by pointing out I'm a new StackExchange user as well as a new Mathematica user (started today!) I mention this as a context to my ineptitude :)

I am attempting to read through a list with the names of files I want to open, import data from them and plot that data. Here is the code I'm trying to use:

testfiles = ({"aspirina.txt", "silicio.txt", "silicio20.txt", 
   "silicio20luz.txt", "tio2.txt", "vidro.txt", "vidro2.txt"})

Do[{data = Import[path <> ToString[i, StandardForm], {"Table"}], 
  Print[path <> ToString[i, StandardForm]], 
  Print[ListLinePlot[data]]}, {i, testfiles}]

(*
Out[500]= {aspirina.txt, silicio.txt, silicio20.txt, silicio20luz.txt,
  tio2.txt, vidro.txt, vidro2.txt}
*)
Import::nffil: File not found during Import. >> 
~/Desktop/TCOEM/Espetros Raman/aspirina.txt

etc.

The directory and file names are correct, so much so that when I try this directly by just typing the exact same directory I get the desired result.

Firstly, I changed the FormatType setting of ToString[] to StandardForm because the default (OutputForm) would give me an extra whitespace on either side of the full stop in the file names. Why does it do this?

Secondly, why is it that even when Print[] seems to indicate the directory is correct I get the exception saying the file cannot be found during Import?

I specifically did not format the names of the files in the testfiles list individually as strings (using "" around each one) to allow for the case where I have hundreds of such files and just want to copy and paste their names via whatever file manager I happen to be using.

Any help will be greatly appreciated!

share|improve this question
    
It's usually easier to let Mathematica find the files, as in VLC's answer, but if you have to paste filenames from the clipboard you can paste between two quote marks to get a single long string, and use StringSplit to break it into individual filenames. –  Simon Woods Oct 26 '12 at 14:09
    
Good to know thanks :) –  wrighteap Oct 26 '12 at 19:30

2 Answers 2

up vote 4 down vote accepted

Filenames are always strings. What you do when you use something like aspirina.txt without quotes is, you use the Dot operator (which is . in infix) to and give the two variables aspirina and txt as input.

Please try

testfiles = {"aspirina.txt", "silicio.txt", "silicio20.txt", "silicio20luz.txt", 
"tio2.txt", "vidro.txt", "vidro2.txt"};

Do[{data = Import[path <> i, {"Table"}], 
  Print[path <> i], 
  Print[ListLinePlot[data]]}, {i, testfiles}]
share|improve this answer
    
I had not realised Dot existed and that it is equivalent to . Thank you. –  wrighteap Oct 26 '12 at 19:24

You don't need to copy and paste the names of your files. If all the files you want to import are in a specific directory you can just import them directly from that directory.

You first need to define where your directory is by using SetDirectory. Then you can get the list of your files:

filesToImport = ToFileName[Directory[], #] & /@ FileNames["*.txt"]

This line will give you a list of all the paths to the files within the directory.

share|improve this answer
    
This appears to be an altogether more sensible approach. Thanks! –  wrighteap Oct 26 '12 at 19:27

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.