Renaming files using MATLAB
48 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MathWorks Support Team
le 28 Juil 2020
Modifié(e) : MathWorks Support Team
le 3 Fév 2025
How can I rename files on my computer using MATLAB?
I have many files on my computer and want to use MATLAB to rename them all at once.
Réponse acceptée
MathWorks Support Team
le 17 Jan 2025
Modifié(e) : MathWorks Support Team
le 3 Fév 2025
From MATLAB, you can use the "movefile" function to rename files on your machine. To access the specific documentation for this function in MATLAB R2020a, please run the following command in the command window:
>> web(fullfile(docroot, 'matlab/ref/movefile.html'))
For more information on managing files and folders in MATLAB, refer to the documentation by executing the following command in MATLAB R2020a:
>> web(fullfile(docroot, 'matlab/matlab_env/manage-files-and-folders.html'))
Alternatively, you can use the "system" command which executes on the local operating system.
On Windows using CMD, the "rename" command works like this:
C:> rename file_path new_name
C:> rename C:\Temp\file1.txt file2.txt
So in MATLAB, from the directory with the file you could execute:
>> system("rename " + "old_name.txt" + " " + "new_name.txt")
If the file names contain spaces, this can confuse the operating system, so a more robust approach is to wrap each file name in double quotes like this:
>> system("rename " + '"' + "old name.txt" + '" "' + "new name.txt" + '"')
This produces a system command like this:
C:> rename "old name.txt" "new name.txt"
To use MATLAB variables for the old and new file names you can use these MATLAB commands:
>> old_path_to_file = "C:\Temp\test a.txt";
>> new_filename = "test b.txt";
>> cmd = "rename " + '"' + old_path_to_file + '" "' + new_filename + '"';
>> system(cmd);
On a Linux or macOS (Unix) system, this would be:
>> old_path_to_file = "/tmp/test a.txt";
>> new_filename = "test b.txt";
>> cmd = "mv " + '"' + old_path_to_file + '" "' + new_filename + '"';
>> system(cmd);
Please follow the below link to search for the required information regarding the current release:
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Startup and Shutdown dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!