How to add extension to files using MATLAB

조회 수: 51 (최근 30일)
Masha
Masha 2023년 6월 20일
댓글: Masha 2023년 6월 22일
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.
Masha
Masha 2023년 6월 22일
Thank you Stephen.. it worked out

댓글을 달려면 로그인하십시오.

추가 답변 (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