How to select specific files from all the files in a folder?

조회 수: 35 (최근 30일)
Ashfaq Ahmed
Ashfaq Ahmed 2022년 2월 2일
댓글: Benjamin Thompson 2022년 2월 2일
Hi all,
As you can see I have a lot of files in this folder, but I want only those files that contains '_020_' in it's name, and I want to save them in a new folder within the same directory. How can I do it?

채택된 답변

Stephen23
Stephen23 2022년 2월 2일
편집: Stephen23 2022년 2월 2일
This is easy using COPYFILE or MOVEFILE with the appropriate wildcard characters, e.g.:
P = 'absolute or relative path to where the files are saved';
copyfile(fullfile(P,'*_020_*.nc'), fullfile(P,'newfolder'));
  댓글 수: 2
Ashfaq Ahmed
Ashfaq Ahmed 2022년 2월 2일
Hi,
it shows
Error using copyfile
Unknown error occurred.
Error in selecting_souths (line 4)
copyfile(fullfile(P,'*_020_*.nc'),
fullfile(P,'newfolder'));
Benjamin Thompson
Benjamin Thompson 2022년 2월 2일
You may be missing the three dots "..." in the copyfile command. If you stop a line in MATLAB and put the rest of a command on a new line, you must use ... to separate them.

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

추가 답변 (1개)

Benjamin Thompson
Benjamin Thompson 2022년 2월 2일
You can get a file or directory listing in MATLAB using dir, and assign that to a workspace variable. Then loop through the elements in the array to tets the name field against your criteria. See the help article "Manage Files and Folder", you have the mkdir command, movefile command. Use copyfile for the copy, which oddly enough is not mentioned with the others in that article.
D = dir('*.*')
D =
48×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
>> D(1).name
ans =
'.'
>> D(3).name
ans =
'ArUcoMarker1.jpg'
mkdir myNewFolder
copyfile(D(3).name, 'myNewFolder')

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by