File Exchange

image thumbnail

easyrename

version 1.3.1.0 (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 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:
IL_test2.s4p
FEXT_Test4_try2.s4p
NEXT_try3_TEST6.s4p
(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#...")
Test2_IL.s4p
Test4_FEXT_try2.s4p
Test6_NEXT_try3.s4p
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'
easyrename(src,dest);
DONE !
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
-------------------------------------------------------------------------
EXAMPLES AND MOST COMMON USES OF EASYRENAME:
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
wildcard
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

-------------------------------------------------------------------------
QUESTIONS, COMMENTS, FEEDBACK
Michael Rowlands - v1.0 2017-06-28
Engineering in the 21st century; Make It Easy !
easineering@gmail.com
-------------------------------------------------------------------------

Cite As

Michael Rowlands (2020). easyrename (https://www.mathworks.com/matlabcentral/fileexchange/63586-easyrename), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (3)

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.

Some notes:

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

https://se.mathworks.com/help/matlab/matlab_prog/preallocate-memory-for-a-cell-array.html

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:

https://se.mathworks.com/help/matlab/ref/lookfor.html

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.

Updates

1.3.1.0

Support ? and * wildcards in directories and extensions

1.3.0.0

Added support for * and ? wildcards in directory names

1.2.0.0

Added support for large numbers of wildcards

1.1.0.0

Added support for searching extensions using wildcards

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

Inspired by: easycopy