use dos function to Rename files

1 回表示 (過去 30 日間)
NangHa
NangHa 2015 年 9 月 19 日
編集済み: per isakson 2015 年 10 月 7 日
Please help ... I am not a programming person. My coding skill is very basic.
I have more than 10,000 files that need to be renamed.
The old file name is oldName = 'C:\Users\aaa\bcd.docx.
the new file name is newFile = 'C:\Users\aaa\b - c - d.docx.
If I use movefile function, it would take me forever to complete the task. I am trying to use dos function but was unsuccessful. Here is my dos function:
dos(['rename "' oldName '" "' newName '"']);
and I got the following error: "The syntax of the command is incorrect."
Greatly appreciate the response ...
  5 件のコメント
per isakson
per isakson 2015 年 9 月 20 日
編集済み: per isakson 2015 年 9 月 20 日
Have a look at this function, cmd_rename. It's a wrapper for dos' rename It takes wildcards. However,
cmd_rename( 'h:\m\cssm\tmp', '???.docx', '? - ? - ?.docx' )
gave an unexpected result.
Walter Roberson
Walter Roberson 2015 年 9 月 21 日
With regards to the implementation of movefile: up to R13, movefile and copyfile invoked DOS commands, including the DOS command attrib . The next version of MS Windows that came along moved attrib out of the default path, causing movefile and copyfile to break. Mathworks responded in R14 by switching to internal calls instead of invoking a process.

サインインしてコメントする。

回答 (3 件)

Star Strider
Star Strider 2015 年 9 月 19 日
編集済み: per isakson 2015 年 10 月 7 日
I haven’t used the dos function in a while, but you probably need to get rid of all but the enclosing quotes and definitely get rid of the square brackets, since they work as a concatenation operator and remove the intervening spaces.
This works:
fidt = fopen('abc.txt','wt'); % Create Demonstration File
fclose(fidt); % Close It
imhere1 = which('abc.txt') % Verify File Exists
%
oldName = 'abc.txt';
newName = 'a-b-c.txt';
[status,cmdout] = dos(sprintf('rename %s %s', oldName, newName)) % Rename File
%
imhere2 = which('a-b-c.txt') % Verify ‘rename’ Call Worked
%
imhere1 =
C:\Users\<USERNAME>\Documents\MATLAB\abc.txt
status =
0.0000e+000
cmdout =
''
imhere2 =
C:\Users\<USERNAME>\Documents\MATLAB\a-b-c.txt
(I edited my user name out of the path, but there were no other changes.)
  4 件のコメント
Walter Roberson
Walter Roberson 2015 年 9 月 21 日
Why not just always put the "" in the sprintf() ?
oldName = 'abc.txt';
newName = 'a - b - c.txt';
[status,cmdout] = dos(sprintf('rename "%s" "%s"', oldName, newName));
Star Strider
Star Strider 2015 年 9 月 21 日
Thanks. I thought of that this morning, but was too tired last night to think to include it in the sprintf call.

サインインしてコメントする。


Jan
Jan 2015 年 9 月 21 日
編集済み: Jan 2015 年 9 月 21 日
Try the fast http://www.mathworks.com/matlabcentral/fileexchange/29569-filerename to move files, which is equivalent to a renaming.
R2011b, 800 Files a 100kb:
  • movefile: 5.4 sec
  • FileRename.mexw64: 0.9 sec
  • java.io.File.renameTo: 1.0 sec
So Java is an alternative, if compiled Mex-code is a problem.

NangHa
NangHa 2015 年 9 月 21 日
Thank you all for great responses.
Dozen years ago, I renamed 500 video file, ~200MB each, using the movefile function. The task took me almost three days on the 386 processor machine. I am having a better PC now for the task. However, I am seeking for better solution than movefile since there are more than 10000 video files.
Again, thank you for great help ....
NangHa
  4 件のコメント
NangHa
NangHa 2015 年 10 月 7 日
Star Strider, Thanks for your help. Your dos code is great. It took about 0.6 seconds to rename 45 files.
Again, greatly appreciate your help.
Also, I did try Jan Simon mex code and it is also very good.
Thank you all in response to my request ...
NangHa
Star Strider
Star Strider 2015 年 10 月 7 日
My pleasure.
I am happy it worked for you.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by