rename txt file within a specific folder

2 次查看(过去 30 天)
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')

更多回答(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