File Exchange

image thumbnail

FileRename

version 1.0.0.0 (8.29 KB) by Jan
Fast renaming of files or folders

13 Downloads

Updated 28 Nov 2010

View License

Rename file or folder

This function renames the existing file or folder specified by the string Source to the name given by the string Dest. This C-Mex is much faster than Matlab's MOVEFILE (timings vary with the size and number of the files due to caching of write operations, see screenshot):
Matlab 2009a: 10 to 50 times faster,
Matlab 6.5: 1600 times faster (!).
This C-Mex is about 50% faster than using java.io.File().renameTo().

[Status, Msg] = FileRename(Source, Dest, [Mode])
INPUT:
Source: String, name of the source file or folder.
Unicode and UNC paths are considered.
Dest: String, name of the destination file or folder.
Mode: String, if 'forced' an existing Dest file is overwritten,
if it is not write protected. Folders are *not* overwritten.
Optional, default: 'DoNotOverwrite'.

OUTPUT:
Status: Scalar DOUBLE. Optional.
0: Success
-1: Source is not existing
-2: Dest is existing already
-3: Dest is write protected
-4: Unknown problems:
Source or Dest is accessed from another program,
Source is a folder and Dest is on another drive.
Msg: String, empty on success, some information in case of problems.

COMPILE: The fast C-Mex file must be compiled before using.
See FileRename.c for details.
Pre-compiled: http://www.n-simon.de/mex

Tested: Matlab 6.5, 7.7, 7.8, WinXP, 32bit
Compiler: LCC2.4 (Matlab2009a), OWC1.8, BCC5.5, MSVC2008
Assumed Compatibility: higher Matlab versions, Mac, Linux, 64bit

Comments and Ratings (15)

Jan

@Andre: Unfortunately I cannot find a solution in the net. The documentation of XCode should reveal the problem. Perhaps removing the leading underscore to these 3 functions work. But I do not have experiences with unicode characters on Macs.

I get these errors on mac (Matlab 2017b)

Error using mex
Undefined symbols for architecture x86_64:
"__waccess", referenced from:
_mexFunction in FileRename.o
"__wremove", referenced from:
_mexFunction in FileRename.o
"__wrename", referenced from:
_mexFunction in FileRename.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Any idea how to fix this?

Jan

@AndyU: After X-code is installed on your computer, Matlab should be able to compile the C-mex file by the "mex" command. Did you try it? Which error messages or problems occur? I do not have a Mac and cannot test this.

AndyU

Any advice on compiling for mac (Matlab 2017a)?

John

I had 2 million files to rename, movefile was going to take so many hours, FileRename did it in minutes. Thank you so much!!!

Harm

Very nice and very fast, thank you!
Would be nice if destination could be a folder name.

Lukas

Nice one, thank you!

Asif ARAIN

Jan

@Magnus: I'm glad that this function is useful. Yes, I have a C-Mex replacement for COPYFILE also. It is about twice as fast. I did not publish it yet: Windows only, no Unicode support. But feel free to contact me by email.

Magnus

This function saved my life (or at least my deadline). Is there a similar implementation of a faster copyfile as well, or is that too much to hope for?

Jan

@Kip: This function renames files from the old to the new name. How could this be "more direct"?
If you have write access to the file, you do not need admin privileges for renaming. Obtaining admin privileges is and must be inconvenient, e.g. this requires a login. Otherwise a virus protection would be impossible.

A tool for renaming files should not perform other complicated tasks. A modular programming style is better. because it is more flexible.

Kip

Besides the obvious of two renamings, can FileRename just change the case in a more direct manner? What about overcoming the need to be 'Administrator'? Thnx

Robert Moss

This worked very well and saved me hours. Why The Mathworks does not fix movefile I do not understand. Thanks also for providing the compiled MEX files on your personal website. You saved me quite a bit of effort.

Bill Wood

Changing from MoveFile to this function sped up my code SIGNIFICANTLY. What used to literally take days now takes hours. Thank you.

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux