directory - 对 dir 进行区分大小写输入的解决方法
问题描述
我在 Windows 10 (x64) 上使用 Octave 5.1.0。我正在解析一系列目录,在每个目录中查找 Excel 电子表格,文件名中包含“日志”。问题是这些文件是手工创建的,文件命名不一致:有时是“日志”,有时是“日志”,等等......
看起来作为输入传递给dir
函数的字符串是区分大小写的,所以如果我没有正确的大小写,则dir
返回一个空结构。目前,我正在使用以下解决方法,但我想知道是否有更好的方法来做到这一点(首先,我没有捕获所有可能的大写/小写组合):
logbook = dir('*LogBook.xls*');
if isempty(logbook)
logbook = dir('*logbook.xls*');
if isempty(logbook)
logbook = dir('*Logbook.xls*');
if isempty(logbook)
logbook = dir('*logBook.xls*');
if isempty(logbook)
error(['Could not find logbook spreadsheet in ' dir_name '.'])
end
end
end
end
解决方案
您需要获取文件名列表(通过readdir
、dir
、ls
),然后在该列表中搜索字符串。如果你使用readdir
,它可以这样做:
[files, err, msg] = readdir ('.'); # read current directory
if (err != 0)
error ("failed to readdir (error code %d): %s", msg);
endif
logbook_indices = find (cellfun (@any, regexpi (files, 'logbook'));
logbook_filenames = files(logbook_indices);
一个不太标准的方法可能是:
glob ('*[lL][oO][gG][bB][oO][kK]*')
推荐阅读
- c# - c# 按多列分组,然后选择 count > 1 的所有字段
- javascript - 删除多个数组Javascript中重复的所有唯一值
- smartsheet-api - 遍历工作表并附加列
- c - 有人可以帮助解释这个 C 算法在做什么吗?
- c# - 为tabcontrol中的每个tabitem添加多个datatable和tabitem.content
- python - 卸载旧python后无法从atom运行py文件
- forms - 如果表单未绑定,如何选择 MS Access 组合框中的第一项
- android - 从图库中重命名选定的图像
- python - 在 Kaggle 内核上使用 train_test_Split 方法时显示错误。帮助表示赞赏
- json - 带有可选登录/密码和使用 perl 发送命令的 Telnet