How to skip temporary files in a folder
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Emily
am 30 Jun. 2022
Kommentiert: Image Analyst
am 7 Jul. 2022
I want to have matlab skip the tempoary excel files when grabbing the files from a folder and copying it to another.
This is the code I currently have
file1=flexdir(fullfile(folder1,['\*',ext]))';
for fileindex=1:size(file1,1)
fname1=files1{fileindex};
[~,fname,fext]=fileparts(fname1);
fname2=flexdir(fullfile(folder2,[fname fext]));
copyfile(fname1, finalpath);
end
I think I need to add something like but not sure where
filelist = filelist(~startsWith({filelist.name}, '~$''));
Also how do I add '_new' at the end of the new file name?
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 1 Jul. 2022
Try this
folder1 = pwd;
folder2 = fullfile(pwd, 'Copied Files');
if ~isfolder(folder2)
mkdir(folder2);
end
ext = '.csv';
filePattern = fullfile(folder1, ['*', ext]);
fileList = dir(filePattern);
for fileindex = 1 : numel(fileList)
% Skip file starting with ~
if startsWith(fileList(fileindex).name, '~')
continue;
end
sourceFileName = fullfile(fileList(fileindex).folder, fileList(fileindex).name);
[~,fname,fext]=fileparts(sourceFileName);
destinationFileName = fullfile(folder2,[fname fext]);
copyfile(sourceFileName, destinationFileName);
end
4 Kommentare
Image Analyst
am 7 Jul. 2022
Why are you doing this:
if ~exist('ext','var')|| ~(strcmp(ext, 'xlsx') || strcmp(ext,'.xls'))
ext = 'xlsx'
end
filePattern = fullfile(folder1, ['*', ext]);
? ext will not exist at that point because it's not passed in to the function.
It should be
filePattern = fullfile(folder1, '*.xls*');
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!