Notepad++文件过滤器

6
我想知道是否可以在Notepad++的“查找文件”功能中的文件过滤器中列出一个排除项。
例如,以下内容将在所有文件中将Dog替换为Cat。
查找内容:Dog
替换为:Cat
过滤器:*.*
我想要做的是在所有文件中将Dog替换为Cat,但不包括.sh文件。
这个可行吗?
2个回答

9
我认为像“负选择器”这样的功能在Notepad ++中不存在。
我快速查看了5.6.6源代码,似乎文件选择机制归结为一个名为getMatchedFilenames()的函数,该函数递归运行所有在某个目录下的文件,并调用以下函数来查看文件名是否匹配模式:
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;
}

据我所知,PathMatchSpec 不允许使用负选择器。
但是,可以输入 一系列正过滤器。如果您能将该列表设置得足够长,以包括目录中除 .sh 之外的所有扩展名,则也可以实现该目的。
祝好运!

1
使用PathMatchSpec来排除文件匹配模式,如果模式以“-”减号开头,则需要使用两个bool变量:matched和excluded。该方法将不会在循环内返回。最终返回值将是!excluded && matched。 - Robert Cutajar

3

由littlegreen提供的很好的答案
不幸的是,Notepad++无法实现它。

这个经过测试的示例可以解决问题(Python)。replace方法感谢Thomas Watnedal

from tempfile import mkstemp
import glob
import os
import shutil

def replace(file, pattern, subst):
    """ from Thomas Watnedal's 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()

太酷了。然后你可以将脚本添加到Nppexec插件的可执行文件列表中。真正的程序员不需要GUI :-) - littlegreen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接