Wildcard '*' not working for "isfile" or renaming using "move file" commands.
15 views (last 30 days)
Show older comments
Brian Bartlett
on 14 Jun 2021
Commented: Brian Bartlett
on 14 Jun 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!
0 Comments
Accepted Answer
Gatech AE
on 14 Jun 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
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!