From 973ccb8ca1e49eabf30b3739c6ee9a3e3125ce6e Mon Sep 17 00:00:00 2001
From: Jan Lemmer <jan.lemmer@fst.tu-darmstadt.de>
Date: Tue, 12 Oct 2021 14:56:38 +0200
Subject: [PATCH] clean commit

---
 +PlotID/createFileCopy.m | 79 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 79 insertions(+)
 create mode 100644 +PlotID/createFileCopy.m

diff --git a/+PlotID/createFileCopy.m b/+PlotID/createFileCopy.m
new file mode 100644
index 0000000..061f109
--- /dev/null
+++ b/+PlotID/createFileCopy.m
@@ -0,0 +1,79 @@
+function [storagePaths] = createFileCopy(filePaths,folderName,storPath,ID,type)
+% Creates a copy of the files (can be used for multiple paths in a cell array)
+% folderName is the name of the exporting folder
+% returns the storage paths were files were stored
+    
+if ~iscell(filePaths)
+    %fixes Issue if Filepath is a char and not a cell array
+    filePaths = {filePaths};
+end
+        
+try 
+    storagePaths = cell(numel(filePaths,1));
+    for i = 1:numel(filePaths)
+        FileNameAndLocation = filePaths{i};
+        [~,name,ext] = fileparts(filePaths{i}); % get the extension
+
+        switch type
+            case 'data'                    
+                  newfile = sprintf([name,ext]); %keep original name
+                  %old behaviour
+                  %sufix = '_data';
+                  %newfile = sprintf([ID, sufix, '_' , num2str(i) ,ext]);
+            case 'dataCentral'
+                %keep original name
+                newfile = sprintf([name,ext]);
+            case 'script'
+                sufix = '_script';
+                newfile = sprintf([ID, sufix ,ext]);
+            case 'userFcn'
+                %keep original name
+                newfile = sprintf([name,ext]);
+            otherwise 
+                error([type,' is not a valid type for createFileCopy'])
+        end %switch 
+
+        RemotePath = fullfile(storPath,folderName, newfile);
+
+%           Check if remote file already exists
+        count = 0;
+        while isfile(RemotePath) && ismember(type,{'data','dataCentral'})
+            % Add a Sufix number to new file name
+            % TODO add more inteligent way then a simple sufix
+            count = count + 1;
+            [~,name,ext] = fileparts(RemotePath);
+            if count < 2
+                RemotePath = fullfile(storPath,folderName,...
+                    [name,'_',num2str(count),ext]);
+            else 
+                RemotePath = fullfile(storPath,folderName,...
+                [name(1:end-length(num2str(count))),num2str(count),ext]);
+            end
+            [~, name, ~] = fileparts(RemotePath);
+
+
+            msg = ['Filename ',name,...
+                ' already exists in the data folder' newline,...
+                ' PlotID will add an suffix if you continue.' newline,...
+                ' This can cause serious confusions.'];
+            warning(msg);                
+            m = input('Do you want to continue, Y/N [Y]:','s');
+
+            if ismember(m,{'N','n'})
+               errorMSG = ['Filename already exists in data folder.' newline,...
+                   ' Rename the File and restart PlotID.'];
+               error();
+            end
+        end
+        copyfile(FileNameAndLocation,RemotePath);
+        storagePaths{i} = RemotePath; 
+    end
+    disp([type, ' sucessfully published']);
+catch
+    warning([type,' export was not sucessful'])
+    if exist('errorMSG')
+        error(errorMSG);
+    end 
+end %try
+end %function
+
-- 
GitLab