No BSD License  

Highlights from
REN, a multiple file renaming utility with GUI

image thumbnail

REN, a multiple file renaming utility with GUI

by

 

23 Mar 2009 (Updated )

REN = renaming utility with a GUI. It was inspired by Quan's post on Blinkdagger.com

ren(varargin)
function varargout = ren(varargin)
% REN M-file for ren.fig
%      REN, by itself, creates a new REN or raises the existing
%      singleton*.
%
%      H = REN returns the handle to a new REN or the handle to
%      the existing singleton*.
%
%      REN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in REN.M with the given input arguments.
%
%      REN('Property','Value',...) creates a new REN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ren_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ren_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ren

% Last Modified by GUIDE v2.5 23-Mar-2009 04:07:20

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ren_OpeningFcn, ...
                   'gui_OutputFcn',  @ren_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before ren is made visible.
function ren_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to ren (see VARARGIN)

% Choose default command line output for ren
handles.output = hObject;

% Set initial modes
handles.domode = 1;
handles.withmode = 1;
handles.howmode = 1;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes ren wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = ren_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in fromhere.
function fromhere_Callback(hObject, eventdata, handles)
% hObject    handle to fromhere (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

filters = {'*.m;*.fig;*.mat;*.mdl','MATLAB Files (.m .fig .mat .mdl)';...
    '*.jpg;*.tif;*.png;*.gif','Image Files (.jpg .tif .png .gif)';...
    '*.txt;*.csv;*.dat;*.raw','Text Files (.txt .csv .dat .raw)';...
    '*.doc;*.docx;*.xls;*.ppt;','MS Office Files (.doc .docx .xls .ppt)';...
    '*.sldprt;*.sldasm;*.slddrw','Solidworlks Files (.sldprt .sldasm .slddrw)';...
    '*.*','All Files (*.*)'};    
[handles.files2get handles.path2get] = uigetfile(filters,'Select Files','multiselect','on');
if ~iscell(handles.files2get)
    handles.files2get = {handles.files2get};
end

% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in tohere.
function tohere_Callback(hObject, eventdata, handles)
% hObject    handle to tohere (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

try
    handles.path2put = uigetdir(handles.path2get,'Destination Directory');
catch ME
    handles.path2put = uigetdir('c:\','Destination Directory');
end

% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in gobutton.
function gobutton_Callback(hObject, eventdata, handles)
% hObject    handle to gobutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Quality check inputs first
errmsg = '';
% check for text input if needed
try
    if handles.withmode == 1 && isempty(get(handles.yourtext,'string'))
        errmsg = {'You must first enter some text!'};
    end
catch
    errmsg = {'You must first enter some text!'};
end
% check that some files were selected
try
    if handles.path2get == 0
        if ~iscell(errmsg)
            errmsg = {'You must first pick some files!'};
        else
            errmsg(end+1) = {'You must first pick some files!'};
        end
    end
catch
    if ~iscell(errmsg)
        errmsg = {'You must first pick some files!'};
    else
        errmsg(end+1) = {'You must first pick some files!'};
    end
end
% check that a destination dir was selected
try
    if handles.path2put == 0
        if ~iscell(errmsg)
            errmsg = {'You must first pick a destination directory!'};
        else
            errmsg(end+1) = {'You must first pick a destination directory!'};
        end
    end
catch
    if ~iscell(errmsg)
        errmsg = {'You must first pick a destination directory!'};
    else
        errmsg(end+1) = {'You must first pick a destination directory!'};
    end
end
% if any errors, display dialog and send back to gui
if iscell(errmsg)
    errordlg(errmsg,'createmode','modal');
    return;
end

% build the cell array of strings to add
switch handles.withmode
    case 1
        txt = cell(size(handles.files2get));
        txt(:) = {get(handles.yourtext,'string')};
    case 2
        txt = cell(size(handles.files2get));
        digits = max(3,length(num2str(numel(txt))));
        for i = 1:numel(txt)
            txt(i) = {eval(['sprintf(''%0',num2str(digits),'.0f'',i);'])};
        end
end

% build the cell array of files2put
files2put = handles.files2get;
switch handles.domode
    case 1
        for i = 1:numel(files2put)
            [pathstr, name, ext] = fileparts(files2put{i});
            files2put{i} = [name txt{i} ext];
        end
    case 2
        for i = 1:numel(files2put)
            [pathstr, name, ext] = fileparts(files2put{i});
            files2put{i} = [txt{i} name ext];
        end
    case 3
        for i = 1:numel(files2put)
            [pathstr, name, ext] = fileparts(files2put{i});
            files2put{i} = [name '.' txt{i}];
        end
end

% make the final write to disk
switch handles.howmode
    case 1
        for i = 1:numel(files2put)
            copyfile([handles.path2get handles.files2get{i}],[handles.path2put '\' files2put{i}]);
        end
    case 2
        for i = 1:numel(files2put)
            movefile([handles.path2get handles.files2get{i}],[handles.path2put '\' files2put{i}]);
        end
end

% exit the gui and function
close(gcbf);

function yourtext_Callback(hObject, eventdata, handles)
% hObject    handle to yourtext (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of yourtext as text
%        str2double(get(hObject,'String')) returns contents of yourtext as a double


% --- Executes during object creation, after setting all properties.
function yourtext_CreateFcn(hObject, eventdata, handles)
% hObject    handle to yourtext (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in radio_txt.
function radio_txt_Callback(hObject, eventdata, handles)
% hObject    handle to radio_txt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radio_txt
set(handles.yourtext,'enable','on');
set(handles.radio_suffix,'string','Add a suffix      (filenameYOURTEXT.ext)');
set(handles.radio_prefix,'string','Add a prefix      (YOURTEXTfilename.ext)');
set(handles.radio_ext,'string','Change extension     (filename.YOURTEXT)');
handles.withmode = 1;

% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in radio_num.
function radio_num_Callback(hObject, eventdata, handles)
% hObject    handle to radio_num (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radio_num
set(handles.yourtext,'enable','off');
set(handles.radio_suffix,'string','Add a suffix      (filename###.ext)');
set(handles.radio_prefix,'string','Add a prefix      (###filename.ext)');
set(handles.radio_ext,'string','Change extension     (filename.###)');
handles.withmode = 2;

% Update handles structure
guidata(hObject, handles);


% --- Executes on button press in radio_suffix.
function radio_suffix_Callback(hObject, eventdata, handles)
% hObject    handle to radio_suffix (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radio_suffix
handles.domode = 1;

% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in radio_prefix.
function radio_prefix_Callback(hObject, eventdata, handles)
% hObject    handle to radio_prefix (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radio_prefix
handles.domode = 2;

% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in radio_ext.
function radio_ext_Callback(hObject, eventdata, handles)
% hObject    handle to radio_ext (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radio_ext
handles.domode = 3;

% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in radio_copy.
function radio_copy_Callback(hObject, eventdata, handles)
% hObject    handle to radio_copy (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radio_copy
handles.howmode = 1;

% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in radio_move.
function radio_move_Callback(hObject, eventdata, handles)
% hObject    handle to radio_move (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radio_move
handles.howmode = 2;

% Update handles structure
guidata(hObject, handles);

Contact us