How to add extension to files using MATLAB

49 visualizaciones (últimos 30 días)
Masha
Masha el 20 de Jun. de 2023
Comentada: Masha el 22 de Jun. de 2023
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.

Respuesta aceptada

Stephen23
Stephen23 el 21 de Jun. de 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
  2 comentarios
Masha
Masha el 21 de Jun. de 2023
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.
Masha
Masha el 22 de Jun. de 2023
Thank you Stephen.. it worked out

Iniciar sesión para comentar.

Más respuestas (1)

Manas
Manas el 20 de Jun. de 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:
  1 comentario
Masha
Masha el 20 de Jun. de 2023
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.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB Coder en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by