Matlab脚本:删除目标类型文件
%% 获取文件类型目标特征文件的路径和名称
% date:2021.01.11
%% file content
clc
clear
subdirectories = genpath( '路径' );
% ispc - 确定版本是否适用于 Windows (PC) 平台
if ispc
% 匹配正则表达式
subdirectories = regexp( subdirectories, '[^;]*', 'match' );
else
subdirectories = regexp( subdirectories, '[^:]*', 'match' );
end
total_list = {};
%% 获取文件列表
for i = 1:length( subdirectories )
% dir()列出当前文件夹中的文件和文件夹。
files = dir( subdirectories{ i } );
% 判断生成文件夹isdir属性
% isdir=1,代表文件夹,否则,代表文件
% 获得文件夹目录中的文件
files = files( ~[ files.isdir ] );
% 比较字符串(不区分大小写),相同,返回1,否则,返回0
if ~strcmpi( 2, '*' )
for j = 1:length( files )
% 对比文件的扩展名与目标文件类型
if strcmpi( file_get_extension( files(j).name), '2)')
% 获得匹配文件的路径
total_list{ end + 1, 1 } = subdirectories{ i };
% 获得文件名称
total_list{ end , 2 } = files(j).name;
end
end
end
end
%%if 1 删除文件
%% if 0 统计文件名
if 0
for i=1:length(total_list)
obj = strcat(total_list{i,1},filesep,total_list{i,2});
delete(obj)
end
end
% 获得文件的扩展名
function extension = file_get_extension( filename )
extension = '';
% strfind - 在其他字符串中查找字符串,返回起始索引
idx = strfind( filename, '.' );
if ~isempty( idx )
% 获得文件的扩展名
extension = strcat( filename( idx - 2 ), filename( idx - 1 ));
end
end