Renaming files using MATLAB

55 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2020 年 7 月 28 日
編集済み: MathWorks Support Team 2025 年 2 月 3 日
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.

採用された回答

MathWorks Support Team
MathWorks Support Team 2025 年 1 月 17 日
編集済み: MathWorks Support Team 2025 年 2 月 3 日
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 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by