Is there any code that can detect a file with certain size and replace it with another one?

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 Comments

Use dir to get the file attributes, compare the file sizes against your limit, then replace those ones using copyfile or any file exporting function of your choice. Most/all of this could be vectorized.
Thank you Stephen for your comment.
Is it too much to ask if it is possible to show that to me in a code?

Sign in to comment.

Answers (1)

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 Comments

Thank you very much Walter for the suggested code. I have modified it to suit my need. However, I keep getting the following error message:
Error: File: FileReplace.m Line: 5 Column: 45
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
The code:
projectdir = pwd(DispOnly); %adjust to the appropriate directory
ext = '.out'; %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
Add another ) to the end of the dir call.
Note: pwd does not accept input arguments. It only reports back what the current directory is.
cd() does accept input arguments. If you use both input and output on cd() then what gets reported is the previous directory that you were in, which is not what you would want in this situation.
Thanks Walter. I actually got another error message:
Undefined function or variable 'DriftOnly2'.
Error in Replace (line 1)
projectdir = pwd(DriftOnly2); %adjust to the appropriate directory
The updated code:
projectdir = pwd(DriftOnly2); %adjust to the appropriate directory
ext = '.out'; %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
Walter, do you mean by "adjust to the appropriate directory" that I need to place the folder name or the location of folder?
Also, I do not see in the code the new file to be printed. Is it the flag_bad_file.txt?
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';

Sign in to comment.

Categories

Asked:

on 7 Apr 2019

Commented:

on 7 Apr 2019

Community Treasure Hunt

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

Start Hunting!