Group Abstract Group Abstract

Message Boards Message Boards

How to Import Files That Stored in Different Folder

GROUPS:
I have the profiles for a single set of parameters stored in several different directories,
like the profiles 1-25 in directory A, 26-50 in directory B, and so on. Is it possible to read them all from these directories using Mathematica (without copying to the same directory by hand)

Thanks in advance..
POSTED BY: selahittin cinar
Answer
2 months ago
Certainly.  One simple way is to use the full file path to the needed files in Import.
POSTED BY: David Reiss
Answer
2 months ago
If by profiles you are talking about getting data from a file like, Import on a file then you can map import over a list of the files to get only the ones you want. Here are a couple of suggestions for now to collect the file names. In this example, the working NB is sitting on top of the target directories, for simplicity. FileNames looks for a pattern in the list of sub-folders.
In[ ]:= SetDirectory[NotebookDirectory[]];
dirs = Select[FileNames["*"], DirectoryQ]
Out[ ]= {"Dynamic", "Graphics", "Graph_Network", "helperfunctions", "Numeric", "Sound", "SystemModeler", "test", "WolframAlpha"}

In[ ]:= FileNames["*Data*.txt", dirs]
Out[ ]= {"test\\someDatajunk.txt"}

Same result using a "*" pattern to represent all sub-folders
In[ ]:= FileNames["*Data*.txt", {"*"}, 2]
Out[ ]= {"test\\someDatajunk.txt"}

Another example, pairing the expected file name patterns up with the known directory names
In[ ]:= FileNames @@@ {{"*Data*.txt", "test"}, {"*.wav", "Sound"}}
Out[ ]= {{"test\\someDatajunk.txt"}, {"Sound\\arnoud.wav", "Sound\\brettc.wav", "Sound\\cindie.wav", "Sound\\pratik.wav", "Sound\\rachelle.wav"}}
POSTED BY: Christopher French
Answer
2 months ago
Thanks for your answer.

I know how to import data if data is in a single folder. But my data are in different folder I want  to collect all data and graph them (simply they are x and y values in table form).. I don't want to change file path for each folder..

If you need I can upload two samples folder..

I hope I explained clearly.

Selahittin..
POSTED BY: selahittin cinar
Answer
2 months ago
Sorry Christoper I saw your reply after I sent my post, I will check your answer later, I gotta go now..

Thanks..

Selahittin..
POSTED BY: selahittin cinar
Answer
2 months ago
These are 3 sample folders of my data.. I could not figure it out..

Since I have a bounch of them, I do not want to copy and paste them in one folder by hand..
Attachment

Attachment

Attachment

Attachment

Attachments: 0.png, 1.png, 2.png, 3.png, dr.nb
POSTED BY: selahittin cinar
Answer
2 months ago
Let us say you have 3 directories: dirA, dirB, dirC.  Then the list of files in those directories are given by
FileNames[{"*"}, {dirA, dirB,dirC}]
POSTED BY: David Reiss
Answer
2 months ago
ListLinePlot[
Map[Drop[Import[#, "Table"], 2] &,
  Table["dirA\\profile_shapes." <>
    StringTake["000" <> ToString[i], -4], {i, 1, 4}]],
PlotRange -> All]
I have had some progress but stil I need to copy and paste  this part  dirA\\profile_shapes   by hand.

I want to plot    dirA\\profile_shapes,   dirB\\profile_shapes and    dirC\\profile_shapes on the same plot..

 I need  something like  this, but it is not working for now..
ListLinePlot[
Map[Drop[Import[#, "Table"], 2] &,
  Table["FileNames["*", dirs]" <>
    StringTake["000" <> ToString[i], -4], {i, 1, 4}]],
PlotRange -> All]

any suggestion, I have attached the code..

Thanks..
Attachments: dr.nb
POSTED BY: selahittin cinar
Answer
2 months ago
I am stil confused b about what you are actually wanting to do.   If you want particular files from each particular directory then you need to form the filepaths for each cirectory separately and selecte from them according to the sting patterns appropriate to each. 

So, for example if you want files from directory dirA fo the form ending with .0001 through .0025 you might create those files using something like this (I am sure there are simpler string patterns that achieve this but this will do):
FileNames[{
  ___ ~~ ".000" ~~ CharacterRange["1", "9"],
  ___ ~~ ".001" ~~ CharacterRange["0", "9"],
  ___ ~~ ".002" ~~ CharacterRange["0", "5"]}, {dirA}]

and you would do similar things for the other directories with the appropriate numerical ranges in those. 
POSTED BY: David Reiss
Answer
2 months ago
First suggestion is, break the problem up into small steps. Also, once you have located the files, then Import does not care that they are spread out over many sub-directores. This is how I would approch the example provided by dr.nb.

I am not a big fan of SetDirectory, but it does simplify the example:
SetDirectory[NotebookDirectory[]]
dirs = Select[FileNames["*"], DirectoryQ]
Restrict the pattern for the files you are looking for with "profile_shapes.*". Store the result that you get from FileNames as rawFileNames.
rawFileNames=FileNames["profile_shapes.*", dirs]
Next, use another even more restricted pattern to select the desired file names off the big list. For this example, I set up three ranges that gets the first four files off each sub directlry - knowing the numerical boundry for each. IntegerString will do the work of dealing with the leading zeros as you switch between one and two digit index numbers. Pasting the individual directory names is not necessary.
targetFileNames = Select[rawFileNames, ! StringFreeQ[#,
     Join[
      Table[IntegerString[i, 10, 4], {i, 1, 4}],
      Table[IntegerString[i, 10, 4], {i, 26, 29}],
      Table[IntegerString[i, 10, 4], {i, 51, 54}]
      ]] &]
In case you want to use the header rows later, don't throw it away, get the full table of data first and then separate the data rows of interest in a seperate step.
rawData=Import[#,"Table"]&/@targetFileNames;
headerData=Take[#,2]&/@rawData;
tableData=Drop[#,2]&/@rawData;
Now the selected table data is ready to be shown:
ListLinePlot[tableData, PlotRange->All]
POSTED BY: Christopher French
Answer
2 months ago
This is it..

Thanks a lot Christopher and thanks the other repliers , you saved my life..

Selahittin..
POSTED BY: selahittin cinar
Answer
2 months ago