How to add extension to files using MATLAB
49 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Stephen23
on 21 Jun 2023
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
More Answers (1)
Manas
on 20 Jun 2023
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:
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!