日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Matlab實現獲取文件夾下所有指定后綴的文件_C 語言

作者:Toblerone_Wind ? 更新時間: 2022-12-08 編程語言

1. 要求

獲取指定文件夾下(包含子文件夾),所有指定后綴(如txt)的文件路徑(即文件所在目錄+文件名),返回一個字符串數組。

2. 代碼

獲取C:/Users/Administrator/Desktop文件夾下(包含子文件夾)的所有.m文件

clear
dirOutput = dir('C:/Users/Administrator/Desktop/**/*.m');
folder = string({dirOutput.folder}');
file = string({dirOutput.name}');
filepath = strcat(folder, '\', file);

獲取C:/Users/Administrator/Desktop文件夾下(不包含子文件夾)的所有.m文件

clear
dirOutput = dir('C:/Users/Administrator/Desktop/*.m');
folder = string({dirOutput.folder}');
file = string({dirOutput.name}');
filepath = strcat(folder, '\', file);

封裝成函數。調用時若輸入兩個參數,則默認搜索子文件夾

function filepath = getFilesPath(baseDir, ext, findSubfile)
if nargin == 2  % 判斷輸入的參數是否為2
    findSubfile = true;
end
if findSubfile
    dirOutput = dir([baseDir '/**/*.' ext]);
else
    dirOutput = dir([baseDir '/*.' ext]);
end
folder = string({dirOutput.folder}');
file = string({dirOutput.name}');
filepath = strcat(folder, '\', file);
end

調用方法

getFilesPath('C:/Users/Administrator/Desktop', 'm')

結果展示

3. 參考

未仔細學習dir()函數之前

本文介紹一種通過MATLAB獲取當前文件夾下所有文件名稱的方法,包含子文件夾內的文件名稱,請參考。

主要用到的函數包括uigetdir()、dir()。

AidDir = uigetdir();     % 通過交互的方式選擇一個文件夾
if AidDir == 0             % 用戶取消選擇
    fprintf('Please Select a New Folder!\n');
else
    ParentFolder = dir(AidDir);    % 獲取當前文件夾下的信息
    AllFile = struct;            % 建立空結構體
    AllFile = GetAllFile(ParentFolder,AllFile);
    if isempty(fieldnames(AllFile))
        fprintf('There are no files in this folder!\n');
    else    % 當前文件夾下有文件,反饋文件數量
        fprintf('Number of Files: %i \n',size(AllFile,2));
    end
end
function [AllFileStruct] = GetAllFile(ParentFolder,AllFile) 
AllFileStruct = AllFile;
if isempty(fieldnames(AllFile))
    FileNum = 0; % 用于統計文件數量
else
    FileNum = size(AllFileStruct,2);
end
try
    %從第3個開始是文件,如果報錯,則表明當前文件夾沒有文件,運行catch
    for i = 3:length(ParentFolder) 
        if ParentFolder(i).isdir == 1 % 當前是文件夾
            SubFolder = [ParentFolder(i).folder,'\',ParentFolder(i).name];
            Temp = dir(SubFolder);
            AllFileStruct = GetAllFile(Temp,AllFileStruct); % 函數遞歸
            FileNum = size(AllFileStruct,2);
        else
            FileNum = FileNum + 1;
            %通過結構體可以存儲多種數據,本文僅以文件名稱為例
            AllFileStruct(FileNum).name = ParentFolder(i).name; 
        end
    end
catch
end
end

最后通過查看AllFile變量即可獲取當前文件夾下所有文件名稱。

仔細學習dir()函數之后

先放代碼:

AidDir = uigetdir();     % 通過交互的方式選擇一個文件夾
if AidDir == 0             % 用戶取消選擇
    fprintf('Please Select a New Folder!\n');
else
    cd(AidDir)
    RawFile = dir('**/*.*'); %主要是這個結構,可以提取所有文件
    AllFile = RawFile([RawFile.isdir]==0);
    if isempty(fieldnames(AllFile))
        fprintf('There are no files in this folder!\n');
    else    % 當前文件夾下有文件,反饋文件數量
        fprintf('Number of Files: %i \n',size(AllFile,1));
    end
end

沒了。。。。不需要畫蛇添足。。。。

dir()函數以n×1結構體數組形式返回,其中n是dir()函數返回的文件和文件夾的數量,下表中顯示了結構體中的字段。

字段名稱 說明
name 文件或文件夾名稱 char
folder 文件或文件夾的位置 char
date 修改日期時間戳 char
bytes 文件大小(以字節為單位) double
isdir 如果名稱為文件夾,則為1;如果名稱為文件,則為0 logical
datenum 修改日期是一個日期序列值 double

提取指定擴展名的文件dir('**/*.m')

原文鏈接:https://blog.csdn.net/qq_42276781/article/details/127739572

欄目分類
最近更新