Is there any code that can detect a file with certain size and replace it with another one?
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I am now post-processing my analysis data. I noticed that some files do not have the targeted data, as the analysis stopped prematurely.
Hence, I need to replace these files with a file that contains a fixed value.
The files to be found and replaced have a size that is usually below 10 KB.
Is there any available code that can scan files in a folder and detect such files and replace them?
I hope I could explain my problem clearly, and please feel free to let me know if I should elaborate my question further.
2 个评论
回答(1 个)
Walter Roberson
2019-4-7
projectdir = pwd(); %adjust to the appropriate directory
ext = '.xyz'; %adjust to extension being looked for
too_small_size = 10*1024;
replacement_file = fullfile(projectdir, 'flag_bad_file.txt'); %if it is in the same directory then avoid using same extension
dinfo = dir( fullfile(projectdir, ['*' ext]);
mask = [dinfo.bytes < too_small_size];
files_to_replace = fullfile( projectdir, {dinfo(mask).name} );
for K = 1 : length(file_to_replace)
copyfile(replacement_file, files_to_replace{K});
end
5 个评论
Walter Roberson
2019-4-7
projectdir = 'Put/In/The/Full/Path/To/The/Directory/At/This/Point';
replacement_file = 'Put/In/The/Full/Path/To/The/Replacement/File/At/This/Point/Including.extension';
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!