0
$\begingroup$

My question is related to the following one: Delete all output in notebooks

I want to delete in all notebooks in a certain root directory all output cells as well as all other output generated during evaluation (e.g. from print and other) and in addition also all empty input cells and afterwards save the cleaned notebooks using the same names.

This solution is from kglr to delete all output cells :

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

Do[file = notebookFiles[[i]];
  nb = NotebookOpen[file, Visible -> False];
  FrontEndExecute[FrontEndToken["DeleteGeneratedCells"]];
  NotebookSave[nb, file];
  NotebookClose[nb];, {i, 1, num}];

Kuba showed a solution how one can delete in a single notebook file all empty input cells:

Deleting empty cells and selecting cells matching a pattern

NotebookDelete /@ Select[Cells[GeneratedCell -> False], StringMatchQ[
    First@
     FrontEndExecute[
      FrontEnd`ExportPacket[NotebookRead@#, "InputText"]],
    (" " | "\t" | "\r" | "\n" | "\[IndentingNewLine]") ...
    ] &
  ];

How can I combine both solutions in the upper Do loop?

$\endgroup$
  • 2
    $\begingroup$ just add Kuba's code before NotebookSave[nb,file]; inside Do[...]? $\endgroup$ – kglr Jan 29 at 11:26

Your Answer

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

Browse other questions tagged or ask your own question.