Renaming files using MATLAB

55 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 17 Gen 2025
Modificato: MathWorks Support Team il 3 Feb 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:

Più risposte (0)

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by