Notepad ++ File Filters

I was wondering if it is possible to specify an exception in file filters in the Find in Files functions of Notepad ++.

For example, the following will replace Dog with Cat in all files.

Find that: Dog

Replace with: Cat

Filters: *. *

What I would like to do is replace Dog with Cat in all files except .sh files.

Is it possible?

+6
filter replace notepad ++
source share
2 answers

I think something like a "negative selector" does not exist in Notepad ++.

I quickly took a look at 5.6.6 source code and it seems that the file selection mechanism comes down to a function called getMatchedFilenames() , which recursively goes through all the files under a specific directory, which in turn calls the following function to see if file name for template:

 bool Notepad_plus::matchInList(const TCHAR *fileName, const vector<generic_string> & patterns) { for (size_t i = 0 ; i < patterns.size() ; i++) { if (PathMatchSpec(fileName, patterns[i].c_str())) return true; } return false; } 

As far as I can tell, PathMatchSpec does not allow negative selectors.

However, you can enter a list of positive filters . If you can make this list long enough to include all extensions in your directory except .sh , you are there too.

Good luck

+9
source share

Great answer from littlegreen.
Unfortunately, Notepad ++ cannot do this.

This tested example will do the trick (Python). replace thanks to Thomas Watnedal :

 from tempfile import mkstemp import glob import os import shutil def replace(file, pattern, subst): """ from Thomas Watnedal answer to SO question 39086 search-and-replace-a-line-in-a-file-in-python """ fh, abs_path = mkstemp() # create temp file new_file = open(abs_path,'w') old_file = open(file) for line in old_file: new_file.write(line.replace(pattern, subst)) new_file.close() # close temp file os.close(fh) old_file.close() os.remove(file) # remove original file shutil.move(abs_path, file) # move new file def main(): DIR = '/path/to/my/dir' path = os.path.join(DIR, "*") files = glob.glob(path) for f in files: if not f.endswith('.sh'): replace(f, 'dog', "cat") if __name__ == '__main__': main() 
+3
source share

All Articles