Skip to main content
Modified as suggested by @Andy Ross
Source Link
Leonid Shifrin
  • 114.9k
  • 16
  • 333
  • 424

Here is my version:

ClearAll[nbLines];
 nbLines[nb_NotebookObject] :=
 Count[
   Characters@StringJoin@Cases[
      Cases[NotebookGet[nb],With[{inputs =
          Cases[NotebookGet[nb], Cell[BoxData[data_], "Input", ___] :> data, Infinity]
       },
      Length[inputs] + 
      Count[Characters@StringJoin@Cases[inputs, _String, Infinity], 
        "\n" | "\[IndentingNewLine]"];"\[IndentingNewLine]"]
     ]

This may not be precise (because the method of input extraction is not totally robust), but should be good enough for qualitative estimate.

Here is my version:

ClearAll[nbLines];
nbLines[nb_NotebookObject] :=
 Count[
   Characters@StringJoin@Cases[
      Cases[NotebookGet[nb], 
           Cell[BoxData[data_], "Input", ___] :> data, Infinity],
      _String, Infinity],
       "\n" | "\[IndentingNewLine]"];

This may not be precise, but should be good enough for qualitative estimate.

Here is my version:

ClearAll[nbLines];
 nbLines[nb_NotebookObject] :=
     With[{inputs =
         Cases[NotebookGet[nb], Cell[BoxData[data_], "Input", ___] :> data, Infinity]
       },
      Length[inputs] + 
      Count[Characters@StringJoin@Cases[inputs, _String, Infinity], 
        "\n" | "\[IndentingNewLine]"]
     ]

This may not be precise (because the method of input extraction is not totally robust), but should be good enough for qualitative estimate.

Source Link
Leonid Shifrin
  • 114.9k
  • 16
  • 333
  • 424

Here is my version:

ClearAll[nbLines];
nbLines[nb_NotebookObject] :=
 Count[
   Characters@StringJoin@Cases[
      Cases[NotebookGet[nb], 
           Cell[BoxData[data_], "Input", ___] :> data, Infinity],
      _String, Infinity],
       "\n" | "\[IndentingNewLine]"];

This may not be precise, but should be good enough for qualitative estimate.