我想知道是否可以在Notepad++的“查找文件”功能中的文件过滤器中列出一个排除项。
例如,以下内容将在所有文件中将Dog替换为Cat。
查找内容:Dog
替换为:Cat
过滤器:*.*
我想要做的是在所有文件中将Dog替换为Cat,但不包括.sh文件。
这个可行吗?
例如,以下内容将在所有文件中将Dog替换为Cat。
查找内容:Dog
替换为:Cat
过滤器:*.*
我想要做的是在所有文件中将Dog替换为Cat,但不包括.sh文件。
这个可行吗?
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;
}
.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()