我想知道是否可以在 Notepad++ 的“在文件中查找”功能的文件过滤器中列出排除项。
例如,以下将在所有文件中将 Dog 替换为 Cat。
找什么:狗
替换为:猫
过滤器:*.*
我想做的是在所有文件中用 Cat 替换 Dog ,除了 .sh 文件中的文件。
这可能吗?
我认为 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
,那么您也在那里。
祝你好运!
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()