Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Salvage a possibly corrupt notebook file by extracting all valid cells from it
| 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. | 
| "IgnoreBoxDataCells" | False | whether to ignore cells containing BoxData | 
| "IgnoreGraphicsDataCells" | True | whether to ignore cells containing GraphicsData | 
| "SummarizeCorruptLines" | True | whether to summarize lines that are not part of a valid cell expression | 
Choose a notebook file to salvage:
| In[1]:= | ![ResourceFunction["SalvageNotebook"][]](https://www.wolframcloud.com/obj/resourcesystem/images/912/912a5e9e-8a2d-4425-9422-b73ea8f1c934/211819c8d779fade.png) | 
| 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]](https://www.wolframcloud.com/obj/resourcesystem/images/912/912a5e9e-8a2d-4425-9422-b73ea8f1c934/1d00e507d8634610.png) | 

Salvage valid cells from that file and place them a new untitled notebook:
| In[3]:= | ![ResourceFunction["SalvageNotebook"][file]](https://www.wolframcloud.com/obj/resourcesystem/images/912/912a5e9e-8a2d-4425-9422-b73ea8f1c934/7f77545427bfdcef.png) | 
| Out[3]= |  | 

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](https://www.wolframcloud.com/obj/resourcesystem/images/912/912a5e9e-8a2d-4425-9422-b73ea8f1c934/66e14a6d3939e9c3.png) | 
| 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"]](https://www.wolframcloud.com/obj/resourcesystem/images/912/912a5e9e-8a2d-4425-9422-b73ea8f1c934/495a5bf00a2d9ebf.png) | 
| Out[7]= |  | 
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]](https://www.wolframcloud.com/obj/resourcesystem/images/912/912a5e9e-8a2d-4425-9422-b73ea8f1c934/4cb8c735007186b4.png) | 

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]](https://www.wolframcloud.com/obj/resourcesystem/images/912/912a5e9e-8a2d-4425-9422-b73ea8f1c934/0245832e8c5d34a1.png) | 
| 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]](https://www.wolframcloud.com/obj/resourcesystem/images/912/912a5e9e-8a2d-4425-9422-b73ea8f1c934/65e44eee6015a935.png) | 

Turn on "IgnoreBoxDataCells" to automatically skip any BoxData cell:
| In[11]:= | ![ResourceFunction["SalvageNotebook"][file, "IgnoreBoxDataCells" -> True]](https://www.wolframcloud.com/obj/resourcesystem/images/912/912a5e9e-8a2d-4425-9422-b73ea8f1c934/27ce59cdf1b7c51c.png) | 
| Out[11]= |  | 

This work is licensed under a Creative Commons Attribution 4.0 International License