# MATLAB for File Management5

Posted by Jiro Doke,

Jiro's pick this week is a set of functions for operating on files and folders by Frederic Moisy.

I use MATLAB for analyzing data and creating tools for data analysis. But I like MATLAB so much that I use it for things other than regular work stuff. That's why when I came across Frederic's entry, it made me smile. This would certainly help me manage my files on my computer. With his collection of files, you can easily navigate to folders, manage folders, and view/manipulate file names.

To get the file names of my image files in "image1" and "image2",

rdir('image*/*.jpg')
    'image1\im001.jpg'
'image1\im002.jpg'
'image1\im003.jpg'
'image1\im004.jpg'
'image1\im005.jpg'
'image1\im006.jpg'
'image2\im018.jpg'
'image2\im019.jpg'
'image2\im020.jpg'
'image2\im021.jpg'
'image2\im022.jpg'


To rename my image files by replacing certain text patterns,

cd image1
renamefile('*.jpg', 'im', 'newIm');
cd ../image2
renamefile('*.jpg', 'im', 'oldIm');
cd ..
rdir('image*/*.jpg')
    'image1\newIm001.jpg'
'image1\newIm002.jpg'
'image1\newIm003.jpg'
'image1\newIm004.jpg'
'image1\newIm005.jpg'
'image1\newIm006.jpg'
'image2\oldIm018.jpg'
'image2\oldIm019.jpg'
'image2\oldIm020.jpg'
'image2\oldIm021.jpg'
'image2\oldIm022.jpg'


One suggestion I have is for "renamefile" to work with multiple folders at once. But it's pretty useful as it is!

Let us know what you think here.

Get the MATLAB code

Published with MATLAB® 7.10

KE replied on : 1 of 5

Very helpful. Would be nice if Matlab itself integrated more file / folder management tools (perl?)

Clayton Chu replied on : 2 of 5

Couldn’t you just use the ! command to perform a command in a system shell and get the exact same result?

jiro replied on : 3 of 5

@Clayton,

Perhaps! I guess I don’t know all the DOS/System commands to do file manipulation.

Bob replied on : 4 of 5

I don’t think DOS allows wildcard usage as shown in Jiro’s example, and the OS commands for file mangement differ between PC and Unix. This submission does add convenience.

I find MATLAB’s file operations so platform agnostic that I have been known to do shell scripting in MATLAB to get cross platform compatibility.

MATLAB does support Perl BTW.

help perl


Hi,

I found a bug when the files have no extention and no extention is provided. This happens at line 87. I replaced the original code:
<pre
D(ii).name = [prepath,D(ii).name];

by
<pre
D(ii).name = fullfile(prepath,postpath,D(ii).name);

Otherwise great code and very fast!

MRB

These postings are the author's and don't necessarily represent the opinions of MathWorks.