-
Notifications
You must be signed in to change notification settings - Fork 54
/
checkFileExistence.html
152 lines (140 loc) · 8.59 KB
/
checkFileExistence.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>Description of checkFileExistence</title>
<meta name="keywords" content="checkFileExistence">
<meta name="description" content="checkFileExistence">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="generator" content="m2html v1.5 © 2003-2005 Guillaume Flandin">
<meta name="robots" content="index, follow">
<link type="text/css" rel="stylesheet" href="../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../index.html">Home</a> > <a href="index.html">io</a> > checkFileExistence.m</div>
<!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png"> Master index</a></td>
<td align="right"><a href="index.html">Index for io <img alt=">" border="0" src="../right.png"></a></td></tr></table>-->
<h1>checkFileExistence
</h1>
<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>checkFileExistence</strong></div>
<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>function files=checkFileExistence(files,fullOrTemp,allowSpace,checkExist) </strong></div>
<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre class="comment"> checkFileExistence
Check whether files exist. If no full path is given a file should be
located in the current folder, which by default is appended to the
filename.
Input:
files string or cell array of strings with path to file(s) or
path or filename(s)
fullOrTemp 0: do not change path to file(s)
1: return full path to file(s)
2: copy file(s) to system default temporary folder and
return full path
(optional, default 0)
allowSpace logical, whether 'space' character is allowed in the
path (optional, default true)
checkExist logical, whether file existence should really be
checked, as this function can also be used to return
the full path to a new file (optional, default true). Can
only be set to false if fullOrTemp is set to 1.
Output:
files string or cell array of strings with updated paths if
fullOrTemp was set as 1 or 2, otherwise original paths
are returned
Usage: files=checkFileExistence(files,fullOrTemp,allowSpace,checkExist)</pre></div>
<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../matlabicon.gif)">
</ul>
This function is called by:
<ul style="list-style-image:url(../matlabicon.gif)">
<li><a href="exportModel.html" class="code" title="function exportModel(model,fileName,neverPrefix,supressWarnings,sortIds)">exportModel</a> exportModel</li><li><a href="getMD5Hash.html" class="code" title="function md5Hash=getMD5Hash(inputFile,binEnd)">getMD5Hash</a> getMD5Hash</li><li><a href="importModel.html" class="code" title="function model=importModel(fileName,removeExcMets,removePrefix,supressWarnings)">importModel</a> importModel</li></ul>
<!-- crossreference -->
<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function files=checkFileExistence(files,fullOrTemp,allowSpace,checkExist)</a>
0002 <span class="comment">% checkFileExistence</span>
0003 <span class="comment">% Check whether files exist. If no full path is given a file should be</span>
0004 <span class="comment">% located in the current folder, which by default is appended to the</span>
0005 <span class="comment">% filename.</span>
0006 <span class="comment">%</span>
0007 <span class="comment">% Input:</span>
0008 <span class="comment">% files string or cell array of strings with path to file(s) or</span>
0009 <span class="comment">% path or filename(s)</span>
0010 <span class="comment">% fullOrTemp 0: do not change path to file(s)</span>
0011 <span class="comment">% 1: return full path to file(s)</span>
0012 <span class="comment">% 2: copy file(s) to system default temporary folder and</span>
0013 <span class="comment">% return full path</span>
0014 <span class="comment">% (optional, default 0)</span>
0015 <span class="comment">% allowSpace logical, whether 'space' character is allowed in the</span>
0016 <span class="comment">% path (optional, default true)</span>
0017 <span class="comment">% checkExist logical, whether file existence should really be</span>
0018 <span class="comment">% checked, as this function can also be used to return</span>
0019 <span class="comment">% the full path to a new file (optional, default true). Can</span>
0020 <span class="comment">% only be set to false if fullOrTemp is set to 1.</span>
0021 <span class="comment">%</span>
0022 <span class="comment">% Output:</span>
0023 <span class="comment">% files string or cell array of strings with updated paths if</span>
0024 <span class="comment">% fullOrTemp was set as 1 or 2, otherwise original paths</span>
0025 <span class="comment">% are returned</span>
0026 <span class="comment">%</span>
0027 <span class="comment">% Usage: files=checkFileExistence(files,fullOrTemp,allowSpace,checkExist)</span>
0028
0029 <span class="keyword">if</span> nargin<2
0030 fullOrTemp = 0;
0031 <span class="keyword">end</span>
0032 <span class="keyword">if</span> nargin<3
0033 allowSpace = true;
0034 <span class="keyword">end</span>
0035 <span class="keyword">if</span> nargin<4
0036 checkExist = true;
0037 <span class="keyword">end</span>
0038 files=convertCharArray(files);
0039 <span class="keyword">if</span> numel(files)==1
0040 oneFile=true;
0041 <span class="keyword">else</span>
0042 oneFile=false;
0043 <span class="keyword">end</span>
0044 filesOriginal = files;
0045
0046 <span class="comment">%Make all full paths before check of file existence</span>
0047 <span class="keyword">if</span> ispc <span class="comment">% full path starts like "C:\"</span>
0048 inCurrDir = cellfun(@isempty,regexpi(files,<span class="string">'^[a-z]\:\\'</span>));
0049 <span class="keyword">else</span> <span class="comment">%isunix full path starts like "/"</span>
0050 inCurrDir = cellfun(@isempty,regexpi(files,<span class="string">'^\/'</span>));
0051 <span class="keyword">end</span>
0052 files(inCurrDir) = fullfile(cd,files(inCurrDir));
0053
0054 <span class="comment">%Check existence</span>
0055 <span class="keyword">if</span> checkExist
0056 <span class="keyword">for</span> i=1:numel(files)
0057 <span class="keyword">if</span> ~isfile(files{i})
0058 error(<span class="string">'File "%s" cannot be found\n'</span>,files{i});
0059 <span class="keyword">elseif</span> allowSpace == false & strfind(files{i},<span class="string">' '</span>)
0060 error(<span class="string">'File "%s" has an invalid space in the filename or path, please remove this before running this function\n'</span>,files{i});
0061 <span class="keyword">end</span>
0062 <span class="keyword">end</span>
0063 <span class="keyword">end</span>
0064
0065 <span class="keyword">switch</span> fullOrTemp
0066 <span class="keyword">case</span> 0
0067 files = filesOriginal;
0068 <span class="keyword">case</span> 1
0069 <span class="comment">% files already contains full path</span>
0070 <span class="keyword">case</span> 2
0071 <span class="keyword">for</span> i=1:numel(files)
0072 tmpFile=tempname;
0073 copyfile(files{i},tmpFile);
0074 files{i}=tmpFile;
0075 <span class="keyword">end</span>
0076 <span class="keyword">end</span>
0077
0078 <span class="keyword">if</span> oneFile == true
0079 files = files{1};
0080 <span class="keyword">end</span></pre></div>
<hr><address>Generated by <strong><a href="http://www.artefact.tk/software/matlab/m2html/" title="Matlab Documentation in HTML">m2html</a></strong> © 2005</address>
</body>
</html>