Function Repository Resource:

SalvageNotebook

Source Notebook

Salvage a possibly corrupt notebook file by extracting all valid cells from it

Contributed by: Lou D'Andria

ResourceFunction["SalvageNotebook"][file]

attempts to salvage valid cells from the given notebook file and writes them into a new untitled notebook window.

ResourceFunction["SalvageNotebook"][file,newfile]

writes the salvaged cells directly to a new notebook file.

ResourceFunction["SalvageNotebook"][file,List]

returns the salvaged cells as a list.

ResourceFunction["SalvageNotebook"][]

displays a dialog for choosing a notebook file to salvage.

Details and Options

ResourceFunction["SalvageNotebook"][file,] returns unevaluated if file does not exist.
ResourceFunction["SalvageNotebook"][file,newfile] returns unevaluated if newfile already exists.
Only cell expressions in the notebook file are considered for salvaging.
Notebook options are not salvaged and are treated as corrupt cell data.
The following options can be given:
"IgnoreBoxDataCells"Falsewhether to ignore cells containing BoxData
"IgnoreGraphicsDataCells"Truewhether to ignore cells containing GraphicsData
"SummarizeCorruptLines"Truewhether to summarize lines that are not part of a valid cell expression
When ResourceFunction["SalvageNotebook"] encounters data that does not correspond to a valid cell expression, it includes a summary of those lines in the salvaged data.
With the setting "SummarizeCorruptLines"False, any lines not corresponding to a valid cell expression are literally included in the salvaged data.
When either "IgnoreBoxDataCells" or "IgnoreGraphicsDataCells" is True, the salvaged data contains an indication for each ignored cell.

Examples

Basic Examples (2) 

Choose a notebook file to salvage:

In[1]:=
ResourceFunction["SalvageNotebook"][]
Out[1]=

Write an intentionally-corrupt notebook to a temporary file:

In[2]:=
WriteString[
 file = CreateFile[], "Notebook[{\nCell[\"This is a text cell\", \"Text\"],\nCell[\"This is an \nincomplete cell expression\nCell[BoxData[RowBox[{SuperscriptBox[\"x\",\"y\"],\"+\",\"z\"}]], \"Input\"]\n},\nStyleDefinitions ->\"Default.nb\"\n]"]; FilePrint[file]

Salvage valid cells from that file and place them a new untitled notebook:

In[3]:=
ResourceFunction["SalvageNotebook"][file]
Out[3]=

Scope (2) 

Return cells salvaged from a notebook as a List:

In[4]:=
WriteString[file = CreateFile[], "Notebook[{\nCell[\"This is a text cell\", \"Text\"],\nCell[\"This is an \nincomplete cell expression\nCell[BoxData[RowBox[{SuperscriptBox[\"x\",\"y\"],\"+\",\"z\"}]], \"Input\"]\n},\nStyleDefinitions ->\"Default.nb\"\n]"];
ResourceFunction["SalvageNotebook"][file, List] // Short
Out[5]=

Salvage a notebook file directly to a new notebook file:

In[6]:=
WriteString[file = CreateFile[], "Notebook[{\nCell[\"This is a text cell\", \"Text\"],\nCell[\"This is an \nincomplete cell expression\nCell[BoxData[RowBox[{SuperscriptBox[\"x\",\"y\"],\"+\",\"z\"}]], \"Input\"]\n},\nStyleDefinitions ->\"Default.nb\"\n]"];
ResourceFunction["SalvageNotebook"][file, "salvagedFile.nb"]
Out[7]=

Options (2) 

Write an intentionally-corrupt notebook to a temporary file:

In[8]:=
WriteString[
 file = CreateFile[], "Notebook[{\nCell[\"This is a text cell\", \"Text\"],\nCell[\"This is an \nincomplete cell expression\nCell[BoxData[RowBox[{SuperscriptBox[\"x\",\"y\"],\"+\",\"z\"}]], \"Input\"]\n},\nStyleDefinitions ->\"Default.nb\"\n]"]; FilePrint[file]

Turn off "SummarizeCorruptLines" to cause the data that is not recognized as valid cells to be inserted literally in the resulting salvaged data:

In[9]:=
ResourceFunction["SalvageNotebook"][file, "SummarizeCorruptLines" -> False]
Out[9]=

Write an intentionally-corrupt notebook to a temporary file:

In[10]:=
WriteString[
 file = CreateFile[], "Notebook[{\nCell[\"This is a text cell\", \"Text\"],\nCell[\"This is an \nincomplete cell expression\nCell[BoxData[RowBox[{SuperscriptBox[\"x\",\"y\"],\"+\",\"z\"}]], \"Input\"]\n},\nStyleDefinitions ->\"Default.nb\"\n]"]; FilePrint[file]

Turn on "IgnoreBoxDataCells" to automatically skip any BoxData cell:

In[11]:=
ResourceFunction["SalvageNotebook"][file, "IgnoreBoxDataCells" -> True]
Out[11]=

Publisher

Lou D'Andria

Version History

  • 1.0.0 – 16 November 2020

Related Resources

License Information