Clear Filters
Clear Filters

rename txt file within a specific folder

4 views (last 30 days)
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);
MarKf on 4 Nov 2023
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 on 10 Nov 2023
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.

Sign in to comment.

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 3 Nov 2023
If you want to rename a specific file only -
%% Fullpath of the file
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')

More Answers (2)

Jon on 3 Nov 2023
Edited: Jon on 3 Nov 2023
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
Is there something I am not understanding about what it is you are trying to achieve?

Voss on 3 Nov 2023
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);




Community Treasure Hunt

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

Start Hunting!