<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Save unsaved files to a folder]]></title><description><![CDATA[<p dir="auto">Hello, with the code below I can successfully:</p>
<ul>
<li>save files that are already stored on the hard drive but could have unsaved modifications</li>
<li>saveas files that have not yet been saved.</li>
</ul>
<p dir="auto">My question: the code activates all files and it visually goes through all of the open tabs. I often have 50+ tabs open. Can I achieve the same result without activating ?</p>
<p dir="auto">2nd question but I am afraid I know the answer: Because of an issue with a variable counter in my code, I have saved all unsaved files as the SAME file: 01.txt. So I lost a lot of texts. Can I somehow get it back - other than the Notepad++ backup folder ?</p>
<pre><code>import os
from Npp import notepad

save_folder = r"D:\_\Notepad++tabs"

if not os.path.exists(save_folder):
    os.makedirs(save_folder)

counter = sum(len(files) for _, _, files in os.walk(save_folder))
for (full_file_name, _, file_index, _) in notepad.getFiles():
    notepad.activateFile(full_file_name)
    if os.path.exists(full_file_name):
        notepad.save()
    else:
        counter += 1
        new_file_path = os.path.join(save_folder, f"{counter:02}.txt")
        notepad.saveAs(new_file_path)
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/topic/26619/save-unsaved-files-to-a-folder</link><generator>RSS for Node</generator><lastBuildDate>Tue, 09 Jun 2026 05:40:21 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/26619.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 09 Feb 2025 01:34:09 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Save unsaved files to a folder on Sun, 09 Feb 2025 18:19:29 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: Alan-Kilborn">@<bdi>Alan-Kilborn</bdi></a><br />
Thank you for the feedback.</p>
<p dir="auto">I made changes to this:</p>
<pre><code>import os
import re
from Npp import notepad, editor


SAVE_FOLDER = r'D:\_\Notepad++tabs'
FILE_PATTERN = r'^\d{2}\.txt$'


if os.path.exists(SAVE_FOLDER):
    counter = sum(len([file for file in files if re.compile(FILE_PATTERN).search(file)]) for _, _, files in os.walk(SAVE_FOLDER))
else:
    os.makedirs(SAVE_FOLDER)
    counter = 0

for (full_file_name, bufferID, file_index, view) in notepad.getFiles():
    notepad.activateBufferID(bufferID)
    do_saveas = True
    if os.path.isfile(full_file_name) and os.sep in full_file_name:
        if os.path.exists(full_file_name):
            notepad.save()
            do_saveas = False

    if do_saveas:
        counter += 1
        new_file_path = os.path.join(SAVE_FOLDER, f"{counter:02}.txt")
        notepad.saveAs(new_file_path)
</code></pre>
<p dir="auto">Best regards,</p>
<p dir="auto">Wim</p>
]]></description><link>https://community.notepad-plus-plus.org/post/99737</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/99737</guid><dc:creator><![CDATA[Wim Gielis]]></dc:creator><pubDate>Sun, 09 Feb 2025 18:19:29 GMT</pubDate></item><item><title><![CDATA[Reply to Save unsaved files to a folder on Sun, 09 Feb 2025 12:54:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/wim-gielis" aria-label="Profile: Wim-Gielis">@<bdi>Wim-Gielis</bdi></a></p>
<p dir="auto">Some comments on the script:</p>
<ul>
<li>
<p dir="auto">If you start up Notepad++ and the first thing you do is think, “I should run the script to save all of these ‘new X’ files!”, and you run it, it doesn’t save any of them.  This is because <code>editor.getModify()</code> will return False for a file tab(s) that hasn’t been changed in the current run of Notepad++, even though you can plainly see a red-diskette (or yellow pencil) icon on the tab(s).  (Yep, this is annoying and I don’t like it)</p>
</li>
<li>
<p dir="auto">Technically, <code>if os.path.exists(full_file_name):</code> may not be a sufficient check.  First, I’d use <code>os.path.isfile()</code> instead.  Second, there is a remote chance that the function (either one) can return True even if the file has not been saved into the file system.  I add an additional check in when I’m doing it:  <code>if os.path.isfile(full_file_name) and os.sep in full_file_name:</code></p>
</li>
<li>
<p dir="auto">This is an amazing line of code: <code>counter = sum(len([file for file in files if re.compile(FILE_PATTERN).match(file)]) for _, _, files in os.walk(SAVE_FOLDER))</code>.  That’s both meant as a “good” and a “bad” comment.  :-)</p>
</li>
<li>
<p dir="auto">Because of the use of an f-string, in <code>f"{counter:02}.txt"</code>, this is a PythonScript 3.x script, not a PythonScript 2.x script.</p>
</li>
<li>
<p dir="auto">Because you use <code>re.match()</code>, you don’t need the <code>^</code> at the start of the regular expression.  I like to use <code>re.search()</code> and keep the caret (I do less mental gyrations later when looking at the code if I take this approach).</p>
</li>
<li>
<p dir="auto">Since you posted a new version of the script within 4 hours of the original, you should have just edited the posting of the first script and replaced it with the second.</p>
</li>
</ul>
]]></description><link>https://community.notepad-plus-plus.org/post/99732</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/99732</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Sun, 09 Feb 2025 12:54:16 GMT</pubDate></item><item><title><![CDATA[Reply to Save unsaved files to a folder on Sun, 09 Feb 2025 12:34:17 GMT]]></title><description><![CDATA[<p dir="auto">Better readable:</p>
<pre><code>import os
import re
from Npp import notepad, editor


SAVE_FOLDER = r'D:\_\Notepad++tabs'
FILE_PATTERN = r'^\d{2}\.txt$'


if os.path.exists(SAVE_FOLDER):
    counter = sum(len([file for file in files if re.compile(FILE_PATTERN).match(file)]) for _, _, files in os.walk(SAVE_FOLDER))
else:
    os.makedirs(SAVE_FOLDER)
    counter = 0

for (full_file_name, bufferID, file_index, view) in notepad.getFiles():
    # notepad.activateFile(full_file_name)
    notepad.activateBufferID(bufferID)
    if editor.getModify():
        if os.path.exists(full_file_name):
            notepad.save()
        else:
            counter += 1
            new_file_path = os.path.join(SAVE_FOLDER, f"{counter:02}.txt")
            notepad.saveAs(new_file_path)
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/99731</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/99731</guid><dc:creator><![CDATA[Wim Gielis]]></dc:creator><pubDate>Sun, 09 Feb 2025 12:34:17 GMT</pubDate></item><item><title><![CDATA[Reply to Save unsaved files to a folder on Sun, 09 Feb 2025 12:27:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: Alan-Kilborn">@<bdi>Alan-Kilborn</bdi></a> Thank you for the confirmation.<br />
For those who want it, a working script with regular expressions to satisfy incrementing files 01.txt, 02.txt, etc.:</p>
<pre><code>import os
import re
from Npp import notepad, editor

save_folder = r"D:\_\Notepad++tabs"

if not os.path.exists(save_folder):
    os.makedirs(save_folder)

pattern = re.compile(r'^\d{2}\.txt$')
counter = sum(len([file for file in files if pattern.match(file)]) for _, _, files in os.walk(save_folder))

for (full_file_name, bufferID, file_index, view) in notepad.getFiles():
    # notepad.activateFile(full_file_name)
    notepad.activateBufferID(bufferID)
    if editor.getModify():
        if os.path.exists(full_file_name):
            notepad.save()
        else:
            counter += 1
            new_file_path = os.path.join(save_folder, f"{counter:02}.txt")
            notepad.saveAs(new_file_path)
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/99729</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/99729</guid><dc:creator><![CDATA[Wim Gielis]]></dc:creator><pubDate>Sun, 09 Feb 2025 12:27:24 GMT</pubDate></item><item><title><![CDATA[Reply to Save unsaved files to a folder on Sun, 09 Feb 2025 11:07:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/wim-gielis" aria-label="Profile: Wim-Gielis">@<bdi>Wim-Gielis</bdi></a> said :</p>
<blockquote>
<p dir="auto">the code activates all files and it visually goes through all of the open tabs… Can I achieve the same result without activating ?</p>
</blockquote>
<p dir="auto">No.<br />
If you’re willing to stop working with have-not-yet-been-saved files, then issuing a <code>notepad.saveAllFiles()</code> will not cause “visually (going) through all of the open tabs”.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/wim-gielis" aria-label="Profile: Wim-Gielis">@<bdi>Wim-Gielis</bdi></a> said :</p>
<blockquote>
<p dir="auto">I have saved all unsaved files as the SAME file: 01.txt. So I lost a lot of texts. Can I somehow get it back - other than the Notepad++ backup folder ?</p>
</blockquote>
<p dir="auto">No (and yes, you already knew the answer).<br />
I don’t think the backup folder is going to help you in this situation.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/99727</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/99727</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Sun, 09 Feb 2025 11:07:19 GMT</pubDate></item></channel></rss>