我需要获取 D:\dic
下的所有文件,并循环逐个进行进一步处理。
MATLAB支持这种操作吗?
其他脚本语言如PHP、Python中可以实现。
更新: 鉴于这篇文章相当古老,而且我在此期间已经为自己的使用修改了这个实用程序很多次,所以我认为我应该发布一个新版本。我的最新代码可以在The MathWorks File Exchange上找到:dirPlus.m
。你也可以从GitHub获取源代码。
我进行了许多改进。现在它提供了许多选项,可选择是否前缀完整路径或仅返回文件名(合并自Doresoom和Oz Radiano),并对文件名应用正则表达式模式(合并自Peter D)。此外,我还添加了对每个文件应用验证函数的功能,使您可以根据除了文件名之外的其他标准(例如文件大小、内容、创建日期等)来选择它们。
注意: 在 MATLAB 的更新版本(R2016b及更高版本)中,dir
函数具有递归搜索功能!因此,您可以执行以下操作,以获取当前文件夹的所有子文件夹中所有*.m
文件的列表:
dirData = dir('**/*.m');
以下是一个函数,它递归地搜索给定目录的所有子目录,收集它找到的所有文件名的列表:
function fileList = getAllFiles(dirName)
dirData = dir(dirName); %# Get the data for the current directory
dirIndex = [dirData.isdir]; %# Find the index for directories
fileList = {dirData(~dirIndex).name}'; %'# Get a list of the files
if ~isempty(fileList)
fileList = cellfun(@(x) fullfile(dirName,x),... %# Prepend path to files
fileList,'UniformOutput',false);
end
subDirs = {dirData(dirIndex).name}; %# Get a list of the subdirectories
validIndex = ~ismember(subDirs,{'.','..'}); %# Find index of subdirectories
%# that are not '.' or '..'
for iDir = find(validIndex) %# Loop over valid subdirectories
nextDir = fullfile(dirName,subDirs{iDir}); %# Get the subdirectory path
fileList = [fileList; getAllFiles(nextDir)]; %# Recursively call getAllFiles
end
end
将上述函数保存在MATLAB路径的某个位置之后,您可以按以下方式调用它:
fileList = getAllFiles('D:\dic');
dirlist = dir('.');
for i = 1:length(dirlist)
dirlist(i)
end
这应该为您提供以下格式的输出,例如:
name: 'my_file'
date: '01-Jan-2010 12:00:00'
bytes: 56
isdir: 0
datenum: []
.
和 ..
? - Gtkerdir('*.ext')
命令,它会自动排除目录(除非它们以 .ext 结尾)。 - Jonas我使用了在这个好答案中提到的代码,并进行了扩展,以支持我需要的两个附加参数。这些参数是要过滤的文件扩展名和指示是否将完整路径连接到文件名的标志。
我希望它已经足够清楚,并且有人会发现它对他们有益。
function fileList = getAllFiles(dirName, fileExtension, appendFullPath)
dirData = dir([dirName '/' fileExtension]); %# Get the data for the current directory
dirWithSubFolders = dir(dirName);
dirIndex = [dirWithSubFolders.isdir]; %# Find the index for directories
fileList = {dirData.name}'; %'# Get a list of the files
if ~isempty(fileList)
if appendFullPath
fileList = cellfun(@(x) fullfile(dirName,x),... %# Prepend path to files
fileList,'UniformOutput',false);
end
end
subDirs = {dirWithSubFolders(dirIndex).name}; %# Get a list of the subdirectories
validIndex = ~ismember(subDirs,{'.','..'}); %# Find index of subdirectories
%# that are not '.' or '..'
for iDir = find(validIndex) %# Loop over valid subdirectories
nextDir = fullfile(dirName,subDirs{iDir}); %# Get the subdirectory path
fileList = [fileList; getAllFiles(nextDir, fileExtension, appendFullPath)]; %# Recursively call getAllFiles
end
end
运行代码的示例:
fileList = getAllFiles(dirName, '*.xml', 0); %#0 is false obviously
.
和..
。
或者,如果你只想要目录中的文件而不是文件夹,你可以使用isdir
字段。list=dir(pwd); %get info of files/folders in current directory
isfile=~[list.isdir]; %determine index of files vs folders
filenames={list(isfile).name}; %create cell array of file names
filenames={list(~[list.isdir]).name};
列出目录中除了“.”和“..”之外的文件夹列表
dirnames={list([list.isdir]).name};
dirnames=dirnames(~(strcmp('.',dirnames)|strcmp('..',dirnames)));
这个答案并没有直接回答问题,但可能是一个创意解决方案。
我赞同gnovice的解决方案,但我想提供另一种解决方案:使用您操作系统的特定命令:
tic
asdfList = getAllFiles('../TIMIT_FULL/train');
toc
% Elapsed time is 19.066170 seconds.
tic
[status,cmdout] = system('find ../TIMIT_FULL/train/ -iname "*.wav"');
C = strsplit(strtrim(cmdout));
toc
% Elapsed time is 0.603163 seconds.
优点:
*.wav
文件的新语法。缺点:
genpath
来递归列出子目录。返回的列表是一个分号分隔的目录字符串,所以你需要使用strread来分离它,例如:
dirlist = strread(genpath('/path/of/directory'),'%s','delimiter',';')
如果你不想包括给定的目录,请删除dirlist
的第一个条目,即dirlist(1)=[];
,因为它总是第一个条目。dir
来获取每个目录中的文件列表。filenamelist=[];
for d=1:length(dirlist)
% keep only filenames
filelist=dir(dirlist{d});
filelist={filelist.name};
% remove '.' and '..' entries
filelist([strmatch('.',filelist,'exact');strmatch('..',filelist,'exact'))=[];
% or to ignore all hidden files, use filelist(strmatch('.',filelist))=[];
% prepend directory name to each filename entry, separated by filesep*
for f=1:length(filelist)
filelist{f}=[dirlist{d} filesep filelist{f}];
end
filenamelist=[filenamelist filelist];
end
filesep
函数返回MATLAB运行平台的目录分隔符。
这将在单元格数组filenamelist中给出具有完整路径的文件名列表。虽然不是最简洁的解决方案,但也能起到帮助作用。
genpath
,它会进行两次搜索。 - Gtker.mat
)的文件名! function filenames = getFilenames(rootDir, format)
% Get filenames with specified `format` in given `foler`
%
% Parameters
% ----------
% - rootDir: char vector
% Target folder
% - format: char vector = 'mat'
% File foramt
% default values
if ~exist('format', 'var')
format = 'mat';
end
format = ['*.', format];
filenames = dir(fullfile(rootDir, format));
filenames = arrayfun(...
@(x) fullfile(x.folder, x.name), ...
filenames, ...
'UniformOutput', false ...
);
end
filenames = getFilenames('D:/dic/**');
for i = 1:numel(filenames)
filename = filenames{i};
% do your job!
end
通过稍微修改但几乎相同的方法来获取每个子文件夹的完整文件路径
dataFolderPath = 'UCR_TS_Archive_2015/';
dirData = dir(dataFolderPath); %# Get the data for the current directory
dirIndex = [dirData.isdir]; %# Find the index for directories
fileList = {dirData(~dirIndex).name}'; %'# Get a list of the files
if ~isempty(fileList)
fileList = cellfun(@(x) fullfile(dataFolderPath,x),... %# Prepend path to files
fileList,'UniformOutput',false);
end
subDirs = {dirData(dirIndex).name}; %# Get a list of the subdirectories
validIndex = ~ismember(subDirs,{'.','..'}); %# Find index of subdirectories
%# that are not '.' or '..'
for iDir = find(validIndex) %# Loop over valid subdirectories
nextDir = fullfile(dataFolderPath,subDirs{iDir}); %# Get the subdirectory path
getAllFiles = dir(nextDir);
for k = 1:1:size(getAllFiles,1)
validFileIndex = ~ismember(getAllFiles(k,1).name,{'.','..'});
if(validFileIndex)
filePathComplete = fullfile(nextDir,getAllFiles(k,1).name);
fprintf('The Complete File Path: %s\n', filePathComplete);
end
end
end
fileList = strcat(dirName,filesep,fileList);
而不是使用CELLFUN,但是这样可能会产生额外的不必要的文件分隔符,FULLFILE也可以为您处理。 - gnoviceif ~isempty(fileList) fileList = cellfun(@(x) fullfile(dirName,x),... %# Prepend path to files fileList,'UniformOutput',false); matchstart = regexp(fileList, pattern); fileList = fileList(~cellfun(@isempty, matchstart)); end
并将函数签名更改为getAllFiles(dirName, pattern)
(也在倒数第二行)。 - Peter D