25

I had my Notepad++ with at least 50 opened files on it, organizing all my work.

I left the computer as always hibernating overnight and surprise, this morning all was gone.

An error message popped up when maximizing Notepad++, something along a file not being able to be opened.

I accepted the error and Notepad++ had no files on it.

After rebooting nothing changed, and in the backups folder I only found standalone old dirty files that I worked on a long time ago...

Any thoughts on how to recover my precious working session?

Thanks,

Peanuts
  • 390

6 Answers6

32

Unless you have saved your session in the cloud, like the previous answer, you can't restore your whole session. You can, however, restore your unsaved (new) notes. They are stored at:

%APPDATA%\Notepad++\backup

You may either open all those files directly in Notepad++, or you can remake the session file in:

%APPDATA%\Notepad++\session.xml

so that the <mainView> section will include all of them.

Here's a Python script to make it easier to redo the session file. Just paste the output between the <mainView> and the </mainView> tags:

import os
import os.path as osp

npp_path = osp.join(osp.expandvars('%APPDATA%'), 'Notepad++', 'backup')
for fn in sorted(os.listdir(npp_path), key=lambda fn: fn.split('@')[1]):
    name = fn.split('@')[0]
    print('<File firstVisibleLine="0" xOffset="0" scrollWidth="64" '
          'startPos="8" endPos="8" selMode="0" lang="Normal Text" '
          'encoding="-1" userReadOnly="no" filename="{name}" '
          'backupFilePath="{npp_path}\{fn}" originalFileLastModifTimestamp="0"'
          'originalFileLastModifTimestampHigh="0" '
          'mapFirstVisibleDisplayLine="-1" mapFirstVisibleDocLine="-1" '
          'mapLastVisibleDocLine="-1" mapNbLine="-1" mapHigherPos="-1" '
          'mapWidth="-1" mapHeight="-1" mapKByteInDoc="0" '
          'mapWrapIndentMode="-1" mapIsWrap="no" />'.format(
                  name=name, npp_path=npp_path, fn=fn))
11

You cannot, but you can secure against happening this again.

This is possible if you have your cloud path set in Preferences:

enter image description here

After the breakdown, immediately turn off syncing with the cloud and restore the original file from there. If your cloud has file versioning, then it is simpler: just retrieve the older version of the session.xml.

This also works for all other setting files, see the link above for details.


Also be sure that you updated to at least Notepad++ 7.5.9.

In its list of fixed bugs, there is

  1. Fix possible file corruption during backup or power loss or other abnormal N++ termination.

So yes, this has been addressed in October 2018.

Rudolph
  • 347
miroxlav
  • 14,845
3

Thanks to Joe Pineda for his comment on the backup folder. After losing my crashed session, I saw that in this folder there were many files - both unsaved notes and opened existing files. They were of non-zero size in bytes but neither the ordinary Notepad, nor Notepad++ itself displayed anything. However, you can read them in Linux.

Remember to make a copy of your backup directory in case something unexpectedly goes wrong.

I recovered my unsaved notes by making a list of the file names and removing the "new" from the filename (Windows just loves spaces in file names).

for note in $(ls new*); do echo $note | grep -v new; done

This list of names can be saved in a variable and then the files can be copied to e.g .txt:

filenames=$(for note in $(ls new*); do echo $note | grep -v new; done) 
for f in $filenames ; do cp 'new '$f 'new '$f.txt; done
Suzanka
  • 31
3

Hi all,

as the session.xml in path "%APPDATA%\Notepad++" got lost for me as well and the python script from Ronan didn't work for me, here an alternative PowerShell script that recreates a session file for all open documents in "%APPDATA%\Notepad++\session_restored.xml" that you just need to rename to session.xml and all backup files will show up in NP++. All backup files are located in folder "%APPDATA%\Notepad++\backup" and will be scanned and ordered by modification date from old to new (duplicated file names will get a #number appended). To prevent future issues and have a backup, I've moved the entire "%APPDATA\Notepad++" folder to my local drive's cloud folder, that gets synced to OneDrive (or GoogleDrive or Amazon Photos/Drive) and left a symlink like mklink /j "%USERPROFILE%\OneDrive\Notepad++" "%APPDATA%\Notepad++".

Hope this helps others.

Cheers - Jens.

$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False

$session_file = "$($env:APPDATA)\Notepad++\session_restored.xml"

@' <?xml version="1.0" encoding="UTF-8" ?> <NotepadPlus> <Session activeView="0"> <mainView activeIndex="0"> '@, $Utf8NoBomEncoding)

$newFiles = [System.Collections.Generic.HashSet[String]]::new() Get-ChildItem "$($env:APPDATA)\Notepad++\backup" | Where-Object { $.Length -gt 0 } | Sort-Object LastWriteTime |% { if ($.Name.IndexOf('@') -gt 0) { $newFile = $.Name.Substring(0, $.Name.IndexOf('@')) } else { $newFile = $.Name } $newFileNo = $newFile for ($no = 2; $newFiles.Contains($newFileNo); $no += 1) { $newFileNo = "$newFile#$no" } $newFile = $newFileNo [void]$newFiles.Add($newFile) @" <File filename="$newFile" backupFilePath="$($.FullName)" /> "@ | Out-File -encoding UTF8 -append $session_file }

@' </mainView> <subView activeIndex="0" /> </Session> </NotepadPlus> '@ | Out-File -encoding UTF8 -append $session_file

0

In case of no backup, rebuild your session from all files in backup folder

  1. Determine the path to your backup folder. Usually it is in menu Settings > Preferences > Backup > Backup Path. Let's say it is:

    C:\Tools\Notepad++\backup\

  2. Get the file list from that folder, let's say it is:

     new 164@2021-10-11_114009
     new 169@2021-10-11_185349
     new 174@2021-10-12_184441
  3. In Notepad++ directory, edit file session.xml
    • do edits in different editor, have Notepad++ not running

  4. Turn you file list into XML <File /> tags and insert them into already present <mainView /> tag like this:

<mainView activeIndex="1">

<file filename="R-new 164" backupFilePath="C:\Tools\Notepad++\backup\new 164@2021-10-11_114009"/>
<file filename="R-new 169" backupFilePath="C:\Tools\Notepad++\backup\new 169@2021-10-11_185349"/>
<file filename="R-new 174" backupFilePath="C:\Tools\Notepad++\backup\new 174@2021-10-12_184441"/>

</mainView>

• you can notice I chose tab name R-new 164 for restored file new 164 and did similar for other files. This is just a naming convention, you can use your own. It will help you recognize that the file was restored. It can also prevent naming conflicts.

  1. Save this into session.xml and run Notepad++. Files present in backup folder will appear in your session.

  2. Your files opened in previously lost session from within other folders (e.g. D:\Project\file.txt) will be still missing in the session, but you can locate and add (open) them normally. If they have an unsaved draft from time when your N++ crashed, you can find it among your already restored files, it will have name R-file.txt (according to naming convention you chose in step 4).

miroxlav
  • 14,845
0

You can recover you session.xml easy by using Disk Investigator, and searching for tags within the session.xml file, or filenames you know were open and listed in session.xml. It can even find it if it was partially overwritten (but can still match your search), unlike other file recovery software, because it's a raw data recovery software. You can also find much older session.xml files. I've recovered a lot of lost session.xml files with it