2
$\begingroup$

I am trying to automate the process of running a Mathematica notebook that takes as an input a .txt file and exports another .txt file. This notebook was not written by me, and I don't fully understand its inner workings, but I think all I need to know is the following parts of the code:

The input statement is:

my_input_file = Import["my_file_name.txt", "Table"];

The export statement is:

Export["exported_file_name.txt", my_input_file_transformed, "Table"]

I would like to run notebook for many (100) different my_file_name.txt files which I could name however I want to facilitate the import process, e.g. my_file_name_1.txt, my_file_name_2.txt, etc., and put them in the same folder as the my_mathematica_notebook.nb.

Similarly, I would like to save the 100 different exported .txt files, in the same form I input them in order to be able to reconcile each imported-exported pair of files, e.g. something like exported_file_name_1.txt, exported_file_name_2.txt

I guess my question is how to run the notebook many times for similarly named files and how to export the files as described above. I have read answers on related questions that call a notebook from another notebook, but none that automates the import of .txt files.

My knowledge of Mathematica is very basic, especially that related to file management, so I am unable to translate the answers I found into a solution for my problem. If it helps, I am working on a Linux machine and could use the command line to automate the process if needed.

Any help with this would be very appreciated. Many thanks.

$\endgroup$
  • $\begingroup$ my_input_file is not a valid Wolfram Language identifier. $\endgroup$ – m_goldberg Dec 18 '19 at 5:04
  • $\begingroup$ Could you give an example of a valid one? Thanks for the edits by the way. $\endgroup$ – Ouroboroski Dec 18 '19 at 9:40
  • $\begingroup$ myInputFile would be valid. Wolfram Language supports camel-case, but not snake-case. myInputFile is parsed as Times[Blank[file], Pattern[my, Blank[input]]] $\endgroup$ – m_goldberg Dec 18 '19 at 15:57
1
$\begingroup$

Not knowing all of the details, I suggest using an additional Notebook to automate the operation of "my_mathematica_notebook.nb"

In the new Notebook

    names = FileNames["*.txt", {".", "*"}, 1]
    num = Dimensions[names][[1]]
    (* finds all txt files *)

    process[index_]:=Module[{}
    CopyFile[names[[index]],"my_file_name.txt"];
    (* copy the file to the placeholder filename *)
    NotebookEvaluate["my_mathematica_notebook.nb", InsertResults -> False];
    (* Run the Notebook *)
    CopyFile["exported_file_name.txt",StringJoin["exported",names[[index]]]];
    (* Copy the default export to the desired filename *)
    ]

    (* "loop over" all txt files located *)
    Table[process[indx],{indx,1,num,1}]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks for your answer, exactly what I was looking for. I am getting an error Module::argmu: Module called with 1 argument; 2 or more arguments are expected. It points to just inside the square bracket after (* Copy the default export to the desired filename *) $\endgroup$ – Ouroboroski Dec 17 '19 at 22:23
  • $\begingroup$ Add comma after {} in Module: Module[{}, CopyFile .... $\endgroup$ – Alx Dec 18 '19 at 3:07
0
$\begingroup$

Pure functions will be helpful here. Start by using Table to automatically create the 100 files names you plan on entering.

importnames=Table[StringJoin["my_file_name_", ToString[i], ".txt"], {i, 1, 100}]

Since this will all be text, you can Import them all at once and keep them in a single variable to alter later.

imported=Import[#]&/@importnames

Now you can manipulate your text. Let's pretend you want it all upper case.

manipulated=ToUpperCase/@imported

Next create all your export names in the same manner as above.

exportnames=Table[StringJoin["exported_file_name_", ToString[i], ".txt"], {i, 1, 100}]

An easy way to automate the export process is create a list of lists where each entry contains the export name and the file contents. You can do that with Transpose.

exportdata=Transpose[{exportnames,manipulated}]

Then run all the exports at the same time.

Export[#[[1]],#[[2]]]&/@exportdata

If you aren't familiar with slots and mapping, you can read up on Pure Functions.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you for your answer, very instructive. However, it does not include how to run my_mathematica_notebook.nb right? $\endgroup$ – Ouroboroski Dec 17 '19 at 22:18
  • $\begingroup$ @Ouroboroski Simply evaluate the cells. $\endgroup$ – kickert Dec 18 '19 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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