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.

采纳的回答

Stephen23
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
  2 个评论
Masha
Masha 2023-6-21
Hello Stephen,
Thank you for your answer. Regarding "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" , it is because I was generating .pdb files using matlab using the code:
Protein = '1UXA'
A = getpdb(Protein,"ToFile",string(Protein))
which generated .pdb files without extension.
I should've used the following code to generate files with extension:
Protein = '1UXC'
A = getpdb(Protein,"ToFile",string(Protein)+".pdb")
But I had already generated 60k+ files without the extension. I shall try this code that you had suggested. Thank you.

请先登录,再进行评论。

更多回答(1 个)

Manas
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:
  1 个评论
Masha
Masha 2023-6-20
Thank you for your reply, however, the code didn't work, i am guessing because the initial files considered didn't have any extension at all. Just the file names.
Like 1UXC,1UXA and so on.

请先登录,再进行评论。

类别

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

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by