23

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,

2
  • I searched for the session.xml file on C:\Users\user.user-PC\AppData\Roaming\Notepad++ but it got "reset" and isn't showing all the old opened tabs. I went to [right click] > Preferences > Old Versions but there's no old version available. On Windows Temp folder there're no "session" files, on "n++" or "npp" or "notepad" files :(
    – Peanuts
    Commented Jul 27, 2018 at 16:41
  • Possible duplicate of Is there any way to recover unsaved notepad++ docs? Commented Aug 21, 2018 at 13:02

6 Answers 6

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.

4
  • 8
    I have Notepad++ 7.5.9 and this just happened to me: my computer lost power (UPS failed!) and when I opened Notepad++ again I see a clean session. In the "backup" folder I see copies of the unnamed files, but I see no way to restore all the named files I was working on
    – Joe Pineda
    Commented Nov 22, 2018 at 16:18
  • @JoePineda – hm, thanks for sharing. So maybe the cloud backup is really the next option to try? Even if the backup may be delayed, it can be still better to recover somewhat older tab set then start with blank one.
    – miroxlav
    Commented Nov 23, 2018 at 9:00
  • 2
    @miroxlav I ended up installing "recuva" as suggested in another answer to the same data loss by Notepad++ - it worked like a charm, recovering all the files I was working at the moment, and even the session file (which I manually substituted). I only needed to point recuva to a shared folder on another network machine to leave the files at. I was dissappointed by Notepad++, though, this destroying of user info is totally unacceptable - they should be using SQLite or something like that to have ACID changes in the files they're touching, database-style
    – Joe Pineda
    Commented Nov 26, 2018 at 23:46
  • 1
    (Here is one example of an answer describing what Joe mentions regarding using Recuva... the first time I tried it, I didn't think it had worked, but lo and behold I tried again and made sure to recover the file the way this answer mentions, and it worked!) Commented Oct 2, 2020 at 7:11
30

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))
5
  • 1
    This is no longer enabled by default: Settings -> Preferences -> Backup and ensure 'Enable session snapshot and periodic backup' is checked Commented Apr 1, 2020 at 18:47
  • Yes, the backups were in the user/<name>/appdata/backups folder, and I could simply recover them. The backups seem to be disabled now - so that is a gotcha.
    – amelvin
    Commented Jul 27, 2020 at 10:30
  • although I got too many duplicate new N files with empty content (probably because they were there in the backup folder with 0 bytes) but this script really helped me to recreate the session.xml. Thanks. Commented Jul 9, 2021 at 11:35
  • This doesn't seem to work anymore. When I'm loading a session in version 8.4.4, it is rewriting the .xml file I've written with session info and only loading the couple files that were named and saved. The unsaved and unnamed 'new N' style files are being discarded and even removed from the session.xml file.
    – NurShomik
    Commented Jul 28, 2022 at 19:13
  • @NurShomik, please check the other comments about backup settings. In the same settings windows, check for the "Backup path" settings. My script uses default settings, but maybe yours changed. Also, I've had to include other paths when using Notepad++'s portable version - when it changed to 64 bits, the backup path changed. Check this and try to modify npp_path. Commented Jul 31, 2022 at 2:09
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
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"

[System.IO.File]::WriteAllLines($session_file,
@'
<?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
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Oct 3, 2022 at 9:07
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).

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .