How to change file date into the folder for yearly

How to change file date into the folder for yearly.
A file sit into the folder need to date modify yearly itself.
any code for this to solve the issue not doing manually yearly.
Thanks

2 Comments

Do I understand that you want to examine the file modification date of each file, and split the files into different folders by year of modification?
@Walter Roberson yes correct,
Even though same file must be its date modified can stay into same folder and overwrite with older file.

Sign in to comment.

Answers (1)

inputdir = 'appropriate directory name';
outputbasedir = 'parent folder for split directories';
dinfo = dir(inputdir);
dinfo([dinfo.isdir]) = []; %ignore subfolders and . and .. entries
for K = 1 : length(dinfo)
dv = datevec(dinfo(K).datenum);
yearstr = num2str(dv(1));
outfolder = fullfile(outputbasedir, yearstr);
if ~exist(outfolder, 'dir'); makedir(outfolder); end
sourcefile = fullfile(dinfo(K).folder, dinfo(K).name);
copyfile(sourcefile, outfolder);
end
If you want to move the files instead of copying them then change copyfile() to movefile()
If you want to process all immediate subdirectories of a parent directory then use
inputdir = 'appropriate directory name/*/*';

Categories

Tags

Asked:

Nav
on 17 Sep 2022

Answered:

on 17 Sep 2022

Community Treasure Hunt

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

Start Hunting!