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 have a lot of files in a folder that needs to be process through a function that I have written in Mathematica. I need help writing a code that would process all the files automatically.

TestData = Import["file.xls"];

function[input_] := 
 Module[{CellsKey, Datalist, Lastcap, Pdata, filename},
  Datalist = {};
  CellsKey = TestData[[1]][[1]][[1]];
  a = Last[TestData[[1]]];
  Lastcap = a[[4]];
  (*Print[Lastcap]*)
  Pdata = Append[CellsKey, Lastcap]
    (*Print[CellsKey]*)
    Print[Pdata]
]

This is what I have so far. With this code, I could only import one file at a time.

share|improve this question
2  
Make TestData or the file name part of the function parameters. Then Map this function of a list of file names, which you can obtain using FileNames –  Sjoerd C. de Vries Jan 6 at 21:37
    
@SjoerdC.deVries Similar question was closed: mathematica.stackexchange.com/q/31984/5478 –  Kuba Jan 7 at 5:41

2 Answers 2

up vote 3 down vote accepted

Set the notebook path to the folder in which the files you wish to process are located. For example, in the same folder as the current notebook.

myNBpath = SetDirectory[NotebookDirectory[]]

Get a list of all the filenames in your designated folder matching some filename string.

nblist = FileNames["filesOfInterest*.nb"];

Now start a Do loop to process all the files whose filenames have been stored in the nblist variable. There is almost certainly a more elegant and efficient Mathematica way to do this without a Do list.

Do[
  (* Place code here to process those files as you wish. *)

  Pause[1]; (* Sometimes a pause of a second or so helps. *)
  ,
  {ichg, 1, Length[nblist]} (* Keep Do-ing this processing until all the designated files have been processed *)
];

Beep[]; (* Optional: make a warning sound when all the files have been processed. *)

Print["- Finished making clean files -"] (* Optional: print a message in the Mathematica notebook when all the files have been processed. *)
share|improve this answer
    
It doesn't work.. I imported my all my files with your code. When I run my function, the function only output data from of the first file only. –  user2895279 Jan 13 at 14:49
    
I wrote this according to what you suggested. Input: myNBpath = SetDirectory[NotebookDirectory[]]; TestData = FileNames["Cell_*"]; Print[TestData] Output: {Cell_0079_103L.xls, Cell_0079_103M.xls} FileNames only sees those two files as a list of two items, instead of two giant lists of data. –  user2895279 Jan 14 at 17:03
    
You have placed the FileNames in your TestData variable. If you want the data stored in those files placed in your TestData variable, you have to Import them, for example, as follows: TestData[[ichg]] = Import[nblist[[ichg]]]; – Place this code in the Do loop where it says (* Place code here *) –  zentient Jan 14 at 21:02
    
Note that this code, using a Do loop, is a procedural programming approach, which is quite clunky compared to the elegant and efficient functional programming approach as offered by Sjoerd de Vries. –  zentient Jan 14 at 21:06

As Sjoerd de Vrjes said, the way to go may be:

function[inputfile_String] :=
Module[{CellsKey, Datalist, Lastcap, Pdata, filename},
  If[FileExistsQ[inputfile],
    TestData = Import[inputfile];
    (* your code *)
  ,
    (* if the file does not exist *)
    Print["The file ", inputfile, "does not exist"];
  ]
];

function /@ FileNames["*.xls"]

The symbol /@ indicates that the function is applied to all the elements in the list of the file names

share|improve this answer
    
Don't I have to set "inputfile" to equal something? I have thousands of files to process.... I don't think I can individually list them out. –  user2895279 Jan 13 at 14:29

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.