# Processing all files in a folder automatically

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.

-
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 '14 at 21:37
@SjoerdC.deVries Similar question was closed: mathematica.stackexchange.com/q/31984/5478 –  Kuba Jan 7 '14 at 5:41

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. *)

-
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 '14 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 '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 '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 '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];