-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoRenameFiles.m
More file actions
166 lines (156 loc) · 5.42 KB
/
autoRenameFiles.m
File metadata and controls
166 lines (156 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
function autoRenameFiles(path)
nameFolder=fullfile(path,'names');
if exist(nameFolder,'file')==7
names=analyzeName(nameFolder,{''});
files=dir(fullfile(path));
%we are only interested in renaming files
files=files(~[files.isdir]);
%now we only keep files with extension we like: nd2|tif|stk
files={files(~cellfun(@isempty,regexpi({files.name},'.*(nd2|tif|stk)'))).name};
[~, fileNames, exts]=cellfun(@fileparts,files,'UniformOutput',false);
%if there are numbers at the end of the filenames, copy them over.
fileNumbers=repmat({''},1,length(names));
[startInd, endInd]=regexp(fileNames,'\d+$');
for n=1:length(fileNames)
fileNumbers{n}=fileNames{n}(startInd{n}:endInd{n});
end
try
names=strcat(names,fileNumbers,exts);
catch ME
if strcmp(ME.identifier,'MATLAB:strcat:InvalidInputSize')
error('MATLAB:autoRenameFiles:InvalidNumberOfFiles',...
'The number of files (%d) does not match the number of new names generated (%d). Check whether you have accounted for all files. aborting.',...
length(files),length(names));
else
rethrow(ME)
end
end
confirmRename(path,files,names);
end
end
function checkNames(files,names)
uniqueNames=unique(names);
if length(names)~=length(uniqueNames)
dup=setdiff(sort(names),uniqueNames);
error('MATLAB:autoRenameFiles:DuplicateNames',...
'Your output structure created duplicate names: %s. aborting!',dup);
end
collision=setdiff(files,names);
if length(collision)~=length(files)
col=setdiff(files,collision);
error('MATLAB:autoRenameFiles:NameCollision',...
'Your output structure created names that already exist: %s. aborting!',[col{:}]);
end
end
function doRename(callerHandle,~,path,files,names)
checkNames(files,names)
undoFile=fullfile(path,'undoRename.mat');
save(undoFile,'files','names');
set(callerHandle,'String','Undo Rename',...
'Callback',{@undoRename,path,files,names});
filesDone={};
namesDone={};
for n=1:length(files)
target=fullfile(path,names{n});
try
if exist(target,'file')
error('MATLAB:autoRenameFiles:TargetFileExists',...
'Output File already exists: %s. aborting',target);
end
movefile(fullfile(path,files{n}),target);
filesDone=[filesDone files(n)]; %#ok<AGROW>
namesDone=[namesDone names(n)]; %#ok<AGROW>
catch ME
save(fullfile(path,'undoPartialRename.mat'),'filesDone','namesDone');
undoRename(callerHandle,[],path,filesDone,namesDone);
set(callerHandle,'String','Rename Now',...
'Callback',{@doRename,path,files,names});
rethrow(ME);
end
end
end
function undoRename(callerHandle,~,path,files,names)
doRename(callerHandle,[],path,names,files);
set(callerHandle,'String','Rename Now',...
'Callback',{@doRename,path,files,names});
end
function closeCallback(~, ~, handle)
close(handle);
end
function confirmRename(path,files,names)
tableHeading={'Current Name -> New Name',''};
tabular=cellfun(@(x) sprintf('%s -> ',x),files,'UniformOutput',false);
tabular=strcat(tabular,names);
table=[tableHeading, tabular];
%textW=max(cellfun(@length,table))*10;
textH=length(table);
%set(0,'units','pixels');
%pixScrS = get(0,'screensize');
%maxWinS=pixScrS*0.9;
fig1=figure('Units','normalized','OuterPosition',[0.05 0.05 0.9 0.9]);
panel1 = uipanel('Parent',fig1,'Position',[0 0.05 1 0.95],...
'Title','This is how the files will be renamed:');
textBox = uicontrol('Parent',panel1,'Style','edit','Units','normalized',...
'Position',[0 0 1 1],...
'FontSize',12,'HorizontalAlignment','left',...
'Max',textH,...
'String', table);
panel3 = uipanel('Parent',fig1,'Position',[0 0 1 0.05]);
okButton=uicontrol(panel3,'Style','pushbutton',...
'Units','normalized','Position',[0.5 0 0.28 1],...
'String','Rename Now',...
'Callback',{@doRename,path,files,names});
undoFile=fullfile(path,'undoRename.mat');
undoPartialFile=fullfile(path,'undoPartialRename.mat');
if exist(undoPartialFile,'file')
load(undoPartialFile);
set(okButton,'String','Undo Partial Rename',...
'Callback',{@undoRename,path,filesDone,namesDone});
elseif exist(undoFile,'file')==2;
load(undoFile);
set(okButton,'String','Undo Rename',...
'Callback',{@undoRename,path,files,names});
end
cancelButton=uicontrol(panel3,'Style','pushbutton',...
'Units','normalized','Position',[0.8 0 0.18 1],...
'String','Cancel',...
'Callback',{@closeCallback,fig1}); %#ok<NASGU>
warning('off','MATLAB:handle_graphics:exceptions:SceneNode');
set(textBox,...
'String',table);
warning('on','MATLAB:handle_graphics:exceptions:SceneNode');
%uiwait(fig1);
end
function string=replaceLatexCharacters(string)
escapeStrings={'\','_','&','$','#'};
replaceStrings={'\textbackslash','\_','\&','\$','\#'};
for n=1:length(escapeStrings)
string=strrep(string,escapeStrings{n},replaceStrings{n});
end
end
function slider_callback1(src,~,arg1)
val = get(src,'Value');
set(arg1,'Position',[0 -val 1 2])
end
function newNames=analyzeName(nameFolder,names)
subFolders=dir(nameFolder);
%first collect all folders
subFolders=subFolders([subFolders.isdir]);
%then remove all hidden folders starting with a dot
subFolders=subFolders(arrayfun(@(x) x.name(1), subFolders) ~= '.');
numFolders=length(subFolders);
if numFolders<1
newNames=names;
else
newNames={};
for n=1:numFolders
split=strsplit(subFolders(n).name,'#');
if length(split)>1
name=strjoin(split(2:end),'#');
else
name=split;
end
newNames=[newNames analyzeName(fullfile(nameFolder, subFolders(n).name),strcat(names,name))]; %#ok<AGROW>
end
end
end