开发者

possible to extract files according filename listed in a text file by using matlab?

i have thousand files in a folder, however, i only need to extract out hundred files from the folder according to the filename listed in a text file into new folder. The filenames in text file is listed as a column..is that possible to be run by using matlab?what is the code shall i need to write? Thanks.

example:

filenames.txt is in the C:\matlab

folder include thousand files is n开发者_运维技巧amed as BigFiles also in C:\matlab

files to be extracted from BigFiles folder is listed in column as below:

filenames.txt

a1sndh

sd3rfe

rgd4de

sd5erw

please advise...thanks...


Enumerate all files in a folder of a specific type (if needed) using:

 %main directory to process
directory = 'to_process';

%enumerate all files (.m in this case)
files = dir(fullfile(directory,'*.m'));

numfiles = length(files);
fprintf('Found %i files\n',numfiles)

Then you could load the single column using one of the many file I/O functions in Matlab.

Then just loop through all the input names and check it's name against all the read in files (files{i}.name), and if so, move it.


EDIT:

From what I understood, you are looking for a solution along the lines:

filenames.txt

a.txt
b.txt
c.txt
.
.
.

moveMyFiles.m

%# read filenames listed in a text file
fid = fopen('C:\matlab\filenames.txt');
fList = textscan(fid, '%s');
fList = fList{1};
fclose(fid);

%# source/destination folder names
sourceDir = 'C:\matlab\BigFiles';
destDir = 'C:\matlab\out';
if ~exist(destDir,'dir')
    mkdir(destDir);
end

%# move files one by one
for i=1:numel(fList)
    movefile(fullfile(sourceDir,fList{i}), fullfile(destDir,fList{i}));
end

You can replace the MOVEFILE function by COPYFILE if you simply want to copy the files instead of moving them...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜