Need an Elegant Sorting Solution for Directory File Names
    5 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
I have a directory of filenames, (within a files structure array) structured like this:
cdef2.20140101.pdef.txt
cdef2.20140102.pdef.txt
....
pdef.gdef.201300101-20130105.txt
pdef.gdef.201300106-20130110.txt
....
I need to reorder these (ascending order from oldest to newest) according to the dates embedded within these file names which are formatted like YYYYMMDD. I've explored several options, but I'm seeking an elegant solution without do loops. Any tips or suggestions would be appreciated.
3 Commenti
  Sandeep Mishra
      
 il 9 Lug 2023
				Can we assume that there exist only two type of files?
One which starts with cdef2 and one which starts with pdef? 
Risposta accettata
  Voss
      
      
 il 9 Lug 2023
        
      Modificato: Voss
      
      
 il 9 Lug 2023
  
      filenames = { ...
    'cdef2.20140101.pdef.txt'; ...
    'cdef2.20140102.pdef.txt'; ...
    'pdef.gdef.20130101-20130105.txt'; ...
    'pdef.gdef.20130106-20130110.txt'}
dates = regexp(filenames,'\d{8}','match','once');
[~,idx] = sort(dates);
sorted_filenames = filenames(idx)
0 Commenti
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su File Operations in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!