use dos function to Rename files
显示 更早的评论
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 个评论
Image Analyst
2015-9-19
movefile() should not take forever. Did you actually even try it?
Star Strider
2015-9-19
I didn’t look at the movefile code, but since it has several options and is platform-independent, it may first have to determine the OS it’s using, and then branch to the correct section of its code for that OS and particular argument set with each call to it. A direct dos call may be more efficient for large numbers of files.
Ken Atwell
2015-9-20
I'll second Image Analyst; I would at least try movefile before dismissing it as performance-inefficient.
For starters, dos will start a subprocess and run Windows' CMD command. For 10,000 files, that would take a very long time. movefile has the opportunity to do this without a subprocess -- though I must admit I don't know how movefile is implemented.
If you must absolutely go the dos route, the cost of starting 10,000 processes is likely to be punitive. Instead, create a batch (.BAT) file that contains the 10K rename commands, and then invoke dos once to run the batch file.
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
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
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
2015-9-20
If you get rid of the "" then how will it deal with filenames that include spaces in them?
Star Strider
2015-9-20
编辑:per isakson
2015-10-7
Excellent point, and one I searched the help on rename for without success. I couldn’t find anything in the documentation, so I experimented and discovered that putting double quotes inside the single quotes will accommodate spaces in the filename. Single quotes around the each string (in this code, using sprintf) are otherwise all that’s necessary:
fidt = fopen('abc.txt','wt'); % Create Demonstration File
fclose(fidt); % Close It
imhere1 = which('abc.txt') % Verify File Exists
%
oldName = 'abc.txt'; % Without Spaces Within File Name, Only Single Quotes Are Necessary
%
newName = '"a - b - c.txt"'; % With Spaces Within The File Name, Put Double Quotes Inside Single Quotes
[status,cmdout] = dos(sprintf('rename %s %s', oldName, newName)) % Rename File
%
imhere2 = which('a - b - c.txt') % Verify ‘rename’ Call Worked
Tested this. Success!
Anyway, the Question was how to do this in DOS, and this works.
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
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.
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
2015-9-21
0 个投票
4 个评论
Star Strider
2015-9-21
Did you use my dos code? Did it do what you wanted it to?
Jan
2015-9-22
Matlab 6.5 had a cruel implementation of MOVEFILE. The mentioned FEX submission was 1600 times faster. But Matlab improved the code and it was much faster in R2009a, such that the C-Mex was only 10 to 50 time faster. Since 2011b the speedup is only a factor of 5 to 6. I do not have a newer version yet, so I could not test the current relation.
NangHa
2015-10-7
Star Strider
2015-10-7
My pleasure.
I am happy it worked for you.
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!