Change file from equipment to .txt files

2 次查看(过去 30 天)
I use equipment that when it saves files it saves them using date codes. For example, .924 or .929 for September 24th and 29th respectively. All they are are txt files and I can make MatLab change them to .txt files but only if I specify that I want a .929 file to change to .txt. How would I code it so that it takes all files and turns them to .txt so that when I have files from a whole week of equipment usage, I can just select a folder and not have to manually change the code for each date?
%Enter the directory to search
directory = uigetdir();
%List all .929 files
fileList = dir([directory, '\*.929');
%Loop through each file, copy it and give new extension: .txt
for i = 1:numel(fileList)
file = fullfile(directory, fileList(i).name);
[tempDir, tempFile] = fileparts(file);
status = copyfile(file, fullfile(tempDir, [tempFile, '.txt']));
end

采纳的回答

Karim
Karim 2022-10-4
This would do the trick, see below for some comments.
% Enter the directory to search
directory = uigetdir();
% List all items in the folder
fileList = dir(directory);
% Delete the subfolders from the list (i.e. only keep files)
fileList(vertcat(fileList.isdir)) = [];
% Loop through each file, copy it and give new extension: .txt
for i = 1:numel(fileList)
file = fullfile(directory, fileList(i).name);
[tempDir, tempFile] = fileparts(file);
status = copyfile(file, fullfile(tempDir, [tempFile, '.txt']));
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Variables 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by