Search multiple image in a folder

9 次查看(过去 30 天)
monika roopak
monika roopak 2022-7-25
回答: Siraj 2023-9-13
I have folder of over 10,000 images, I need to search specific mulitple images by filename (such as 1.jpg, 15.jpg )and save all those images into another folder.
Could anyone please suggest how to code for this.
  4 个评论
Rik
Rik 2022-7-26
You can use dir to find all files, then use fullfile and movefile (or copyfile) in a loop.
Jan
Jan 2022-7-28
@monika roopak: If you want to get help, do not let the readers guess the details of your problem.
How is your list stored? As table, cell string, string array or vector containing the numerical indices? What does "which I want to search" mean?
The code will have about 4 or 5 lines only and is typed in 20 seconds, but currently this would include too much guessing of what you exactly need. See: How to ask a good question

请先登录,再进行评论。

回答(1 个)

Siraj
Siraj 2023-9-13
Hi! It is my understanding that you intend to choose specific files from a source folder and transfer them to another destination folder. The filenames of these specific files are stored in a list, and you want to move only these files.
To accomplish this task, you can iterate through the files in the source folder and determine whether each file should be moved or not. If a file is meant to be moved to the destination folder, you can utilize the "movefile" function to perform the actual file transfer. To obtain a list of all files in the source folder, the "dir" function can be used. To check if a filename exists in the list of files to be moved, you can employ the "ismember" function. For a clearer understanding, please refer to the provided code example and accompanying screenshots.
%path to source folder
sourceFolder = 'SourceFolder';
%path to destination folder
destinationFolder = 'DestinationFolder';
%looking up for all the files present in the SourceFolder
fileList = dir(fullfile(sourceFolder, '*.txt')); % Replace '*.txt' with the desired file extension or pattern
List_of_files_to_move = {'File1.txt', 'File4.txt'};
for i = 1:numel(fileList)
fileName = fileList(i).name;
% if this file, is one of the file that is to be moved.
if (ismember(fileName, List_of_files_to_move))
sourceFile = fullfile(sourceFolder, fileName);
destinationFile = fullfile(destinationFolder, fileName);
movefile(sourceFile, destinationFile);
end
end
Folder contents before executing the above code
Folder contents after executing the above code.
For more information on the "movefile", "dir", and "ismember" functions, you can refer to the following links:
These resources will provide you with detailed explanations and examples of how to use these functions effectively.
Hope this helps.

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by