聘我网

新概念招聘3.0

如何获取目录下的所有文件?

vote up0vote downstar

然后遍历他们,逐个操作,该如何写?

 

1 个答复

vote up0vote downcheck
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 

其中@(x) fullfile(dirName,x)是匿名函数

用例:

fileList = getAllFiles('D:\path'); 
链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.