How to add extension to files using MATLAB
51 次查看(过去 30 天)
显示 更早的评论
I have around 60,000 .pdb files like 1UXC, 1UXA, 3FUS and so on.
I need to add an extension of .pdb to each of these files such that it would be 1UXC.pdb , 1UXA.pdb
Can someone help with the code?
I need to load the files from the directory and change their names so that only the extension is appended to its name.
0 个评论
采纳的回答
Stephen23
2023-6-21
According to a comment you made in another thread, the files have no file extension. The first thing is to check if that is really the case, or if they are just not displayed due to MS Windows new default behavior of not showing file extensions.
Once you have confirmed (using some tool) the lack of file extensions, use code something like this. Note that this will add the specified file extension to all files in the given folder: if you need to filter out other files then you will need more complex code.
P = '.'; % absolute or relative path to where the files are saved.
S = dir(fullfile(P,'*'));
S([S.isdir]) = []; % remove directories
for k = 1:numel(S)
old = fullfile(S(k).folder,S(k).name);
new = sprintf('%s.pdb',old);
assert(movefile(old,new),'Failed: %s',old)
end
更多回答(1 个)
Manas
2023-6-20
Hi Masha,
You can try using the following code to add the.pdb extension to each file
% Set the path to the directory where the files are saved
dirpath = 'C:\myFolder';
% Get a list of all files in the directory with the '.pdb' extension
pdbFiles = dir(fullfile(dirpath, '*.pdb'));
% Loop through each file and update its name
for i = 1:numel(pdbFiles)
% Get the file name without the extension
baseName = pdbFiles(i).name(1:end-4);
% Create a new file name with the '.pdb' extension
newName = strcat(baseName, '.pdb');
% Move the file to the new name
movefile(fullfile(dirpath, pdbFiles(i).name), fullfile(dirpath, newName));
end
You can refer to the following documentation:
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!