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]];
  Pdata = Append[CellsKey, Lastcap]

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

  • 2
    $\begingroup$ 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 $\endgroup$ Commented Jan 6, 2014 at 21:37
  • $\begingroup$ @SjoerdC.deVries Similar question was closed: mathematica.stackexchange.com/q/31984/5478 $\endgroup$
    – Kuba
    Commented Jan 7, 2014 at 5:41

2 Answers 2


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.

  (* 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. *)
  • $\begingroup$ 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. $\endgroup$ Commented Jan 13, 2014 at 14:49
  • $\begingroup$ 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. $\endgroup$ Commented Jan 14, 2014 at 17:03
  • $\begingroup$ 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 *) $\endgroup$ Commented Jan 14, 2014 at 21:02
  • $\begingroup$ 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. $\endgroup$ Commented Jan 14, 2014 at 21:06

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

function[inputfile_String] :=
Module[{CellsKey, Datalist, Lastcap, Pdata, filename},
    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

  • $\begingroup$ 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. $\endgroup$ Commented Jan 13, 2014 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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