rename txt file within a specific folder
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Alberto Acri
el 3 de Nov. de 2023
Comentada: Alberto Acri
el 10 de Nov. de 2023
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 comentarios
MarKf
el 4 de Nov. de 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.
Respuesta aceptada
Dyuman Joshi
el 3 de Nov. de 2023
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 comentario
Más respuestas (2)
Jon
el 3 de Nov. de 2023
Editada: Jon
el 3 de Nov. de 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
movefile('myfile.txt','mynewfile.txt')
Is there something I am not understanding about what it is you are trying to achieve?
0 comentarios
Voss
el 3 de Nov. de 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);
end
0 comentarios
Ver también
Categorías
Más información sobre Get Started with MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!