Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

How do I search a directory for an already existing file name and if it is present, save a new file name the same as the old one but with an iteration number at the end of it?

1 vue (au cours des 30 derniers jours)
Hello,
I am wondering if anybody could help me out with my problem? I am trying to search a directory for an existing file name and if it exits, save a new file with the existing file name but with an iteration number after it. For example I have a file called 'processed_results_Br0' and I want to save a new file as 'processed_results_Br0_RUN1' if the file exists. I have written the following code in an attempt to implement what I am describing:
dir_string = 'C:\Ripple\';
mkdir (dir_string);
output_file_name = ['processed_results_' name_str];
filesAndFolders = dir(dir_string);
for i=1:length(filesAndFolders)
if ~isempty(strfind(filesAndFolders(i).name,output_file_name))
run_index = strfind(filesAndFolders(i).name,'_RUN');
if ~isempty(run_index) && (filesAndFolders(i).name(end) ~= 'N')
run_iteration = i
else
display('Test');
run_iteration = filesAndFolders(i).name(run_index+4);
end
end
end

Réponses (2)

Fangjun Jiang
Fangjun Jiang le 24 Sep 2018
How about use some of the functions?
fullfile()
exist(..,'file')
copyfile()

Bish Erbas
Bish Erbas le 24 Sep 2018
Modifié(e) : Bish Erbas le 24 Sep 2018
curDir = pwd;
files = dir(curDir);
files = files(~ismember({files.name},{'.','..'}));
fileToSearch = 'processed_results.dat';
for k = 1:length({files.name})
idx = cellfun(@ (x) strcmp(x,fileToSearch), {files.name});
myFullFile = fullfile(curDir,files(idx).name);
copyfile(myFullFile,[myFullFile '_RUN1']);
end

Cette question est clôturée.

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by