# CMD method to convert .nb file into .m file

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

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}
];

• Problem is I think yode is explicitly trying to avoid using the FE. Can NotebookImport work without it? – b3m2a1 Jan 13 '18 at 0:42
• @ 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. – mrz Jan 13 '18 at 7:52
• Ah I see. I thought he wanted a CLI solution. – b3m2a1 Jan 13 '18 at 7:53
• Yes, I just don't open Mathematica to do it. I hope to use command method. – yode Jan 13 '18 at 8:16