필터 지우기
필터 지우기

rename txt file within a specific folder

조회 수: 4 (최근 30일)
Alberto Acri
Alberto Acri 2023년 11월 3일
댓글: Alberto Acri 2023년 11월 10일
I need to rename a specific txt file ('file_name') within a folder. How is this done?
folder = dir('C:\...\...\...\*.txt');
% file_name = 'test.txt';
final_file_name = 'analysis.txt';
for id = 1
[~, f,ext] = fileparts(folder(id).name);
rename = strcat(final_file_name,ext) ;
movefile(folder(id).name, rename);
end
  댓글 수: 2
MarKf
MarKf 2023년 11월 4일
This might be the simplest one you've asked so far @Alberto Acri and you asked something very similar before. You can also find answers to similar questions other people have posted. You could learn the basics of Matlab programming on this website, search it (or the web at large) for similar solutions and try to figure it out things on your own more often, I think it might help you save time for your projects and eventually learn faster how to program. Instead of asking something like a question a day, one every 5 days on average since 2020 (I guess it goes by project/period).
You aren't specifically going against guidelines, maybe you do not wish to learn by yourself because it seems overwhelming or it is a side hobby, afterall you do provide the opportunity for others online coming across and searching your same specific issue to have it answered and the opportunity for this community to provide answers and have them accepted, but I felt maybe I should discretely make a comment about it after being surprised by the sheer amount. It's indeed better to have a member like you than the one-shot ungrateful vast majority here, and if I'm overstepping I apologize that in case.
Alberto Acri
Alberto Acri 2023년 11월 10일
Hi @MarKf. I generally look for the solution to my problem on the Internet. I don't always find the right solution. Using Matlab as a hobby (and not having extensive knowledge about Matlab and the various operations that can be done) I think it is normal to ask some questions. Some, like this one, may be more basic and that I actually could have avoided.

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

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 11월 3일
If you want to rename a specific file only -
%% Fullpath of the file
%example
fn = 'C:\Users\Hp\Downloads\test.txt'
%Get the path name
fpath = fileparts(fn);
current_file_name = 'test.txt';
final_file_name = 'analysis.txt';
movefile(fullfile(fpath, '\', current_file_name), fullfile(fpath, '\', final_file_name))
In case you can't write to the folder, use
movefile(fullfile(fpath, '\', current_file_name), fullfile(fpath, '\', final_file_name), 'f')
  댓글 수: 1
Voss
Voss 2023년 11월 3일
The '\' arguments to fullfile are unnecessary.

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

추가 답변 (2개)

Jon
Jon 2023년 11월 3일
편집: Jon 2023년 11월 3일
I'm not sure what you are doing with all of the looping through directories.
If you just want to change the name of one file, and you know the name of the file that you want to rename, say it is called myfile.txt and you know the new name, for example, mynewfile.txt you can just use
movefile('myfile.txt','mynewfile.txt')
Is there something I am not understanding about what it is you are trying to achieve?

Voss
Voss 2023년 11월 3일
folder = dir('C:\...\...\...\*.txt');
% file_name = 'test.txt';
final_file_name = 'analysis.txt';
for id = 1
p = folder(id).folder;
old_name = fullfile(p,folder(id).name);
new_name = fullfile(p,final_file_name); % final_file_name has the extension
movefile(old_name, new_name);
end

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by