简体   繁体   English

Notepad ++文件过滤器

[英]Notepad++ File Filters

I was wondering if it was possible to list an exclusion within the file filters in the "find in files" functionality of Notepad++. 我想知道是否可以在Notepad ++的“查找文件”功能中列出文件过滤器中的排除项。

For example the following will replace Dog with Cat in all files. 例如,以下内容将在所有文件中替换Dog with Cat。

Find what: Dog 找到什么:狗

Replace with: Cat 替换为:Cat

Filters: *.* 过滤器:*。*

What I would like to do is replace Dog with Cat in all files except those in .sh files. 我想做的是在所有文件中替换Dog with Cat,除了.sh文件中的那些文件。

Is this possible? 这可能吗?

I think something like a "negative selector" does not exist in Notepad++. 我认为在Notepad ++中不存在类似“否定选择器”的东西。

I took a quick look at the 5.6.6 source code and it seems like the file selection mechanism boils down to a function called getMatchedFilenames() which recursively runs through all the files below a certain directory, which in turn calls the following function to see whether the filename matches the pattern: 我快速浏览了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;
}

As far as I can determine, PathMatchSpec does not allow negative selectors. 据我所知, PathMatchSpec不允许使用否定选择器。

It is however possible to enter a list of positive filters . 但是,可以输入正滤波器列表 If you could make that list long enough to include all the extensions in your directory except .sh , you're also there. 如果您可以将该列表设置得足够长,以包含除.sh之外的目录中的所有扩展名,那么您也可以使用。

Good luck! 祝好运!

Great answer by littlegreen. littlegreen的答案很棒。
Unfortunate that Notepad++ can't do it. 不幸的是,Notepad ++无法做到这一点。

This tested example will do the trick (Python). 这个经过测试的例子可以解决这个问题(Python)。 replace method thanks to Thomas Watnedal : 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM