2
$\begingroup$

.nb file consists of the boxes, and the .m file is a pure text file, what can help to share on GitHub. Of course, we can open a .nb file with Mathematica, then Save As... it as a .m file. But I don't want to cost much time to open the FrontEnd to do such thing. If the CMD command can do this, it will help a lot. Is it possible?

I'm using Windows 10, of course, if someone provide a method based on other operation system, it is welcome here.

I think use FilePrint in wolframscript can do this, but actually it is don't work for me.

$\endgroup$
1
$\begingroup$

You can use the following code to convert all .nb files in all sub-directories in a root directory dir to .m files (see also this ):

ChoiceDialog[{FileNameSetter[Dynamic[dir], "Directory"], Dynamic[dir]}];
notebookFiles = FileNames["*.nb", dir, Infinity]
num = Length[notebookFiles];

Do[
    file = notebookFiles[[i]];
    notebookText = Import[file, "Text"];

    Export[
      StringDrop[file, -2] <> "m", 
      StringRiffle[NotebookImport[file, "Input" -> "InputText"], "\n"], 
      "Text"
    ];
    , {i, 1, num}
];
$\endgroup$
  • $\begingroup$ Problem is I think yode is explicitly trying to avoid using the FE. Can NotebookImport work without it? $\endgroup$ – b3m2a1 Jan 13 '18 at 0:42
  • $\begingroup$ @ b3m2a1: As I understand Yode does not not want to do the conversion by opening each notebook and then saving over the menu, because it is too much time consuming if one has a lot of notebooks. The code I show above can be executed in an external notebook and converts all your notebook files to .m files at once. $\endgroup$ – mrz Jan 13 '18 at 7:52
  • $\begingroup$ Ah I see. I thought he wanted a CLI solution. $\endgroup$ – b3m2a1 Jan 13 '18 at 7:53
  • $\begingroup$ Yes, I just don't open Mathematica to do it. I hope to use command method. $\endgroup$ – yode Jan 13 '18 at 8:16

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.