How to copy files to different folders based on part of filename (strings and numbers)

7 ビュー (過去 30 日間)
Robert
Robert 2018 年 2 月 17 日
コメント済み: Greg 2018 年 2 月 17 日
Hi,
I haven't been able to find a solution to my problem when trying to copy files with similar part of filename, including words and numbers. I have a few thousand files (each file has GB's of data) with names like these:
Land_0020_AAAA_VXC_XXXxXX_19910311_19970329_01.51mEa_072414.csv
Land_0075_AAAA_VXC_XXXxXX_19890615_19930119_04.11mEa_052274.csv
Land_0077_AAAA_VXC_XXXxXX_19930611_19970319_11.48mNo_0624394.csv
Land_0079_AAAA_VXC_XXXxXX_19790815_19930109_17.52mFa_0224343.csv
Land_0083_AAAA_XDC_XXXxXX_19930611_19970319_11.38mEa_0644043.csv
Land_0089_AAAA_XDC_XXXxXX_19790815_19930109_19.57mNa_0124343.csv
Land_0120_BBBB_Msx_XXXxXX_19910311_19970329_01.51mEa_07243430.csv
Land_0125_CCCC_PLK_XXXxXX_19890615_19930119_04.11mPa_0522143.csv
Land_0130_BBBB_Msx_XXXxXX_19930611_19970319_11.48mEa_0621713.csv
Land_0137_CCCC_PLK_XXXxXX_19790815_19930109_16.52mNa_0220346.csv
...
Land_8169_ABAB_Mhl_XXXxXX_19800617_19930121_19.32mSa_0227336.csv
...
In filenames above, the combination of letters like (AAAA and VXC) and numbers like (01.51) are the key info for my identification before copying files to different folders.
My main problem in script below is that my statements don’t work as I want…
if (Code1 =='AAAA' & Code2='VXC' & Code3 > 0.00 & Code3 <= 4,99 );
This is the example of my attempt ...
cd ..
myFolder = 'A:\..';
filePattern = fullfile(myFolder, '*.csv');
theFiles = dir(filePattern);
FileNames = theFiles;
NumFiles = size(FileNames);
%Loop through files
for ifile=1:NumFiles;
FileTemp=FileNames(ifile).name
Code1 = FileTemp(11:14); %Read AAAA from above first filenames
Code2 = FileTemp(16:18); %Read VXC from above first filenames
Code3 = str2num(FileTemp(45:49)); %Read 01.51 from above first filename
clear FileIwant;
count=0
%STATEMENTS
if (Code1 =='AAAA' & Code2='VXC' & Code3 > 0.00 & Code3 <= 4,99 );
%Create new folder or use existent
aa = fullfile(pwd, [Code1 '_' Code2 '_Land0-4m']); if ~exist(aa, 'dir'); mkdir(aa); end
%Create and copy file that follow statement
FileIwant{count}=FileTemp;
copyfile(char(FileIwant), aa);
elseif
if (Code1 =='AAAA' & Code2='VXC' & Code3 > 5.00 & Code3 <= 9,99 );
%Create new folder or use existent
bb = fullfile(pwd, [Code1 '_' Code2 '_Land5-10m']); if ~exist(bb, 'dir'); mkdir(bb); end
%Create and copy file that follow statement
FileIwant{count}=FileTemp;
copyfile(char(FileIwant), bb);
else
if (Code1 =='AAAA' & Code2='XDC' & Code3 > 10.00 & Code3 <= 14.99 );
%Create new folder or use existent
cc = fullfile(pwd, [Code1 '_' Code2 '_Land10-15m']); if ~exist(cc, 'dir'); mkdir(cc); end
%Create and copy file that follow statement
FileIwant{count}=FileTemp;
copyfile(char(FileIwant), cc);
else
if (Code1 =='AAAA' & Code2='XDC' & Code3 > 15.00 & Code3 <= 20.00);
%Create new folder or use existent
dd = fullfile(pwd, [Code1 '_' Code2 '_Land15-20m']); if ~exist(dd, 'dir'); mkdir(dd); end
%Create and copy file that follow statement
FileIwant{count}=FileTemp;
copyfile(char(FileIwant), dd);
%After check that filename has the 4 letters (e.g. AAAA) and the 3 letter %(e.g. VXC) and is in the numerical range between (e.g. 0 to 4.99), then the loop should create a folder (if this doesn’t exist already) and then copy the files that follow the statements. Then move to next statement.
%Then I need to continue with ‘else’ for all other Code1 and Code2 combinations.
count=count+1;
end
Thanks for your suggestions!
  2 件のコメント
Stephen23
Stephen23 2018 年 2 月 17 日
編集済み: Stephen23 2018 年 2 月 17 日
Do not use == for comparing character vectors. Use strcmp, strcmpi, strncmp, etc:
if strcmp(Code1,'AAAA') && strcmp(Code2,'VXC') && Code3>0 && Code3<=4.99
You should also use the short-circuit operator &&, as I showed above. Using == and & will create a logical vector, which causes a lots of confusion for beginners when they try to use that vector with if (we regularly get questions on this because they misunderstand what it is doing). It is simpler and more reliable to use strcmp and && to ensure that you are working with a logical scalar.
Robert
Robert 2018 年 2 月 17 日
Thank you so much, very appreciated!

サインインしてコメントする。

採用された回答

Greg
Greg 2018 年 2 月 17 日
You're mixing U.S. and European decimal separators in the if statement. In the first, you use period (0.00) then you switch to comma (4,99). Also, str2double is better here than str2num.
Finally, the following bit makes no sense:
FileIwant{count}=FileTemp;
copyfile(char(FileIwant), dd);
If you're trying to store all files for a later list, but only copy one at a time, you need to index inside copyfile:
FileIwant{count}=FileTemp;
copyfile(FileIwant{count},dd);
If you aren't keeping the list:
copyfile(FileTemp,dd);
  2 件のコメント
Robert
Robert 2018 年 2 月 17 日
Thank you so much, very appreciated!
Greg
Greg 2018 年 2 月 17 日
Happy to help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by