Want to rename MF4 files

3 次查看(过去 30 天)
Sneha Patil
Sneha Patil 2023-2-3
回答: Dinesh 2023-2-7
I have MF4 files with same nomenclature but in different folders for a particular day. As an example, I have attached the files for refernce.
I need to rename those MF4 file with modified date and stamp when it was created by datalogger.

回答(1 个)

Dinesh
Dinesh 2023-2-7
Hi Sneha,
As I understand the question, there are subfolders within the folder which have a single (or multiple) .MF4 files in them. The names of those files (not the folders) have to be changed to the modified timestamp.
The following code changes the names of all the files in the folder structure that you have sent to the modified unix timestamp of that particular file.
root = 'C:\path\to\root\folder'; % specify the path to the root folder
directories = dir(root);
% As in the image sent by you, there are several directories and each of
% them have a file whose name has to be changed. So, looping through those
% directories here.
for i = 1:length(directories)
dirName = directories(i).name;
if strcmp(dirName, '.') || strcmp(dirName, '..') % The '.' and '..' directories are to be omitted because they represent the current and previous directories
continue;
end
fullPath = fullfile(root, dirName);
files = dir(fullPath);
for j = 1:length(files)
file = files(j).name;
if strcmp(file, '.') || strcmp(file, '..') % These files are to be skipped even inside the directory as they are present in every level
continue;
end
filePath = fullfile(fullPath, file);
fileInfo = dir(filePath);
unixTime = (fileInfo.datenum - datenum(1970,1,1)) * 24 * 60 * 60; % convert to Unix time
newFileName = [num2str(unixTime), '.MF4']; % new file name has the unix timestamp of modification as its name
newFilePath = fullfile(fullPath, newFileName); % new file path that includes the modified file name
movefile(filePath, newFilePath); % changing the file path to include the new file name
end
end
As mentioned, I've used the Unix time stamp that includes information on both time and date. The format of representation can be changed as required.

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by