Hello,
I have a question. I would like to change the extension of several files. I mean that in one directory a I have many files in type file1.out., file2.out ,..., file600.out.
I would like to rename these files to *.txt (I mean file1.txt, file2.txt,...., file600.txt) in this directory with one command via matlab?
Could anyone help me?

 채택된 답변

Adam Danz
Adam Danz 2020년 4월 2일
편집: Adam Danz 2021년 1월 27일

7 개 추천

This shows how to
  1. List all files in a given directory with the extension .out
  2. Copy the file and rename the extention .txt
  3. Delete the .out file (commented out)
% Enter the directory to search
directory = 'C:\Users\name\Documents\MATLAB';
% List all .out files
fileList = dir([directory, '\*.out']);
% Loop through each .out file, copy it and give new extension: .txt
for i = 1:numel(fileList)
file = fullfile(directory, fileList(i).name);
[tempDir, tempFile] = fileparts(file);
status = copyfile(file, fullfile(tempDir, [tempFile, '.txt']))
% Delete the .out file; but don't do this until you've backed up your data!!
% delete(file)
end
*Not tested

댓글 수: 5

Ivan Mich
Ivan Mich 2020년 4월 2일
편집: Adam Danz 2020년 4월 6일
Excuse me, I have tried your solution but command window shows me :
Error in (line 11)
for i = 1:numel(fileLIst)
Do you know why?
Adam Danz
Adam Danz 2020년 4월 2일
편집: Adam Danz 2020년 4월 3일
Whenever you share an error message, please share the entire copy-pasted error message.
Note the typo in my answer (which I'll fix now). In that line, the I is capitalized (fileLIst) and it shouldn't be.
Also, whenever you're trying new code, take time to look at it and understand what each line is doing, especially for really short pieces of code like the one in my answer. Also take time to think about what the error message is telling you. In this case, the error message likely indicated that the variable fileLIst didn't exist and you could have caught the typo.
Pedro Barba
Pedro Barba 2021년 1월 8일
Shouldn't it be "fileList(i).name" instead of "fileList(1).name" ?
Adam Danz
Adam Danz 2021년 1월 27일
Thanks Pedro, fixed.
Emmagnio Desir
Emmagnio Desir 2023년 4월 1일
thank you, working for me

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

추가 답변 (2개)

Femke Cappon
Femke Cappon 2021년 1월 27일

1 개 추천

Thank you, I have used to code to rewrite LabVIEW .lvm files to .txt
Musaddiq Al Ali
Musaddiq Al Ali 2020년 12월 6일
편집: Musaddiq Al Ali 2020년 12월 6일

0 개 추천

Mr. Adam Danz's code is vey nice and neat. I like it. I would like to suggest, to add '\' to the secound lines of the code:-
fileList = dir([directory,'\','*.out']);

댓글 수: 2

Even better would be to use the correct function for the job:
fileList = dir(fullfile(directory,'*.out'));
Adam Danz
Adam Danz 2020년 12월 6일
thanks, corrected.

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

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

질문:

2020년 4월 2일

댓글:

2023년 4월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by