File Exchange

image thumbnail


version (25.9 KB) by Michael Rowlands
Convenient way of moving and renaming large groups of files using lists and wildcards.

1 Download

Updated 15 Jul 2017

View Version History

View License

This function was written to solve a particular filename problem, then was expanded to do some
other useful things. I had a bunch of measured files from the lab named:
(Notice the mix of upper and lower case letters, plus the "try.." notes
for some measurements. )
There is a customer-required tool that takes these files and makes a report, and it can only read
files named like this: (Each file must start with "Test#...")
There are hundreds of lab measurements with imperfect labels.
I was looking for an easy way to rename all the files, so I
wrote this function.
src = '\\testlab\product_xyz\data\*test?*.s4p'
dest = \\testlab\product_xyz\data_renamed\*!bTest?*.s4p'
This scenario uses wildcards, plus the "!b" custom code. More on that, later in EXAMPLES and in the ADVANCED USERS section.
This function takes a list of source files and a list of destination
filenames and moves the source to the corresponding destination.
source = single filename or list of files
dest = single filename or list of files
sourcedir = source directory for all the source file list
destdir = destination directory for all the dest file lists
sourcelist_fullpath = { 'c:\users\mjrowlands\desktop\NOTES.txt' 'c:\users\mjrowlands\desktop\.condarc' };
destlist_fullpath = { 'c:\users\mjrowlands\desktop\n2.txt' 'c:\users\mjrowlands\desktop\condy.bat' };
sourcelist_relative = { 'desktop\NOTES.txt' 'desktop\.condarc' };
destlist_relative = { 'desktop\n2.txt' 'desktop\condy.bat' };
source_basedir = 'c:\users\mjrowlands'; for use with sourcelist_relative
dest_basedir = 'c:\users\mjrowlands\desktop2'; for use with destlist_relative
Generally, a blank argument ('' or {} or []) gives the user the typical default settings.
INPUT: easyrename
RESULT: displays these help instructions

INPUT: easyrename(sourcelist_fullpath,destlist_fullpath)
RESULT: This is the most basic form of easyrename.
Takes the sourcelist and moves files 1:1 to the destlist.
Moving c:\users\mjrowlands\desktop\NOTES.txt
To c:\users\mjrowlands\desktop\n2.txt
Moving c:\users\mjrowlands\desktop\.condarc
To c:\users\mjrowlands\desktop\condy.bat

***************** ADVANCED USERS: ****************************
Wildcard Rules:
The * and ? wildcards are supported in the source and dest file lists and behave as the basic windows wildcards.
* = match anything, ? = match any one character
If the source has wildcards, then the dest must be blank, or a single
entry, or contain the same wildcards, or must match the length of the list of found files in the
A user may match a source list of one entry, with a wildcard, with a
multi-entry dest list, with no wildcards, if the dest list length has
exactly the same number of entries that the source list matches.

The source and dest must have the same number of each type of wildcard.
For instance if source is 'c:\users\mjrowlands\desktop\a*abc?*.txt' then
dest must have exactly two * and one ?.
The dest argument also supports !b and !e which force the matching wildcards to the
beginning of the destination string (See the story at the beginning of the help text, to see why this feature is useful)

If there's one argument, the contents of the file will be read and each row of the
file will be run as an easyrename command.
Then the exact source and destination file lists from the columns of
the file, with the first row ignored as a header. Supported formats are .txt,.csv and .xlsx
.txt is read as tab-delimited text
.csv is read as comma delimited
.xlsx is read as standard excel format

Michael Rowlands - v1.0 2017-06-28
Engineering in the 21st century; Make It Easy !

Cite As

Michael Rowlands (2021). easyrename (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (3)

Michael Rowlands

Stephen: Thanks for the comments and suggestions !
You are right that the listout = listin... code that you highlighted, is simjpler than what I used.

Also, I appreciate good processes and workflows, so I see a lot of value in updating the first comment line to have a description of the function with some keywords.

The response to your third note is that I put functions into files for easy abstraction and re-use in other scripts. In order to reduce clutter, I will take your suggestion to put them in a PRIVATE directory.

Stephen Cobeldick

Some notes:

1. The function REMOVEBLANKS is far too complex and it continually changes the cell array size in a while loop:

Much simpler would be:

listout = listin(~cellfun('isempty',listin));

2. Putting a descriptive H1 line would be more useful than simply repeating the function definition:

3. Is there a reason why so many files are required? It seems that many of these could be saved as local functions, or possibly in a PRIVATE directory.

Davi Correia

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

Inspired by: easycopy

Community Treasure Hunt

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

Start Hunting!