# operating on text files in different folders

I have a large number of folders (say, m) in a directory and each one of them has n1, n2, ..., nm numbers of text files. I have to perform a mathematical operation on these text files which will result in a number. The names of the folders/text files are arbitrarily chosen.

How to tell Mathematica to do this mathematical operation on all the text files in a single step, such that the final o/p is a table having first column same as the name of the folder, second column name of the text file and third column the o/p of the mathematical operation on the corresponding file.

I tried the functions like FileNames, which gives the list of text files in folder but no success in figuring out the way to use these folder names as variable to run all the files in one go. Will appreciate any suggestion. Thanks

Let's say your textfiles are in fact images, and your mathematical operation is to make them lighter. Then you could do it like this:

(* Retrieve all filenames *)
files = FileNames["*.png", "~/parent_folder/", 2];

(* Import all files *)
images = Import /@ files;

(* Process *)
result = Lighter /@ images;

(* Combine folder name, file name and processed image *)

• If I understand you correctly, then define your import&process function importAndProcess[file_] := Module[{}, input = Import[file]; Lighter[input]] and map this as result = importAndProcess /@ files;. You mentioned some parallelization: use ParallelMap instead of simple Map: result = ParallelMap[importAndProcess, files];. Jul 29 at 15:54