如何检查文件夹是否存在于搜索路径中?

3
我在外置驱动器上有一个文件夹,其中包含50多个文件夹,每个文件夹都有2000多个文件。这50个文件夹中没有子文件夹。我想将MATLAB搜索路径中的所有文件夹添加进去,所以我执行了addpath(genpath(...))。这需要大约5分钟。如果这些文件夹已经在搜索路径中,我不想再次重复这个操作。我怎样才能确定呢?
我知道我可以使用which测试文件是否在搜索路径中,但我想查看主文件夹(其中有50个子文件夹)和子文件夹是否在搜索路径中。我该怎么做呢?
我甚至尝试过使用exist命令,但即使文件夹不在搜索路径中,它也会给我返回非零值。

你能在子目录中找到一个m文件来使用吗?exist对于文件夹是一样的,但如果有一个函数在其中,你可以检查该函数是否在你的路径上。 - Raab70
不,没有 .m 文件。它是一个文件夹 -> 50 个文件夹 -> 每个文件夹中有 2000 个文件。下面给出的答案完美地解决了这个问题。 - Autonomous
2个回答

4

单目录搜索案例

%%// path_to_be_searched is the folder or directory to be detected 
%%// to be in path or not

%%// colon is the separator used for paths under Linux.
%%// For Windows and others, it needs to be investigated.
path_list_cell = regexp(path,pathsep,'Split')

if any(ismember(path_to_be_searched,path_list_cell))
    disp('Yes, this directory is in MATLAB path');
else
    disp('No, this directory is not in MATLAB path');
end

搜索主目录和子目录时添加选项

对于基本路径以及子目录搜索,以下代码将尝试查找每个子目录和基本路径的匹配项,并添加任何缺失的内容。因此,即使您已选择性地从路径中删除了任何子目录或甚至是基本路径,此代码也会负责添加路径中缺失的所有内容。

%%// basepath1 is the path to the main directory with sub-directories that
%%// are to detected for presence

basepath_to_be_searched = genpath(basepath1)
basepath_list_cell = regexp(basepath_to_be_searched,pathsep,'Split')

%%// Remove empty cells
basepath_list_cell = basepath_list_cell(~cellfun(@isempty,basepath_list_cell))

path_list_cell = regexp(path,pathsep,'Split');

ind1 = ismember(basepath_list_cell,path_list_cell)

%%// Add the missing paths
addpath(strjoin(strcat(basepath_list_cell(~ind1),pathsep),''))

%%// strjoin is a recent MATLAB addition and is also available on file-exchange -
%%// http://www.mathworks.in/matlabcentral/fileexchange/31862-strjoin

谢谢你提供这段代码。我正在通过编辑你的代码来添加Windows版本。如果你觉得合适,可以保留它。此外,我建议你修改一下代码。你可以将any(ismember(...))反转输入参数,并将其写成(ismember(path_to_be_searched,path_list_cell))。这样你就会得到与第一个参数相同长度的数组。这样做会使过程更加简单。我会稍等一下再接受你的答案。 - Autonomous
1
@ParagS.Chandakkar 几个编辑 - 1. 添加了 pathsep,它必须处理 Linux 和 Windows 之间的冒号和分号问题。2. 我已经采纳了您使用 path_to_be_searched 作为 ismember 的第一个参数的建议,并且在这里完美地适合。3. 添加了 **主目录及其子目录搜索情况**,我认为这是您的主要目标,这也可以让人们添加缺失的路径。希望这解决了您所有的问题! - Divakar

0

虽然这是一个老问题,但是我们可以使用单元数组的方式来拆分path()字符串。不确定对于大量文件夹而言,它是否比regexp方法更快或更慢,但我认为它在概念上更简单。

首先,创建一个用于检查单个路径的函数。该函数使用p_array=strsplit(path(),pathsep);来创建单元数组,然后使用any(strcmp(p_array,folder_to_search_for))来检查您要查找的文件夹是否在单元数组中。它只匹配完整的字符串。

function folder_in_path=checkPath(folder_to_search_for)
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % folder_in_path=checkPath(folder_to_search_for)
  % 
  % Input: 
  %   folder_to_search_for   string, the folder to check 
  %
  % Output: 
  %   folder_in_path    1 if in path, 0 if not in path 
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  % create a cell array with each folder as a cell
  p_array=strsplit(path(),pathsep); 

  % search the cell array for your folder_to_search_for
  if any(strcmp(p_array,folder_to_search_for))
     folder_in_path=1;
  else 
     folder_in_path=0;
  end 
end

如果要检查存储在top_level_dir中的所有子目录并添加缺失的子目录:

% generate cell array of all the subdirs in top_level_dir:
allthesubdirs=strsplit(genpath(top_level_dir),pathsep);

% check each one and add to path if not there
for i_fo=1:numel(allthesubdirs)  
  if ~checkPath(allthesubdirs{i_fo})
     disp(['adding ',allthesubdirs{i_fo},' to the path'])
     addpath(allthesubdirs{i_fo})
  else
     disp([allthesubdirs{i_fo},' is already in the path'])
  end
end 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接