필터 지우기
필터 지우기

finding same pattern files from two different folders and copying to other folders

조회 수: 1 (최근 30일)
Hi everyone,
I have two different folders (Folder 1 and Folder 2) that contain netcdf files from two different fields (UV, and ST) with 3hour-internal outputs. Those files are named as yyyyMMddHH patterns. At some steps, some files are missing. Therefore, I need to find files having the same time steps (same yyyyMMddHH) from each folder and copying into other folders (Folder 3 and Folder 4) respectively. An illustration is attached.
Could you please help?
Thanks
  댓글 수: 3
wave_buoys
wave_buoys 2020년 5월 21일
Hi per,
Missing files cannot be determined in Folder 1 and Folder 2 as in each of these folders I have several thousand files. These missing files were due to downloading issues. As you can see files would follow a 3 hour-time interval if the downloading process was ok.
per isakson
per isakson 2020년 5월 21일
편집: per isakson 2020년 5월 21일
Problem is, I fail to deduce the rule that determines which files to copy; the highlighted files. Why should *_2001010100 be copied?

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

채택된 답변

Stephen23
Stephen23 2020년 5월 21일
편집: Stephen23 2020년 5월 21일
Read the folder contents using dir.
Extract the timestamps (e.g. regexp or indexing)
Call intersect on the timestamps, returninng both indices.
Use the indices to copy the files using copyfile in a loop.
Something like this (untested, but should get you started):
D1 = 'absolute/relative path to the 1st folder';
D2 = 'absolute/relative path to the 2nd folder';
S1 = dir(fullfile(D1,'*_*.nc'));
S2 = dir(fullfile(D2,'*_*.nc'));
C1 = {S1.name};
C2 = {S2.name};
T1 = regexp(C1,'\d+','match','once'); % extract timestamps
T2 = regexp(C2,'\d+','match','once'); % extract timestamps
[~,X1,X2] = intersect(T1,T2); % identify common timestamps
D3 = 'absolute/relative path to the 3rd folder';
D4 = 'absolute/relative path to the 4th folder';
for k = 1:numel(X1)
F1 = C1{X1(k)};
F2 = C2{X2(k)};
copyfile(fullfile(D1,F1),D3)
copyfile(fullfile(D2,F2),D4)
end
  댓글 수: 2
wave_buoys
wave_buoys 2020년 5월 21일
Hi Stephan,
The error happened at the intercept function: [~,X1,X2] = intersect(T1,T2);
Error using cell/intersect>cellintersectR2012a (line 280)
Input A of class cell and input B of class cell must be cell arrays of character vectors,
unless one is a character vector.
Error in cell/intersect (line 84)
[varargout{1:nlhs}] = cellintersectR2012a(varargin{:});
Do you know why? thanks
wave_buoys
wave_buoys 2020년 5월 21일
ok.. it worked using your edited codes. Thank you Stephan.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by