rename txt file within a specific folder
3 次查看(过去 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
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.
采纳的回答
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
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?
0 个评论
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
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!