Effacer les filtres
Effacer les filtres

Wildcard '*' not working for "isfile" or renaming using "move file" commands.

17 vues (au cours des 30 derniers jours)
Brian Bartlett
Brian Bartlett le 14 Juin 2021
Commenté : Brian Bartlett le 14 Juin 2021
I am trying to use a for loop to rename multiple netcdf files based on the dates within their filenames ex:
my filenames are "SAMETEXTFORALL_2018MMDD_DifferentTextForAll.nc"
and I need to get them to be "nest_1_2018MMDD000000.nc"
I am trying to use a for loop such as the following for each month:
for x=0:9;
if isfile(['SAMETEXTFORALL_2018010',num2str(x),'_*.nc'])==1;
movefile(['SAMETEXTFORALL_2018010',num2str(x),'_*.nc'], ['nest_1_2018010',num2str(x),'000000.nc']);
else
continue;
end;
end;
but the isfile is returning a 0 for each despite the file existing, and when I do the movefiles individually, they create new folders with the filename, but keep the original filename the same. I think the issue lies within the '*' wildcard, because when I put in the appropriate name for individual files it works. Any help/suggestions? There are too many files to do this on a more individual basis. Thanks!

Réponse acceptée

Gatech AE
Gatech AE le 14 Juin 2021
I frequently need all of the FITS image files from a directory, and so I've gotten used to using the "dir" function. It seems like you could use the wildcard below. "dir" outputs a structure of various information, but we can just get names directly by dot indexing "name." Then you can parse out the MMDD and do everything at once.
filelist = {dir('SAMETEXTFORALL_2018*.nc').name};
for fn = 1:length(filelist)
name = filelist{fn};
ind = strfind(name,'2018');
MMDD = name((ind+4):(ind+7));
modName = ['nest_1_2018' MMDD '000000.nc'];
movefile(name,modName);
end

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by