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


1 Answer 1


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 *)
    FileBaseName[#1], #2} &, {files, result}] // TableForm
  • $\begingroup$ Thanks. The mathematical operation I have to perform on each text file is a complex multi-step process and I would prefer to use a parallel operation to evaluate them. Is it possible to import the files one by one and perform the task instead of importing all of them in one go. $\endgroup$
    – user49535
    Jul 29, 2021 at 8:05
  • $\begingroup$ 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];. $\endgroup$
    – Domen
    Jul 29, 2021 at 15:54

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.