Save unsaved files to a folder
-
Hello, with the code below I can successfully:
- save files that are already stored on the hard drive but could have unsaved modifications
- saveas files that have not yet been saved.
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 ?
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 ?
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)
-
@Wim-Gielis said :
the code activates all files and it visually goes through all of the open tabs… Can I achieve the same result without activating ?
No.
If you’re willing to stop working with have-not-yet-been-saved files, then issuing anotepad.saveAllFiles()
will not cause “visually (going) through all of the open tabs”.@Wim-Gielis said :
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 ?
No (and yes, you already knew the answer).
I don’t think the backup folder is going to help you in this situation. -
@Alan-Kilborn Thank you for the confirmation.
For those who want it, a working script with regular expressions to satisfy incrementing files 01.txt, 02.txt, etc.: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)
-
Better readable:
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)
-
Some comments on the script:
-
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
editor.getModify()
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) -
Technically,
if os.path.exists(full_file_name):
may not be a sufficient check. First, I’d useos.path.isfile()
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:if os.path.isfile(full_file_name) and os.sep in full_file_name:
-
This is an amazing line of code:
counter = sum(len([file for file in files if re.compile(FILE_PATTERN).match(file)]) for _, _, files in os.walk(SAVE_FOLDER))
. That’s both meant as a “good” and a “bad” comment. :-) -
Because of the use of an f-string, in
f"{counter:02}.txt"
, this is a PythonScript 3.x script, not a PythonScript 2.x script. -
Because you use
re.match()
, you don’t need the^
at the start of the regular expression. I like to usere.search()
and keep the caret (I do less mental gyrations later when looking at the code if I take this approach). -
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.
-
-
@Alan-Kilborn
Thank you for the feedback.I made changes to this:
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)
Best regards,
Wim