Skip to content

Commit

Permalink
Uploaded file renaming script and library
Browse files Browse the repository at this point in the history
  • Loading branch information
jengriesh committed Mar 13, 2024
1 parent 4f043b6 commit 049f09f
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 0 deletions.
101 changes: 101 additions & 0 deletions File_Renaming_Current.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
%% Replace a substring in all files in a directory or all subdirectories
% Created - Alex Salmon - 2019.02.27
% Not yet approved for AOIP use. Last validated: yyyy.mm.dd

%% Imports
addpath('.\lib');

%% Get user input
prompt = {'Desired directory path:',...
'Old string:',...
'New string:',...
'Search subfolders? (y/n)'};
dialog_name = 'Replace a substring in all files';
defaults = {'','','','n'};
opts.Resize = 'on';

%% Loop until input is valid
input_valid = false;
while ~input_valid
answers = inputdlg(prompt, dialog_name, 1, defaults, opts);
if exist('mb', 'var') % Close message box
close(mb);
end
if isempty(answers) % Cancel
return;
end

% Reset error_found
error_found = false;
% Update defaults for quick fixes
defaults = answers;

% unpackaging input data
warning_msg = '';
root_dir = answers{1};
if exist(root_dir, 'dir') == 0
warning_msg = 'Directory not found';
error_found = true;
end
string_to_replace = answers{2};
desired_string = answers{3};
include_subdirs = strtrim(answers{4});

if ~error_found
%% Search for files
search = fullfile(root_dir, sprintf('*%s*', string_to_replace));
if strcmpi(include_subdirs, 'y')
f_list = subdir(search);
% subdir outputs name as the ffname, fix that for the next step
for ii=1:numel(f_list)
[~,name,ext] = fileparts(f_list(ii).name);
f_list(ii).name = [name,ext];
end
elseif strcmpi(include_subdirs, 'n')
f_list = dir(search);
else
warning_msg = 'Input not recognized';
error_found = true;
end
% Check if search comes up empty
if isempty(f_list)
warning_msg = 'No files found with specified substring';
error_found = true;
end
end

%% Validate input
if ~error_found
input_valid = true;
else
mb = msgbox(warning_msg, 'Error', 'Error');
end
end

%% Waitbar
wb = waitbar(0, sprintf('Replacing substring "%s" as "%s"', ...
string_to_replace, desired_string));
wb.Children.Title.Interpreter = 'none';

%% Rename files
for ii=1:numel(f_list)
in_name = f_list(ii).name;
out_name = strrep(in_name, string_to_replace, desired_string);
in_ffname = fullfile(f_list(ii).folder, in_name);
out_ffname = fullfile(f_list(ii).folder, out_name);
% Rename w/ cmd line
eval(sprintf('! ren "%s" "%s"', in_ffname, out_name));
waitbar(ii/numel(f_list), wb)
end

%% Done
close(wb)









Binary file added lib/grid_template/FileNames.xlsx
Binary file not shown.
151 changes: 151 additions & 0 deletions lib/subdir.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
function varargout = subdir(varargin)
%SUBDIR Performs a recursive file search
%
% subdir
% subdir(name)
% files = subdir(...)
%
% This function performs a recursive file search. The input and output
% format is identical to the dir function.
%
% Input variables:
%
% name: pathname or filename for search, can be absolute or relative
% and wildcards (*) are allowed. If ommitted, the files in the
% current working directory and its child folders are returned
%
% Output variables:
%
% files: m x 1 structure with the following fields:
% name: full filename
% date: modification date timestamp
% bytes: number of bytes allocated to the file
% isdir: 1 if name is a directory; 0 if no
%
% Example:
%
% >> a = subdir(fullfile(matlabroot, 'toolbox', 'matlab', '*.mat'))
%
% a =
%
% 67x1 struct array with fields:
% name
% date
% bytes
% isdir
%
% >> a(2)
%
% ans =
%
% name: '/Applications/MATLAB73/toolbox/matlab/audiovideo/chirp.mat'
% date: '14-Mar-2004 07:31:48'
% bytes: 25276
% isdir: 0
%
% See also:
%
% dir

% Copyright 2006 Kelly Kearney


%---------------------------
% Get folder and filter
%---------------------------

narginchk(0,1);
nargoutchk(0,1);

if nargin == 0
folder = pwd;
filter = '*';
else
[folder, name, ext] = fileparts(varargin{1});
if isempty(folder)
folder = pwd;
end
if isempty(ext)
if isdir(fullfile(folder, name))
folder = fullfile(folder, name);
filter = '*';
else
filter = [name ext];
end
else
filter = [name ext];
end
if ~isdir(folder)
error('Folder (%s) not found', folder);
end
end

%---------------------------
% Search all folders
%---------------------------

pathstr = genpath_local(folder);
pathfolders = regexp(pathstr, pathsep, 'split'); % Same as strsplit without the error checking
pathfolders = pathfolders(~cellfun('isempty', pathfolders)); % Remove any empty cells

Files = [];
pathandfilt = fullfile(pathfolders, filter);
for ifolder = 1:length(pathandfilt)
NewFiles = dir(pathandfilt{ifolder});
if ~isempty(NewFiles)
fullnames = cellfun(@(a) fullfile(pathfolders{ifolder}, a), {NewFiles.name}, 'UniformOutput', false);
[NewFiles.name] = deal(fullnames{:});
Files = [Files; NewFiles];
end
end

%---------------------------
% Prune . and ..
%---------------------------

if ~isempty(Files)
[~, ~, tail] = cellfun(@fileparts, {Files(:).name}, 'UniformOutput', false);
dottest = cellfun(@(x) isempty(regexp(x, '\.+(\w+$)', 'once')), tail);
Files(dottest & [Files(:).isdir]) = [];
end

%---------------------------
% Output
%---------------------------

if nargout == 0
if ~isempty(Files)
fprintf('\n');
fprintf('%s\n', Files.name);
fprintf('\n');
end
elseif nargout == 1
varargout{1} = Files;
end


function [p] = genpath_local(d)
% Modified genpath that doesn't ignore:
% - Folders named 'private'
% - MATLAB class folders (folder name starts with '@')
% - MATLAB package folders (folder name starts with '+')

files = dir(d);
if isempty(files)
return
end
p = ''; % Initialize output

% Add d to the path even if it is empty.
p = [p d pathsep];

% Set logical vector for subdirectory entries in d
isdir = logical(cat(1,files.isdir));
dirs = files(isdir); % Select only directory entries from the current listing

for i=1:length(dirs)
dirname = dirs(i).name;
if ~strcmp( dirname,'.') && ~strcmp( dirname,'..')
p = [p genpath(fullfile(d,dirname))]; % Recursive calling of this function.
end
end

0 comments on commit 049f09f

Please sign in to comment.