How to skip temporary files in a folder

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?

 채택된 답변

Image Analyst
Image Analyst 2022년 7월 1일

0 개 추천

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

Emily
Emily 2022년 7월 1일
It doesn't seem like it's copying the tempoary files but when I do the code below on the new copied files folder, it still shows tempoary files. Maybe someone is else is using the folder as well.
newfiles=flexdir(fullfile(folder2,['\*'])
Is there a way to have newfiles = non tempoary files only?
Image Analyst
Image Analyst 2022년 7월 1일
편집: Image Analyst 2022년 7월 1일
Maybe the files were already there. Try printing out each file as it gets copied and see if there are any starting with a tilde (there should NOT be)
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
% Prepare source filename.
sourceFileName = fullfile(fileList(fileindex).folder, fileList(fileindex).name);
% Prepare destination filename.
[~,fname,fext]=fileparts(sourceFileName);
destinationFileName = fullfile(folder2,[fname fext]);
% Print out what is getting copied.
fprintf('Copying "%s"\n to "%s".\n', sourceFileName, destinationFileName)
% Do the copy.
copyfile(sourceFileName, destinationFileName);
end
What is flexdir?
Emily
Emily 2022년 7월 7일
Hi, I was able to take your input to account and currently have this.
Right now this is working except the one of the file it is supposed to copy over.
It is able to make the copy of the file but when I try to open the excel file it is all grayed out for me.
The original file looks normal so I'm not sure what's wrong.
function excelcopy(folder1, folder2)
if ~exist('ext','var')|| ~(strcmp(ext, 'xlsx') || strcmp(ext,'.xls'))
ext = 'xlsx'
end
filePattern = fullfile(folder1, ['*', ext]);
fileList = dir(filePattern);
for fileindex = 1 : numel(fileList)
% Skip file starting with ~
if startsWith(fileList(fileindex).name, '~')
continue;
end
% Prepare source filename.
sourceFileName = fullfile(fileList(fileindex).folder, fileList(fileindex).name);
% Prepare destination filename.
[~,fname,fext]=fileparts(sourceFileName);
destinationFileName = fullfile(folder2,[fname fext]);
% Print out what is getting copied.
fprintf('Copying "%s"\n to "%s".\n', sourceFileName, destinationFileName)
% Do the copy.
copyfile(sourceFileName, destinationFileName);
end
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*');

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

태그

질문:

2022년 6월 30일

댓글:

2022년 7월 7일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by