Attachment 'netDIO.v1.1.txt'
Download 1 :Namespace netDIO
2
3 ⍝ Disk And File Input/Output Utility Using .Net.
4 ⍝ Check The Comments of Each Methods For More Information.
5 ⍝ Requirements: v12.1 Unicode and .Net 2.0
6 ⍝
7 ⍝ Methods in DIO:
8 ⍝ BFileRead - Reads The Contents of a Binary File Into a Character Vector Without Encoding.
9 ⍝ BFileWrite - Creates a New File, Write The Specified Character Vector to The File Without Encoding.
10 ⍝ DialogFolder - Selects a Directory with FolderBrowserDialog.
11 ⍝ DialogOpenFile - Selects a File to Open With OpenFileDialog.
12 ⍝ DialogSaveFile - Selects a File to Save With SaveFileDialog.
13 ⍝ DirCopy - Copies a Directory And Its Contents. SubDirectories Are Not Copied.
14 ⍝ DirCreate - Creates All Directories And Subdirectories as Specified By Path.
15 ⍝ DirDelete - Deletes The Specified Directory And Any Subdirectories in The Directory.
16 ⍝ DirExists - Determines Whether The Given Path Refers to an Existing Directory on Disk.
17 ⍝ DirMove - Moves a Directory And Its Contents. SubDirectories Are Moved.
18 ⍝ DirRename - Renames a Directory (This Method is Identical to DirMove).
19 ⍝ DirSize - Returns The Size in Bytes of a Directory And It's Subdirectories.
20 ⍝ EncodingDetector - Returns The Encoding of a File And it's Content.
21 ⍝ FileCopy - Copies an Existing File to a New File.
22 ⍝ FileDelete - Deletes a File.
23 ⍝ FileExists - Verify if One or Many File(s) Exists.
24 ⍝ FileMove - Moves a File to a New Location.
25 ⍝ FileRename - Renames a File (This Method is Identical to FileMove).
26 ⍝ FileSize - Returns The Size of a File in Bytes.
27 ⍝ FileVersion - Returns The File Version of a File in Characters.
28 ⍝ GetAttributes - Returns The Attributes of a File in Numeric And Litteral Form.
29 ⍝ GetCreationTime - Returns The Creation Date And Time of a File as a DateTime Object.
30 ⍝ GetDirCurrent - Returns The Current Directory Path.
31 ⍝ GetDirectories - Returns The SubDirectories of a Directory.
32 ⍝ GetDrives - Returns The Names of The Logical Drives on This Computer as "<drive letter>:\".
33 ⍝ GetExtension - Returns The Extension of a Path String.
34 ⍝ GetFiles - Returns The Names of Files in a Directory.
35 ⍝ GetTempFileName - Returns a Uniquely Named Temporary File on Disk With Full Path.
36 ⍝ GetTempPath - Returns The Path of The Current System's Temporary Folder.
37 ⍝ NS2File - Function to save and retrieve a Namespace (NS) under a format compatible with C#.
38 ⍝ SetAttributes - Sets The Attributes of a File.
39 ⍝ SetDirCurrent - Sets The Current Directory Path.
40 ⍝ TFileAppend - Appends The Specified Text to a File Using The Default Windows Code Page.
41 ⍝ TFileRead - Reads The Contents of a Text File Using the Default Windows Code Page.
42 ⍝ TFileWrite - Creates a New File, Writes The Text Using The Default Windows Code Page.
43 ⍝ UFileAppend - Appends The Specified Text to a File Using The UTF-8 Encoding.
44 ⍝ UFileAppendAllLines- Appends lines to a file using UTF8 encoding, and then closes the file.
45 ⍝ UFileRead - Reads The Contents of a Unicode Text File.
46 ⍝ UFileReadAllLines - Opens a text file, reads all lines of the file, and then closes the file.
47 ⍝ UFileWrite - Creates a New File, Writes The Text Using The UTF8 Encoding.
48 ⍝ UFileWriteAllLines - Creates a new file, writes the specified individual lines to the file by using UTF8 encoding.
49 ⍝ ZipCompressFile - Compress a file using Syncfusion Zip namespace.
50
51 ⍝ Version 1.0 February 2015, Pierre Gilbert
52 ⍝
53 ⍝ Version 1.1 August 2015
54 ⍝ Method Added: UFileAppendAllLines
55
56 (⎕IO ⎕ML ⎕WX)←1 3 3
57
58 ∆DBR←{
59 (~(∧\' '=⍵)∨(⌽∧\⌽' '=⍵))/⍵}
60
61 ∆IsString←{
62 0 2∊⍨10|⎕DR ⍵}
63
64 ∇ r←BFileRead file_name;BA;FS;sink;size;⎕USING
65 :Access Public Shared
66 ⍝ Reads The Contents of a Binary File Into a Character Vector Without Encoding.
67 ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name.
68 ⍝
69 ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile
70 ⍝ r = 1 (Bytes as 8 bits characters) (File Name) if Successfull
71 ⍝ r = 0 (Error Description) (File Name) if Failure
72 ⍝
73 ⍝ Example: R ← DIO.BFileRead 'C:\MyBinaryFile'
74 ⍝ Success: R ← 1 (Bytes as 8 bits characters) 'C:\MyBinaryFile'
75 ⍝ Failure: R ← 0 'File Name Does Not Exists' 'C:\MyBinaryFile'
76 ⍝
77 ⍝ Example: R ← DIO.BFileRead '' ⍝ Select the File to Read Using the Open File Dialog
78
79 ⍝ VERIFY IF 'file_name' IS EMPTY OR HAS CHARACTERS
80 :If 0=↑⍴file_name←∊file_name ⍝ Empty ?
81 ⍝ Call the Open File Dialog
82 :If 0=↑⍴file_name←DialogOpenFile
83 ⍝ Failure: No File Name. Exit the program.
84 r←0 'No File Name' '' ⋄ →0
85 :Else
86 ⍝ Do Nothing, 'file_name' is not Empty.
87 :End
88 :ElseIf ~∆IsString file_name
89 ⍝ Failure: 'file_name' Not Characters.
90 r←0 'File Name Must Be Characters'file_name ⋄ →0
91 :Else
92 ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty.
93 :End
94
95 ⍝ MAKE THE CALL
96 :Trap 0
97 ⍝ No Conversion is made with this Call.
98 ⎕USING←',mscorlib.dll'
99 r←1(⎕UCS System.IO.File.ReadAllBytes(⊂,file_name))file_name
100 :Else
101 ⍝ Failure: Unexpected Error While Reading the File
102 r←0 GetLastError file_name
103 :EndTrap
104 ∇
105
106 ∇ r←data BFileWrite file_name;⎕USING
107 :Access Public Shared
108 ⍝ Creates a New File, Write The Specified Character Vector to The File Without Encoding
109 ⍝ And Then Close the File. If the Target File Already Exists, It Is Overwritten.
110 ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name.
111 ⍝
112 ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile
113 ⍝ data = Vector of 8 bits Characters
114 ⍝
115 ⍝ r = 1 (data) (File Name) if Successfull
116 ⍝ r = 0 (Error Description) (File Name) if Failure
117 ⍝
118 ⍝ Example: R ← 'ABCDEF' DIO.BFileWrite 'C:\MyBinaryFile'
119 ⍝ Success: R ← 1 'ABCDEF' 'C:\MyBinaryFile'
120 ⍝ Failure: R ← 0 'File Name Does Not Exists' 'C:\MyBinaryFile'
121 ⍝
122 ⍝ Example: R ← 'ABCDEF' DIO.BFileWrite '' ⍝ Select the File to Write Using the Open File Dialog
123
124 ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTER
125 :If 0=↑⍴file_name←∊file_name ⍝ Empty ?
126 ⍝ Call the Open File Dialog
127 :If 0=↑⍴file_name←DialogOpenFile
128 ⍝ Failure: No File Name. Exit the program.
129 r←0 'No File Name' '' ⋄ →0
130 :Else
131 ⍝ Do Nothing, 'file_name' is not Empty.
132 :End
133 :ElseIf ~∆IsString file_name
134 ⍝ Failure: File Name Not Characters.
135 r←0 'The File Name Must Be Characters'file_name ⋄ →0
136 :Else
137 ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty.
138 :End
139
140 ⍝ VERIFY IF 'data' IS CHARACTER
141 :If ~∆IsString data
142 ⍝ 'data' Must be 8 bits Characters
143 r←0 'The Data Must be 8 bits Characters'file_name ⋄ →0
144 :End
145
146 ⍝ MAKE THE CALL
147 :Trap 0
148 ⎕USING←',mscorlib.dll'
149 System.IO.File.WriteAllBytes((⊂,file_name),(⊂⎕UCS data))
150 r←1 data file_name
151 :Else
152 ⍝ Failure: Unexpected Error While Writing the File
153 r←0 GetLastError file_name
154 :End
155 ∇
156
157 ∇ path←DialogFolder;DR;FBD;⎕USING
158 :Access Public Shared
159 ⍝ Selects a Directory with FolderBrowserDialog.
160
161 :Trap 0
162 ⎕USING←',System.Windows.Forms.dll'
163 FBD←⎕NEW System.Windows.Forms.FolderBrowserDialog
164 DR←System.Windows.Forms.DialogResult
165
166 ⍝ Shows the Dialog as Modal and Owned by the Current APL Process
167 :If DR.OK=FBD.ShowDialog ⍬
168 path←FBD.SelectedPath
169 :Else
170 path←''
171 :End
172
173 FBD.Dispose ⍝ With ShowDialog the form is not dispose automatically.
174
175 :Else
176 ⍝ Unexpected Error
177 ⎕←GetLastError
178 path←''
179 :EndTrap
180 ∇
181
182 ∇ file_name←DialogOpenFile;DR;OFD;⎕USING
183 :Access Public Shared
184
185 :Trap 0
186 ⍝ Selects a File to Open With OpenFileDialog.
187 ⎕USING←',System.Windows.Forms.dll'
188 OFD←⎕NEW System.Windows.Forms.OpenFileDialog
189 DR←System.Windows.Forms.DialogResult
190
191 OFD.Title←'Select a File'
192 OFD.CheckFileExists←0
193 OFD.InitialDirectory←'::{20D04FE0-3AEA-1069-A2D8-08002B30309D}' ⍝ My Computer
194 ⍝ OFD.InitialDirectory←'::{450D8FBA-AD25-11D0-98A8-0800361B1103}' ⍝ My Documents
195 ⍝ OFD.InitialDirectory←'::{208D2C60-3AEA-1069-A2D7-08002B30309D}' ⍝ My Network Places
196
197 ⍝ Shows the Dialog as Modal and Owned by the Current APL Process
198 :If DR.OK=OFD.ShowDialog ⍬
199 file_name←OFD.FileName
200 :Else
201 file_name←''
202 :End
203
204 OFD.Dispose ⍝ With ShowDialog the form is not dispose automatically.
205
206 :Else
207 ⍝ Unexpected Error
208 ⎕←GetLastError
209 file_name←''
210 :EndTrap
211 ∇
212
213 ∇ file_name←DialogSaveFile;DR;SFD;⎕USING
214 :Access Public Shared
215 ⍝ Selects a File to Save With SaveFileDialog.
216
217 :Trap 0
218 ⎕USING←',System.Windows.Forms.dll'
219 SFD←⎕NEW System.Windows.Forms.SaveFileDialog
220 DR←System.Windows.Forms.DialogResult
221
222 SFD.Title←'Save As'
223 SFD.OverwritePrompt←1
224 SFD.InitialDirectory←'::{20D04FE0-3AEA-1069-A2D8-08002B30309D}' ⍝ My Computer
225 ⍝ OFD.InitialDirectory←'::{450D8FBA-AD25-11D0-98A8-0800361B1103}' ⍝ My Documents
226 ⍝ OFD.InitialDirectory←'::{208D2C60-3AEA-1069-A2D7-08002B30309D}' ⍝ My Network Places
227
228 ⍝ Shows the Dialog as Modal and Owned by the Current APL Process
229 :If DR.OK=SFD.ShowDialog ⍬
230 file_name←SFD.FileName
231 :Else
232 file_name←''
233 :End
234
235 SFD.Dispose ⍝ With ShowDialog the form is not dispose automatically.
236
237 :Else
238 ⍝ Unexpected Error
239 ⎕←GetLastError
240 file_name←''
241 :EndTrap
242 ∇
243
244 ∇ r←DirCopy(source_path target_path);i;inter;source_file;source_files;target_file;⎕USING
245 :Access Public Shared
246 ⍝ Copies a Directory And Its Contents to a New Location. SubDirectories Are Not Copied.
247 ⍝ The Copy is Made Synchronously Without Animation.
248 ⍝
249 ⍝ source_path = Source Directory Path
250 ⍝ target_path = Target Directory Path
251 ⍝
252 ⍝ r = 1 (source_path target_path), if successfull
253 ⍝ r = 0 (ERROR) (source_path target_path), if failure
254
255 ⍝ CHECK IF SOURCE DIRECTORY EXISTS
256 :If 0=↑DirExists source_path
257 r←0 'Source Directory Does Not Exists'(source_path target_path) ⋄ →0
258 :End
259
260 ⍝ GET THE FILE NAMES OF THE SOURCE DIRECTORY
261 :If 1=1↑inter←GetFiles source_path'*' 'TopDirectoryOnly'
262 source_files←2⊃inter ⍝ File Names Without the Source Path
263 :Else
264 r←inter ⋄ →0
265 :End
266
267 ⍝ CREATE A NEW TARGET DIRECTORY, IF NECESSARY
268 :If 0=↑DirExists target_path
269 :AndIf 0=↑DirCreate target_path
270 r←0 'Not Able to Create the Target Directory'target_path ⋄ →0
271 :End
272
273 ⍝ COPY THE FILES FROM THE SOURCE TO THE TARGET DIRECTORY
274 :Trap 0
275
276 ⎕USING←',mscorlib.dll'
277 :For i :In ⍳⍴source_files
278 ⍝ File Name With the Source Directory Path
279 source_file←System.IO.Path.Combine((⊂,source_path),(⊂,i⊃source_files))
280
281 ⍝ File Name With the Target Directory Path
282 target_file←System.IO.Path.Combine((⊂,target_path),(⊂,i⊃source_files))
283
284 ⍝ 1 = Overwrite Permitted
285 System.IO.File.Copy((⊂,source_file),(⊂,target_file),1)
286 :EndFor
287
288 r←1(source_path target_path)
289
290 :Else
291 ⍝ Unexpected Error
292 r←0 GetLastError(source_path target_path)
293 :End
294 ∇
295
296 ∇ r←DirCreate path;⎕USING
297 :Access Public Shared
298 ⍝ Creates All Directories And Subdirectories as Specified By Path.
299 ⍝
300 ⍝ r = 1 (path), if successfull
301 ⍝ r = 0 (ERROR) (path), if Failure
302
303 :If ~∆IsString path←∊path
304 r←0 'Argument Must Be Characters'path ⋄ →0
305 :End
306
307 ⍝ MAKE THE CALL
308 :Trap 0
309 ⎕USING←',mscorlib.dll'
310 r←1((System.IO.Directory.CreateDirectory(⊂,path)).FullName)
311 :Else
312 ⍝ Unexpected Error While Creating the Directory
313 r←0 GetLastError path
314 :EndTrap
315 ∇
316
317 ∇ r←DirDelete path;⎕USING
318 :Access Public Shared
319 ⍝ Deletes The Specified Directory And Any Subdirectories in The Directory.
320 ⍝ Will Erase the Directory Even if Not Empty.
321 ⍝
322 ⍝ r = 1 (path), if Successfull
323 ⍝ r = 0 (ERROR) (path), if Failure
324
325 ⍝ CHECK IF DIRECTORY EXISTS
326 :If 0=↑DirExists path
327 ⍝ The Directory Does Not Exists. No Need to Erase It.
328 r←1 path ⋄ →0
329 :End
330
331 ⍝ MAKE THE CALL
332 :Trap 0
333 ⎕USING←',mscorlib.dll'
334 System.IO.Directory.Delete((⊂,path),1)
335 r←1 path
336 :Else
337 ⍝ Unexpected Error While Deleting the Directory
338 r←0 GetLastError path
339 :EndTrap
340 ∇
341
342 ∇ r←DirExists path;⎕USING
343 :Access Public Shared
344 ⍝ Determines Whether The Given Path Refers to an Existing Directory on Disk.
345 ⍝
346 ⍝ r = 1 (path), if Exists
347 ⍝ r = 0 (ERROR) (path), if Failure
348
349 :If ~∆IsString path←∊path
350 r←0 'Argument Must Be Characters'path ⋄ →0
351 :End
352
353 ⍝ MAKE THE CALL
354 :Trap 0
355 ⎕USING←',mscorlib.dll'
356 r←(System.IO.Directory.Exists(⊂,path))path
357 :Else
358 ⍝ Unexpected Error
359 r←0 GetLastError path
360 :End
361 ∇
362
363 ∇ r←DirMove(source_path target_path);⎕USING
364 :Access Public Shared
365 ⍝ Moves a Directory And Its Contents to a New Location. SubDirectories Are Moved.
366 ⍝ This Method is Identical to DirRename.
367 ⍝
368 ⍝ source_path = Source Directory Path
369 ⍝ target_path = Target Directory Path
370 ⍝
371 ⍝ r = 1 (source_path target_path), if successfull
372 ⍝ r = 0 (ERROR) (source_path target_path), if failure
373
374 ⍝ CHECK IF SOURCE DIRECTORY EXIST
375 :If 0=↑DirExists source_path
376 r←0 'Source Directory Does Not Exists'(source_path target_path) ⋄ →0
377 :End
378
379 ⍝ CHECK IF TARGET DIRECTORY EXIST
380 :If 1=↑DirExists target_path
381 r←0 'Target Directory Must Not Exists'(source_path target_path) ⋄ →0
382 :End
383
384 ⍝ MAKE THE CALL
385 :Trap 0
386 ⎕USING←',mscorlib.dll'
387 System.IO.Directory.Move((⊂,source_path),(⊂,target_path))
388 r←1(source_path target_path)
389 :Else
390 ⍝ Unexpected Error While Moving the Directory
391 r←0 GetLastError(source_path target_path)
392 :EndTrap
393 ∇
394
395 ∇ r←DirRename(actual_path new_path);⎕USING
396 :Access Public Shared
397 ⍝ Renames a Directory (This Method is Identical to DirMove).
398 ⍝
399 ⍝ actual_path = Actual Directory Path
400 ⍝ new_path = New Directory Path
401 ⍝
402 ⍝ r = 1 (actual_path new_path), if successfull
403 ⍝ r = 0 (ERROR) (arg), if failure
404
405 ⍝ CHECK IF ACTUAL DIRECTORY EXIST
406 :If 0=↑DirExists actual_path
407 r←0 'Actual Directory Does Not Exists'(actual_path new_path) ⋄ →0
408 :End
409
410 ⍝ CHECK IF NEW DIRECTORY EXIST
411 :If 1=↑DirExists new_path
412 r←0 'New Directory Must Not Exists'(actual_path new_path) ⋄ →0
413 :End
414
415 ⍝ MAKE THE CALL
416 :Trap 0
417 ⎕USING←',mscorlib.dll'
418 System.IO.Directory.Move((⊂,actual_path),(⊂,new_path))
419 r←1(actual_path new_path)
420 :Else
421 ⍝ Unexpected Error While Renaming the Directory
422 r←0 GetLastError(actual_path new_path)
423 :EndTrap
424 ∇
425
426 ∇ r←DirSize arg;directory_info;files_info;i;infos;IODI;path;search_pattern;test;⎕USING
427 :Access Public Shared
428 ⍝ Returns The Size in Bytes of a Directory And It's Subdirectories.
429 ⍝
430 ⍝ arg[1] = The fully qualified Directory Path
431 ⍝ arg[2] = [Optional] The search string to match against the names of directories found
432 ⍝ r = 1 (Size in Bytes of the Directories), if Successfull
433 ⍝ r = 0 (ERROR) (arg), if Failure
434 ⍝
435 ⍝ EXAMPLES: r ← DIO.DirSize Path [SearchPattern]
436 ⍝
437 ⍝ EXAMPLES:
438 ⍝ r←DIO.DirSize 'C:\MyDir' ⍝ Size of All Directories of 'C:\MyDir'
439 ⍝ r←DIO.DirSize 'C:\MyDir' 'p*' ⍝ Size of 'C:\MyDir' and SubDirectories Name beginning with 'p'
440
441 ⍝ PARSE THE ARGUMENT
442 :If 2=≡arg ⍝ Nested ?
443 :Select ↑⍴arg
444 :CaseList 0 1
445 path←arg←∊arg
446 search_pattern←'*'
447 :Case 2
448 (path search_pattern)←arg
449 :Else
450 r←0 'Argument Ill Formed'arg ⋄ →0
451 :EndSelect
452 :ElseIf ∆IsString arg ⍝ Characters ?
453 path←arg
454 search_pattern←'*'
455 :Else
456 r←0 'Argument Ill Formed'arg ⋄ →0
457 :End
458
459 ⍝ TEST IF TOP DIRECTORY EXISTS
460 :If 0=↑DirExists path
461 r←0 'Directory Does Not Exists'arg ⋄ →0
462 :End
463
464 ⍝ FIND SIZE OF TOP DIRECTORY AND SUBDIRECTORIES
465 :Trap 0
466 ⎕USING←',mscorlib.dll'
467 ⍝ Directory Structure for Top Directory
468 directory_info←,⎕NEW System.IO.DirectoryInfo(⊂,path)
469
470 ⍝ Iterate to find the subdirectories's size
471 r←0
472 :While 0≠⍴directory_info
473 IODI←1⊃directory_info
474 directory_info←1↓directory_info
475 :Trap 0
476 infos←IODI.GetFileSystemInfos(⊂,search_pattern)
477 :Else
478 ⍝ Directory That Can't be Access
479 'Directory Not Accessible: ',IODI.FullName
480 :Continue
481 :End
482
483 :If 0≠⍴infos
484 ⍝ Not an empty directory
485 files_info←(test←infos.GetType∊System.IO.FileInfo)/infos
486 :For i :In ⍳⍴files_info
487 :Trap 0
488 r+←{Convert.ToDouble ⍵.Length}(i⊃files_info)
489 :Else
490 ⍝ File That Can't be Read
491 'File Not Accessible: ',(i⊃files_info).FullName
492 :Continue
493 :EndTrap
494 :End
495 directory_info←directory_info,(~test)/infos
496 :Else
497 ⍝ Do Nothing - Empty Directory
498 :End
499 :EndWhile
500
501 r←1 r
502
503 :Else
504 ⍝ Unexpected Error
505 r←0 GetLastError arg
506 :End
507 ∇
508
509 ∇ r←EncodingDetector file_name;BA;bytes;ENC;inter;I2;string;z;⎕USING
510 :Access Public Shared
511 ⍝ Returns The Encoding of a File And it's Content.
512 ⍝ Detected From Their BOM: UTF-32LE, UTF-32BE, UTF-8, UTF-16LE, UTF-16BE.
513 ⍝ An UTF-8 File Without BOM or ASCII Encoding Can Also be Detected.
514 ⍝ This method could also be use to read a text file when the encoding is unknown.
515 ⍝
516 ⍝ file_name = Fully Qualified File Name
517 ⍝ r = 1 (ENCODING NAME) (file content) (file_name),if Success
518 ⍝ r = 0 (Exception) file_name ,if Failure
519
520 :Trap 0
521 ⍝ GET THE BINARY VALUE OF THE FILE
522 :If 1=↑inter←BFileRead file_name
523 bytes←2⊃inter ⍝ Character Vector
524 BA←⎕UCS bytes ⍝ Numeric Vector ⍝ Value
525 file_name←3⊃inter ⍝ In Case file_name Was Empty
526 :Else
527 r←0(2⊃inter)file_name ⋄ →0
528 :End
529
530 ⎕USING←',mscorlib.dll'
531 ⍝ DETECTION WITH THE BYTE ORDER MARK (BOM)
532 :If 255 254 0 0≡4⍴BA ⍝ 0xFF FE 00 00
533 ⍝ UTF-32LE Detected
534 ENC←⎕NEW System.Text.UTF32Encoding(0 1 1)
535 string←ENC.GetString((⊂,BA),4,¯4+⍴BA)
536 r←1 'UTF-32LE'string file_name ⋄ →0
537
538 :ElseIf 0 0 254 255≡4⍴BA ⍝ 0x00 00 FE FF
539 ⍝ UTF-32BE Detected
540 ENC←⎕NEW System.Text.UTF32Encoding(1 1 1)
541 string←ENC.GetString((⊂,BA),4,¯4+⍴BA)
542 r←1 'UTF-32BE'string file_name ⋄ →0
543
544 :ElseIf 239 187 191≡3⍴BA ⍝ 0xEF BB BF
545 ⍝ UTF-8 Detected
546 ENC←⎕NEW System.Text.UTF8Encoding(1 1)
547 string←ENC.GetString((⊂,BA),3,¯3+⍴BA)
548 r←1 'UTF-8'string file_name ⋄ →0
549
550 :ElseIf 255 254≡2⍴BA ⍝ 0xFF FE
551 ⍝ UTF-16LE Detected
552 ENC←⎕NEW System.Text.UnicodeEncoding(0 1 1)
553 string←ENC.GetString((⊂,BA),2,¯2+⍴BA)
554 r←1 'UTF-16LE'string file_name ⋄ →0
555
556 :ElseIf 254 255≡2⍴BA ⍝ 0xFE FF
557 ⍝ UTF-16BE Detected
558 ENC←⎕NEW System.Text.UnicodeEncoding(1 1 1)
559 string←ENC.GetString((⊂,BA),2,¯2+⍴BA)
560 r←1 'UTF-16BE'string file_name ⋄ →0
561 :End
562
563 ⍝ If there is no BOM the only way to test is to try to Decode
564 ⍝ and verify if there is an error or not doing so.
565
566 ⍝ *** DECODE TEST FOR UTF-8 WITHOUT BOM ***
567 ⍝ CREATE THE MULTIBYTE SEQUENCES
568 →(∨/254 255∊BA)/ASCII ⍝ 0xFE and 0xFF not permitted
569 z←(BA≥128)∧(BA≤191) ⍝ Multibyte are 1 and BA≤127 are 0
570 z←z+2×(BA≥192)∧(BA≤223) ⍝ 2 Multibyte Indicator
571 z←z+3×(BA≥224)∧(BA≤239) ⍝ 3 Multibyte Indicator
572 z←z+4×(BA≥240)∧(BA≤247) ⍝ 4 Multibyte Indicator
573 z←z+5×(BA≥248)∧(BA≤251) ⍝ 5 Multibyte Indicator
574 z←z+6×(BA≥252)∧(BA≤253) ⍝ 6 Multibyte Indicator
575
576 ⍝ CONVERT TO CHARACTERS
577 z←⎕UCS z
578
579 ∆SS←{⎕ML←3 ⍝ Approx alternative to xutils' ss.
580 srce find repl←,¨⍵ ⍝ Source, find and replace vectors.
581 cvex←{(+\find⍷⍵)⊂⍵}find,srce ⍝ Partitioned at find points.
582 (⍴repl)↓∊{repl,(⍴find)↓⍵}¨cvex}
583
584 ⍝ REPLACE THE MULTIBYTE SEQUENCE WITH ZERO'S
585 z←∆SS z(⎕UCS 2 1)(⎕UCS 0 0) ⍝ 2 Multibyte Sequences
586 z←∆SS z(⎕UCS 3 1 1)(⎕UCS 0 0 0) ⍝ 3 Multibyte Sequences
587 z←∆SS z(⎕UCS 4 1 1 1)(⎕UCS 0 0 0 0) ⍝ 4 Multibyte Sequences
588 z←∆SS z(⎕UCS 5 1 1 1 1)(⎕UCS 0 0 0 0 0) ⍝ 5 Multibyte Sequences
589 z←∆SS z(⎕UCS 6 1 1 1 1 1)(⎕UCS 0 0 0 0 0 0) ⍝ 6 Multibyte Sequences
590
591 ⍝ CONVERT TO NUMBERS
592 z←⎕UCS z
593
594 ⍝ THE SUM SHOULD BE 0 IF UTF-8 ENCODING
595 :If 0=+/z
596 ⍝ UTF-8 Encoding With No BOM Detected
597 ENC←⎕NEW System.Text.UTF8Encoding(1 1)
598 string←ENC.GetString((⊂,BA),0,⍴BA)
599 r←1 'UTF-8NoBOM'string file_name ⋄ →0
600 :End
601
602 ASCII: ⍝ *** DECODE TEST FOR ASCII ***
603 :If 1=∧/BA≤127
604 ⍝ ASCII DETECTED
605 ENC←⎕NEW System.Text.ASCIIEncoding
606 string←ENC.GetString((⊂,BA),0,⍴BA)
607 r←1 'ASCII'string file_name ⋄ →0
608 :End
609
610 ⍝ When All The Other Test Fails, We Use Then The Default Page of The Computer
611 ENC←System.Text.Encoding
612 string←ENC.Default.GetString((⊂,BA),0,⍴BA)
613 r←1 'DEFAULT'string file_name
614 :Else
615 ⍝ Unexpected Error While Searching the Encoding
616 r←0 GetLastError file_name
617 :EndTrap
618 ∇
619
620 ∇ r←FileCopy(source_FileName target_FileName);target_path;⎕USING
621 :Access Public Shared
622 ⍝ Copies an Existing File to a New File.
623 ⍝ Overwriting a file of the same name is allowed.
624 ⍝
625 ⍝ r = 1 (source_FileName target_FileName), if successfull
626 ⍝ r = 0 (ERROR) (source_FileName target_FileName), if failure
627
628 ⍝ CHECK IF SOURCE FILE EXISTS
629 :If 0=↑FileExists source_FileName
630 r←0 'Source File Does Not Exists'(source_FileName target_FileName) ⋄ →0
631 :End
632
633 ⎕USING←',mscorlib.dll'
634 ⍝ GET THE TARGET DIRECTORY FROM THE TARGET FILE NAME
635 :Trap 0
636 target_path←System.IO.Path.GetDirectoryName(⊂,target_FileName)
637 :Else
638 r←0 'Not Able to Get the Target Directory'(source_FileName target_FileName) ⋄ →0
639 :EndTrap
640
641 ⍝ CREATE A NEW TARGET DIRECTORY, IF IT DOES NOT EXISTS
642 :If 0=↑DirExists target_path
643 :AndIf 0=↑DirCreate target_path
644 r←0 'Not Able to Create the Target Directory'(source_FileName target_FileName) ⋄ →0
645 :End
646
647 ⍝ COPY THE FILE
648 :Trap 0
649 System.IO.File.Copy((⊂,source_FileName),(⊂,target_FileName),1) ⍝ 1 = Overwrite Permitted
650 r←1(source_FileName target_FileName)
651 :Else
652 r←0 GetLastError(source_FileName target_FileName)
653 :End
654 ∇
655
656 ∇ r←FileDelete file_name;⎕USING
657 :Access Public Shared
658 ⍝ Deletes a File.
659 ⍝
660 ⍝ file_name = Fully Qualified File Name
661 ⍝ r = 1 (file_name), if Successfull
662 ⍝ r = 0 (ERROR) (file_name) ,if Failure
663
664 :If ~∆IsString file_name
665 ⍝ Failure: 'file_name' Not Characters.
666 r←0 'File Name Must Be Characters'file_name ⋄ →0
667 :End
668
669 ⍝ MAKE THE CALL
670 :Trap 0
671 ⎕USING←',mscorlib.dll'
672 System.IO.File.Delete(⊂,file_name)
673 r←1 file_name
674 :Else
675 r←0 GetLastError file_name
676 :EndTrap
677 ∇
678
679 ∇ r←FileExists file_name;i;⎕USING
680 :Access Public Shared
681 ⍝ Verify if One or Many File(s) Exists.
682 ⍝
683 ⍝ file_name = One or More Fully Qualified File Name(s)
684 ⍝ r = 1 if Exists, 0 Otherwise
685 ⍝
686 ⍝ Example: r ← DIO.FileExists 'C:\MyDir\MyFile1.ext' 'C:\MyDir\MyFile2.ext'
687
688 ⍝ PARSE THE ARGUMENT
689 :If 2=≡file_name ⍝ Nested ?
690 ⍝ Nested File Name. Replace the Non-Character Names with ''
691 file_name[{(1=⍵)/⍳⍴,⍵}~∆IsString¨file_name]←⊂,''
692 :ElseIf ∆IsString file_name ⍝ Characters Not Nested ?
693 ⍝ Vector of Characters, only one File Name
694 file_name←,⊂,file_name
695 :Else
696 ⍝ Vector of Non-Character. No need to make the Call.
697 r←(⍴,file_name)⍴0 ⋄ →0
698 :End
699
700 ⍝ MAKE THE CALL
701 :Trap 0
702 ⎕USING←',mscorlib.dll'
703 r←System.IO.File.Exists¨⊂¨,¨file_name ⍝ File(s) Exist(s) or Name(s) are Valid
704 :Else
705 ⍝ Failure: Unexpected Error.
706 ⎕←'#.UTIL.DIO.FileExists Error: '
707 ⎕←GetLastError
708 :End
709 ∇
710
711 ∇ r←FileMove(source_FileName target_FileName);target_path;⎕USING
712 :Access Public Shared
713 ⍝ Moves a File to a New Location.
714 ⍝ Target Directory Is Created if Inexistant.
715 ⍝ Target File Name Is Deleted if Existant.
716 ⍝
717 ⍝ r = 1 (source_FileName target_FileName), if successfull
718 ⍝ r = 0 (ERROR) (source_FileName target_FileName), if failure
719
720 ⍝ CHECK IF SOURCE FILE EXISTS
721 :If 0=↑FileExists source_FileName
722 r←0 'Source File Does Not Exists'(source_FileName target_FileName) ⋄ →0
723 :End
724
725 ⎕USING←',mscorlib.dll'
726
727 ⍝ GET THE TARGET DIRECTORY FROM THE TARGET FILE NAME
728 :Trap 0
729 target_path←System.IO.Path.GetDirectoryName(⊂,target_FileName)
730 :Else
731 r←0 'Not Able to Obtain the Target Directory'(source_FileName target_FileName) ⋄ →0
732 :EndTrap
733
734 ⍝ CREATE A NEW TARGET DIRECTORY, IF IT DOES NOT EXISTS
735 :If 0=↑DirExists target_path
736 :AndIf 0=↑DirCreate target_path
737 r←0 'Not Able to Create the Target Directory'(source_FileName target_FileName) ⋄ →0
738 :End
739
740 ⍝ ERASE THE TARGET FILE IF EXISTING
741 :If 1=↑FileExists target_FileName
742 :AndIf 0=↑FileDelete target_FileName
743 r←0 'Not Able to Erase the Target File'(source_FileName target_FileName) ⋄ →0
744 :End
745
746 ⍝ MOVE THE FILE
747 :Trap 0
748 System.IO.File.Move((⊂,source_FileName),(⊂,target_FileName))
749 r←1(source_FileName target_FileName)
750 :Else
751 ⍝ Unexpected Error While Moving the File
752 r←0 GetLastError(source_FileName target_FileName)
753 :End
754 ∇
755
756 ∇ r←FileRename(old_FileName new_FileName);target_path;⎕USING
757 :Access Public Shared
758 ⍝ Renames a File (This Method is Identical to FileMove).
759 ⍝ New File Name Directory Is Created if Inexistant.
760 ⍝ New File Name Is Deleted if Existant.
761 ⍝
762 ⍝ r = 1 (old_FileName new_FileName), if successfull
763 ⍝ r = 0 (ERROR) (old_FileName new_FileName), if failure
764
765 ⍝ CHECK IF OLD FILE NAME EXISTS
766 :If 0=↑FileExists old_FileName
767 r←0 'Old File Name Does Not Exists'(old_FileName new_FileName) ⋄ →0
768 :End
769
770 ⎕USING←',mscorlib.dll'
771
772 ⍝ GET THE TARGET DIRECTORY FROM THE NEW FILE NAME
773 :Trap 0
774 target_path←System.IO.Path.GetDirectoryName(⊂,new_FileName)
775 :Else
776 r←0 'Not Able to Obtain the New File Directory'(old_FileName new_FileName) ⋄ →0
777 :EndTrap
778
779 ⍝ CREATE A NEW TARGET DIRECTORY, IF IT DOES NOT EXISTS
780 :If 0=↑DirExists target_path
781 :AndIf 0=↑DirCreate target_path
782 r←0 'Not Able to Create the New File Directory'(old_FileName new_FileName) ⋄ →0
783 :End
784
785 ⍝ ERASE THE TARGET FILE IF EXISTING
786 :If 1=↑FileExists new_FileName
787 :AndIf 0=↑FileDelete new_FileName
788 r←0 'Not Able to Erase the New File'(old_FileName new_FileName) ⋄ →0
789 :End
790
791 ⍝ RENAME THE FILE
792 :Trap 0
793 System.IO.File.Move((⊂,old_FileName),(⊂,new_FileName))
794 r←1(old_FileName new_FileName)
795 :Else
796 ⍝ Unexpected Error While Moving the File
797 r←0 GetLastError(old_FileName new_FileName)
798 :End
799 ∇
800
801 ∇ r←FileSize file_name;IOFI;⎕USING
802 :Access Public Shared
803 ⍝ Returns The Size of a File in Bytes.
804 ⍝
805 ⍝ file_name = Fully Qualified File Name
806 ⍝ r = 1 (Size in Bytes of the File) file_name, if Successfull
807 ⍝ r = 0 (ERROR) file_name, if Failure.
808 ⍝
809 ⍝ EXAMPLE: r ← DIO.FileSize 'C:\MyDir\MyFile.ext'
810
811 ⍝ CHECK IF FILE EXISTS
812 :If 0=↑FileExists file_name
813 r←0 'File Does Not Exists'file_name ⋄ →0
814 :End
815
816 ⍝ MAKE THE CALL
817 :Trap 0
818 ⎕USING←',mscorlib.dll'
819 IOFI←⎕NEW System.IO.FileInfo(⊂,file_name)
820 r←1(Convert.ToDouble(IOFI.Length))file_name
821 :Else
822 ⍝ Unexpected Error While Obtaining The File Size
823 r←0 GetLastError file_name
824 :End
825 ∇
826
827 ∇ r←FileVersion file_name;FVI;v;⎕USING
828 :Access Public Shared
829 ⍝ Returns The File Version of a File in Characters.
830 ⍝
831 ⍝ file_name = Fully Qualified File Name
832 ⍝ r = 1 (Version in Characters or '' if empty) (File Name) if Successfull
833 ⍝ r = 0 (Error Description) (File Name) if Failure
834
835 ⍝ CHECK IF FILE EXISTS
836 :If 0=↑FileExists file_name
837 r←0 'File Does Not Exists'file_name ⋄ →0
838 :End
839
840 ⍝ MAKE THE CALL
841 :Trap 0 6
842 ⍝ FileVersionInfo Object is Returned
843 ⎕USING←',System.dll'
844 FVI←System.Diagnostics.FileVersionInfo.GetVersionInfo(⊂,file_name)
845
846 ⍝ The File Version is a Vector of Characters
847 r←1(FVI.FileVersion)file_name
848 :Case 6
849 ⍝ VALUE ERROR. There is No File Version Available
850 r←1 ''file_name
851 :Else
852 ⍝ Unexpected Error While Obtaining The File Version
853 r←0 GetLastError file_name
854 :EndTrap
855 ∇
856
857 ∇ r←GetAttributes file_name;attributes;inter;⎕USING
858 :Access Public Shared
859 ⍝ Returns The Attributes of a File in Numeric And Litteral Form.
860 ⍝
861 ⍝ file_name = Fully Qualified File Name
862 ⍝ r = 1 (Numeric Attributes) (Litteral Attributes) (file_name), if Successfull
863 ⍝ r = 0 (Error Description) (file_name), if Failure
864 ⍝
865 ⍝ Attributes Are:
866 ⍝ 1 = ReadOnly
867 ⍝ 2 = Hidden
868 ⍝ 4 = System
869 ⍝ 16 = Directory
870 ⍝ 32 = Archive
871 ⍝ 64 = Device
872 ⍝ 128 = Normal
873 ⍝ 256 = Temporary
874 ⍝ 512 = SparseFile
875 ⍝ 1024 = ReparsePoint
876 ⍝ 2048 = Compressed
877 ⍝ 4096 = Offline
878 ⍝ 8192 = NotContentIndexed
879 ⍝ 16384 = Encrypted
880
881 ⍝ CHECK IF FILE EXISTS
882 :If 0=↑FileExists file_name
883 r←0 'File Does Not Exists'file_name ⋄ →0
884 :End
885
886 ⍝ MAKE THE CALL
887 :Trap 0
888 ⎕USING←',mscorlib.dll'
889 attributes←System.IO.File.GetAttributes(⊂,file_name)
890 :Else
891 ⍝ Unexpected Error While Obtaining the Attributes
892 r←0 GetLastError file_name ⋄ →0
893 :End
894
895 ⍝ FIND THE NUMERICAL VALUES ATTRIBUTES
896 inter←⌽(15⍴2)⊤attributes.value__
897 inter←,2*(-⎕IO)+inter/⍳⍴inter
898
899 ⍝ RETURNED VALUE IS THE NUMERICAL AND LITTERAL ATTRIBUTES
900 r←1 inter(attributes.ToString ⍬)file_name
901 ∇
902
903 ∇ r←GetCreationTime file_name;⎕USING
904 :Access Public Shared
905 ⍝ Returns The Creation Date And Time of a File as a DateTime Object.
906 ⍝
907 ⍝ file_name = Fully Qualified File Name
908 ⍝ r = 1 ([.net:DateTime]) (file_name), if Successfull
909 ⍝ r = 0 (ERROR) (file_name) ,if Failure
910
911 ⍝ CHECK IF FILE EXISTS
912 :If 0=↑FileExists file_name
913 r←0 'File Does Not Exists'file_name ⋄ →0
914 :End
915
916 ⍝ MAKE THE CALL
917 :Trap 0
918 ⎕USING←',mscorlib.dll'
919 r←1(System.IO.File.GetCreationTime(⊂,file_name))file_name
920 :Else
921 r←0 GetLastError file_name
922 :EndTrap
923
924 ∇
925
926 ∇ r←GetDirCurrent;⎕USING
927 :Access Public Shared
928 ⍝ Returns The Current Directory Path.
929 ⍝
930 ⍝ r = 1 (Current Directory), if Successfull
931 ⍝ r = 0 (ERROR), if Failure
932
933 ⍝ MAKE THE CALL
934 :Trap 0
935 ⎕USING←',mscorlib.dll'
936 r←1(System.IO.Directory.GetCurrentDirectory)
937 :Else
938 ⍝ Unexpected Error
939 r←0 GetLastError
940 :End
941 ∇
942
943 ∇ r←GetDirectories arg;arg_save;path;search_option;search_pattern;⎕USING
944 :Access Public Shared
945 ⍝ Returns The SubDirectories of a Directory.
946 ⍝
947 ⍝ USAGE: r ← DIO.GetDirectories Path [SearchPattern] [SearchOption]
948 ⍝
949 ⍝ Path = Directory Path to search
950 ⍝ SearchPattern = The search string to match against the names of directories found
951 ⍝ SearchOption = 'TopDirectoryOnly' to search only the specified directory (default)
952 ⍝ 'AllDirectories' to search all subdirectories
953 ⍝ r = 1 (Nested vector with the fully qualified directory names) arg, if Successfull
954 ⍝ r = 0 (ERROR) arg, if Failure
955 ⍝
956 ⍝ EXAMPLES:
957 ⍝ r←DIO.GetDirectories 'C:\MyDir' '*' 'AllDirectories' ⍝ All Subdirectories of 'C:\MyDir'
958 ⍝ r←DIO.GetDirectories 'C:\MyDir' 'p*' ⍝ Subdirectories of 'C:\MyDir' beginning with 'p'
959
960 ⍝ PARSE THE ARGUMENT
961 search_pattern←'*.*'
962 :If 2=≡arg_save←arg ⍝ Nested ?
963 :Select ↑⍴arg
964 :CaseList 0 1
965 path←∊arg
966 :Case 2
967 (path search_pattern)←arg
968 :Case 3
969 (path search_pattern search_option)←arg
970 :Else
971 r←0 'Argument Ill Formed'arg ⋄ →0
972 :EndSelect
973 :ElseIf ∆IsString arg ⍝ Characters ?
974 path←arg
975 :Else
976 r←0 'Argument Ill Formed'arg ⋄ →0
977 :End
978
979 ⍝ TEST IF DIRECTORY EXISTS
980 :If 0=↑DirExists path
981 r←0 'Directory Does Not Exists'arg_save ⋄ →0
982 :End
983
984 ⎕USING←',mscorlib.dll'
985
986 ⍝ TEST IF SEARCH OPTION IS VALID
987 :If 0≠⎕NC'search_option' ⍝ Exists ?
988 ⍝ search_option is defined
989 :If ∆IsString search_option ⍝ Characters ?
990 :If 1=(⊂search_option)∊System.IO.SearchOption.⎕NL ¯2
991 ⍝ Valid Search Option
992 search_option←⍎'System.IO.SearchOption.',search_option
993 (3⊃arg)←search_option
994 :Else
995 ⍝ Search Option is not Valid
996 ⍝ 'AllDirectories' or 'TopDirectoryOnly' are Valid
997 r←0 'Search Option is Not Valid'arg ⋄ →0
998 :End
999 :Else
1000 ⍝ Search Option is not Valid
1001 ⍝ 'AllDirectories' or 'TopDirectoryOnly' are Valid
1002 r←0 'Search Option is Not Valid'arg ⋄ →0
1003 :End
1004 :Else
1005 ⍝ search_option is not defined
1006 search_option←System.IO.SearchOption.TopDirectoryOnly
1007 :End
1008
1009 ⍝ MAKE THE CALL
1010 :Trap 0
1011 r←1(System.IO.Directory.GetDirectories((⊂,path),(⊂,search_pattern),(search_option)))arg
1012 :Else
1013 ⍝ Failure: Unexpected Error
1014 r←0 GetLastError arg
1015 :End
1016 ∇
1017
1018 ∇ r←GetDrives;⎕USING
1019 :Access Public Shared
1020 ⍝ Returns The Names of The Logical Drives on This Computer in The Form "<drive letter>:\".
1021 ⍝
1022 ⍝ r = 1 (Logical Drives), if Successfull
1023 ⍝ r = 0 (ERROR), if Failure
1024
1025 ⍝ MAKE THE CALL
1026 :Trap 0
1027 ⎕USING←',mscorlib.dll'
1028 r←1(System.IO.Directory.GetLogicalDrives)
1029 :Else
1030 ⍝ Unexpected Error While Obtaining the Logical Drives
1031 r←0 GetLastError
1032 :End
1033 ∇
1034
1035 ∇ r←GetExtension file_name;⎕USING
1036 :Access Public Shared
1037 ⍝ Returns The Extension of a Path String.
1038 ⍝ Will Return the extension of a file name even if the file does not exists
1039 ⍝
1040 ⍝ file_name = Fully Qualified File Name
1041 ⍝ r = (Extension), if Successfull
1042 ⍝ r = '', if Failure
1043
1044 ⍝ MAKE THE CALL
1045 :Trap 0
1046 ⎕USING←',mscorlib.dll'
1047 r←System.IO.Path.GetExtension(⊂,file_name)
1048 :Else
1049 ⍝ Unexpected Error While Getting the Extension
1050 r←''
1051 :End
1052 ∇
1053
1054 ∇ r←GetFiles arg;arg_save;path;rLong;rShort;search_option;search_pattern;⎕USING
1055 :Access Public Shared
1056 ⍝ Returns The Names of Files in a Directory.
1057 ⍝
1058 ⍝ USAGE: r ← DIO.GetFiles Path [SearchPattern] [SearchOption]
1059 ⍝
1060 ⍝ Path = Directory Path to search
1061 ⍝ SearchPattern = The search string to match against the names of files found
1062 ⍝ SearchOption = 'TopDirectoryOnly' to search only the specified directory (default)
1063 ⍝ 'AllDirectories' to search all subdirectories
1064 ⍝
1065 ⍝ r = 1 (Nested vector with the SHORT files names) (Nested vector with the LONG files names) arg, if Successfull
1066 ⍝ r = 0 (ERROR) arg, if Failure
1067 ⍝
1068 ⍝ EXAMPLES:
1069 ⍝ r←DIO.GetFiles 'C:\MyDir' '*' 'AllDirectories' ⍝ All Files in All Subdirectories of 'C:\MyDir'
1070 ⍝ r←DIO.GetFiles 'C:\MyDir' 'p*' ⍝ Files of 'C:\MyDir' beginning with 'p'
1071 ⍝ r←DIO.GetFiles 'C:\MyDir' '*.exe' ⍝ Files of 'C:\MyDir' ending with '.exe'
1072
1073 ⍝ PARSE THE ARGUMENT
1074 search_pattern←'*.*'
1075 :If 2=≡arg_save←arg
1076 :Select ↑⍴arg
1077 :CaseList 0 1
1078 path←∊arg
1079 :Case 2
1080 (path search_pattern)←arg
1081 :Case 3
1082 (path search_pattern search_option)←arg
1083 :Else
1084 r←0 'Argument Ill Formed'arg_save ⋄ →0
1085 :EndSelect
1086 :ElseIf ∆IsString arg
1087 path←arg
1088 :Else
1089 r←0 'Argument Ill Formed'arg_save ⋄ →0
1090 :End
1091
1092 ⍝ TEST IF DIRECTORY EXISTS
1093 :If 0=↑DirExists path
1094 r←0 'Directory does not Exists'arg_save ⋄ →0
1095 :End
1096
1097 ⎕USING←',mscorlib.dll'
1098
1099 ⍝ TEST IF SEARCH OPTION IS VALID
1100 :If 0≠⎕NC'search_option' ⍝ Exists ?
1101 ⍝ search_option is defined
1102 :If ∆IsString search_option ⍝ Characters ?
1103 :If 1=(⊂search_option)∊System.IO.SearchOption.⎕NL ¯2
1104 ⍝ Valid Search Option
1105 search_option←⍎'System.IO.SearchOption.',search_option
1106 (3⊃arg)←search_option
1107 :Else
1108 ⍝ Search Option is not Valid
1109 ⍝ 'AllDirectories' or 'TopDirectoryOnly' are Valid
1110 r←0 'Search Option is Not Valid'arg ⋄ →0
1111 :End
1112 :Else
1113 ⍝ Search Option is not Valid
1114 ⍝ 'AllDirectories' or 'TopDirectoryOnly' are Valid
1115 r←0 'Search Option is Not Valid'arg ⋄ →0
1116 :End
1117 :Else
1118 ⍝ Default Value
1119 search_option←System.IO.SearchOption.TopDirectoryOnly
1120 :End
1121
1122 ⍝ MAKE THE CALL
1123 :Trap 0
1124 rLong←System.IO.Directory.GetFiles((⊂,path),(⊂,search_pattern),(search_option))
1125 :If 0≠↑⍴rLong
1126 rShort←System.IO.Path.GetFileName¨⊂¨,¨rLong ⍝ To Removes the Directory Path
1127 rLong←∆DBR¨⊂[2]rLong[⍋rLong←⊃[2]rLong;] ⍝ Sorts the Names
1128 rShort←∆DBR¨⊂[2]rShort[⍋rShort←⊃[2]rShort;] ⍝ Sorts the Names
1129 :Else
1130 ⍝ The directory is empty
1131 rShort←rLong
1132 :End
1133
1134 r←1 rShort rLong arg_save
1135 :Else
1136 ⍝ Failure: Unexpected Error
1137 r←0 GetLastError arg_save
1138 :End
1139 ∇
1140
1141 ∇ r←GetLastError
1142 :Access Public Shared
1143 ⍝ Returns the Last Error
1144
1145 :If 90=⎕EN
1146 r←'EXCEPTION: ',⎕EXCEPTION.Message
1147 :Else
1148 r←(1⊃⎕DM),': ',∆DBR(2⊃⎕DM)
1149 :EndIf
1150 ∇
1151
1152 ∇ sa←GetNetStringArray apl;i;⎕USING
1153 ⍝ To get a .Net String[]
1154
1155 ⎕USING←'System'
1156 apl←,apl
1157 sa←Array.CreateInstance(Type.GetType⊂'System.String')(⍴apl)
1158
1159 :For i :In ⍳⍴apl
1160 sa.SetValue(i⊃apl)(i-1)
1161 :EndFor
1162 ∇
1163
1164 ∇ r←GetTempFileName;⎕USING
1165 :Access Public Shared
1166 ⍝ Returns a Uniquely Named, Zero-Byte Temporary File on Disk With Full Path.
1167 ⍝
1168 ⍝ r = 1 (File Name), if Successfull
1169 ⍝ r = 0 (ERROR), if Failure
1170
1171 ⍝ MAKE THE CALL
1172 :Trap 0
1173 ⎕USING←',mscorlib.dll'
1174 r←1(System.IO.Path.GetTempFileName)
1175 :Else
1176 ⍝ Unexpected Error
1177 r←0 GetLastError
1178 :EndTrap
1179 ∇
1180
1181 ∇ r←GetTempPath;⎕USING
1182 :Access Public Shared
1183 ⍝ Returns The Path of The Current System's Temporary Folder.
1184 ⍝
1185 ⍝ r = 1 (Path), if Successfull
1186 ⍝ r = 0 (ERROR), if Failure
1187
1188 ⍝ MAKE THE CALL
1189 :Trap 0
1190 ⎕USING←',mscorlib.dll'
1191 r←1(System.IO.Path.GetTempPath)
1192 :Else
1193 ⍝ Unexpected Error
1194 r←0 GetLastError
1195 :EndTrap
1196 ∇
1197
1198 ∇ r←{nsOrKey}NS2File fileName;binaryFormatter;case;fileStream;hashTable;key;ns;val;⎕USING
1199 :Access Public Shared
1200 ⍝ Function to save and retrieve a Namespace (NS) under a format compatible with C#.
1201 ⍝ Based on: http://forums.dyalog.com/viewtopic.php?f=18&t=213&p=856
1202 ⍝
1203 ⍝ fileName = Fully qualified file name
1204 ⍝ nsOrKey = Namespace to save under fileName
1205 ⍝ = Key of retreived NS saved under fileName
1206 ⍝ = if empty returns the full NS saved under fileName
1207
1208 ⎕USING←0⍴⊂'' ⍝ To Speed up ⎕NC
1209
1210 ⍝ Check for nsOrKey and select the case:
1211 :If 0=⎕NC'nsOrKey'
1212 ⍝ nsOrKey does not exist. Read the fileName and return a NameSpace.
1213 case←'ReadFile'
1214
1215 :ElseIf 9=⎕NC'nsOrKey'
1216 ⍝ nsOrKey is a NameSpace. Save it under FileName.
1217 ns←nsOrKey ⋄ case←'SaveNS'
1218
1219 :ElseIf ' '=↑1↑0⍴nsOrKey
1220 ⍝ nsOrKey is character(s). Read the fileName and return only this Key.
1221 key←nsOrKey ⋄ case←'ReturnKey'
1222
1223 :Else
1224 r←0 'Left Argument Must be a NameSpace or a Character Vector(Key)'
1225 →0
1226
1227 :End
1228
1229 ⍝ The only ⎕USING required subsequently.
1230 ⎕USING←',mscorlib.dll'
1231
1232 ⍝ Required for all the cases.
1233 binaryFormatter←⎕NEW System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
1234
1235 :Select case
1236 :Case 'ReadFile'
1237 ⍝ Read the file and return a namespace
1238 :Trap 0
1239 fileStream←⎕NEW System.IO.FileStream(fileName System.IO.FileMode.Open)
1240 hashTable←binaryFormatter.Deserialize fileStream
1241 ns←⎕NS''
1242
1243 :For key :In hashTable.Keys
1244 val←hashTable.get_Item⊂key
1245 ⍎'ns.',key,'←val'
1246 :End
1247
1248 fileStream.Close
1249 r←1 ns
1250
1251 :Else
1252 →ERROR
1253
1254 :EndTrap
1255
1256 :Case 'ReturnKey'
1257
1258 :Trap 0
1259 fileStream←⎕NEW System.IO.FileStream(fileName System.IO.FileMode.Open)
1260 hashTable←binaryFormatter.Deserialize fileStream
1261
1262 :If ~hashTable.ContainsKey(⊂key)
1263 ⍝ Key is not present in HashTable
1264 r←0 'Invalid Key: ',∊key ⋄ fileStream.Close ⋄ →0
1265
1266 :Else
1267 ⍝ The Key is valid.
1268 val←hashTable.get_Item⊂key
1269 fileStream.Close
1270 r←1 val
1271 :End
1272
1273 :Else
1274 →ERROR
1275
1276 :EndTrap
1277
1278
1279 :Case 'SaveNS'
1280 ⍝ Save the NS to the file name.
1281 :Trap 0
1282 hashTable←⎕NEW System.Collections.Hashtable
1283
1284 :For key :In ns.⎕NL-2
1285 hashTable.Add key(ns.⍎key)
1286 :End
1287
1288 fileStream←⎕NEW System.IO.FileStream(fileName System.IO.FileMode.Create)
1289 binaryFormatter.Serialize fileStream hashTable
1290 r←1 fileStream.Length hashTable.Count ⍝ Bytes Written, Number of Items
1291 fileStream.Close
1292
1293 :Else
1294 →ERROR
1295
1296 :EndTrap
1297
1298 :EndSelect
1299
1300 →0
1301
1302 ERROR:
1303
1304 ⍝ Show the Error:
1305 :If 90=⎕EN
1306
1307 r←0('#.UTIL.DIO.NS2File EXCEPTION: ',⎕EXCEPTION.Message)
1308 :Else
1309 r←0((1⊃⎕DM),': ',{(~(∧\' '=⍵)∨(⌽∧\⌽' '=⍵))/⍵}(2⊃⎕DM))
1310 :EndIf
1311
1312 ⍝ Try to close the fileStream in case the error happened before closing it:
1313 :Trap 0
1314 fileStream.Close
1315 :EndTrap
1316 ∇
1317
1318 ∇ r←new_attributes SetAttributes file_name;inter;old_attributes;⎕USING
1319 :Access Public Shared
1320 ⍝ Sets The Attributes of a File.
1321 ⍝ Positive Attributes Will be Added and Negative Attributes Removed
1322 ⍝
1323 ⍝ file_name = Fully Qualified File Name
1324 ⍝ r = 1 (Numeric Attributes) (Litteral Attributes) (file_name), if Successfull
1325 ⍝ r = 0 (Error Description) (file_name), if Failure
1326 ⍝
1327 ⍝ Attributes Are:
1328 ⍝ 1 = ReadOnly
1329 ⍝ 2 = Hidden
1330 ⍝ 4 = System
1331 ⍝ 16 = Directory
1332 ⍝ 32 = Archive
1333 ⍝ 64 = Device
1334 ⍝ 128 = Normal
1335 ⍝ 256 = Temporary
1336 ⍝ 512 = SparseFile
1337 ⍝ 1024 = ReparsePoint
1338 ⍝ 2048 = Compressed
1339 ⍝ 4096 = Offline
1340 ⍝ 8192 = NotContentIndexed
1341 ⍝ 16384 = Encrypted
1342
1343 ⍝ CHECK IF FILE EXISTS
1344 :If 0=↑FileExists file_name
1345 r←0 'File Does Not Exists'file_name ⋄ →0
1346 :End
1347
1348 ⍝ GET THE CURRENT ATTRIBUTES
1349 :If 0=↑inter←GetAttributes file_name
1350 ⍝ Failure
1351 r←0(2⊃inter)file_name ⋄ →0
1352 :Else
1353 ⍝ Success
1354 old_attributes←2⊃inter
1355 :End
1356
1357 ⍝ REMOVE THE NEGATIVE ATTRIBUTES
1358 inter←old_attributes~|(new_attributes<0)/new_attributes
1359
1360 ⍝ ADD THE POSITIVE ATTRIBUTES
1361 inter←inter,(new_attributes>0)/new_attributes
1362
1363 ⍝ MAKE SURE THERE IS NO REPETITIONS
1364 inter←∪inter
1365
1366 ⍝ ADD UP ALL THE ATTRIBUTES
1367 inter←+/inter
1368
1369 ⍝ MAKE THE CALL
1370 :Trap 0
1371 ⎕USING←',mscorlib.dll'
1372 System.IO.File.SetAttributes((⊂,file_name),(⍬⍴inter))
1373 r←GetAttributes file_name
1374 :Else
1375 ⍝ Unexpected Error While Setting the Attributes
1376 r←0 GetLastError file_name
1377 :EndTrap
1378 ∇
1379
1380 ∇ r←SetDirCurrent path;⎕USING
1381 :Access Public Shared
1382 ⍝ Sets The Current Directory Path.
1383 ⍝
1384 ⍝ r = 1 (Current Directory), if Successfull
1385 ⍝ r = 0 (ERROR) path, if Failure
1386
1387 :If ~∆IsString path←∊path
1388 ⍝ Failure: 'path' Not Characters.
1389 r←0 'Path Must Be Characters'path ⋄ →0
1390 :End
1391
1392 ⍝ Check if 'path' Exists
1393 :If 0=↑DirExists path
1394 r←0 'Directory Does Not Exists'path ⋄ →0
1395 :End
1396
1397 ⍝ MAKE THE CALL
1398 :Trap 0
1399 ⎕USING←',mscorlib.dll'
1400 System.IO.Directory.SetCurrentDirectory(⊂,path)
1401 r←1 path
1402 :Else
1403 ⍝ Unexpected Error
1404 r←0 GetLastError path
1405 :End
1406 ∇
1407
1408 ∇ r←text TFileAppend file_name;⎕USING
1409 :Access Public Shared
1410 ⍝ Appends The Specified Text to a File Using The Default Windows Code Page.
1411 ⍝ The file is created if it does not already exist.
1412 ⍝ The file handle is guaranteed to be closed by this method, even if exceptions are raised.
1413 ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name.
1414 ⍝
1415 ⍝ file_name = Fully Qualified File Name, if Empty Use DialogOpenFile
1416 ⍝ text = Vector of Characters or String Object
1417 ⍝ r = 1 (text) (File Name), if Successfull
1418 ⍝ r = 0 (ERROR) (File Name), if Failure
1419 ⍝
1420 ⍝ Example: R ← 'ABCDEF' DIO.TFileAppend 'C:\MyTextFile.txt'
1421 ⍝ Success: R ← 1 'ABCDEF' 'C:\MyTextFile.txt'
1422 ⍝ Failure: R ← 0 'Error Description' 'C:\MyTextFile.txt'
1423 ⍝
1424 ⍝ Example: R ← 'ABCDEF' DIO.TFileAppend '' ⍝ Select the File to Write Using the Open File Dialog
1425
1426 ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTERS
1427 :If 0=↑⍴file_name ⍝ Empty ?
1428 ⍝ Call the Open File Dialog
1429 :If 0=↑⍴file_name←DialogOpenFile
1430 ⍝ Failure: No File Name. Exit the program.
1431 r←0 'No File Name' '' ⋄ →0
1432 :Else
1433 ⍝ Do Nothing, 'file_name' is not Empty.
1434 :End
1435 :ElseIf ~∆IsString file_name
1436 ⍝ Failure: 'file_name' Not Characters.
1437 r←0 'File Name Must Be Characters'file_name ⋄ →0
1438 :Else
1439 ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty.
1440 :End
1441
1442 ⍝ VERIFY IF 'text' IS CHARACTERS
1443 :If ~∆IsString text
1444 ⍝ 'text' Must be Characters
1445 r←0 'The Text Must be Characters'file_name ⋄ →0
1446 :End
1447
1448 ⍝ MAKE THE CALL
1449 :Trap 0
1450 ⍝ System.Text.Encoding.Default = Default Encoding Page On This Computer
1451 ⎕USING←',mscorlib.dll'
1452 System.IO.File.AppendAllText((⊂,file_name),(⊂,text),(System.Text.Encoding.Default))
1453 r←1 text file_name
1454 :Else
1455 ⍝ Failure: Unexpected Error While Writing the File
1456 r←0 GetLastError file_name
1457 :EndTrap
1458 ∇
1459
1460 ∇ r←TFileRead file_name;⎕USING
1461 :Access Public Shared
1462 ⍝ Reads The Contents of a Text File Using the Default Windows Code Page.
1463 ⍝ The file handle is guaranteed to be closed by this method, even if exceptions are raised.
1464 ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name.
1465 ⍝
1466 ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile
1467 ⍝ r = 1 (text) (File Name), if Successfull
1468 ⍝ r = 0 (Error Description) (File Name), if Failure
1469 ⍝
1470 ⍝ Example: R ← DIO.TFileRead 'C:\MyTextFile.txt'
1471 ⍝ Success: R ← 1 (text) 'C:\MyTextFile.txt'
1472 ⍝ Failure: R ← 0 (ERROR) 'C:\MyTextFile.txt'
1473 ⍝
1474 ⍝ Example: R ← DIO.TFileRead '' ⍝ To Select the File to Read Using the Open File Dialog
1475
1476 ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTERS
1477 :If 0=↑⍴file_name ⍝ Empty ?
1478 ⍝ Call the Open File Dialog
1479 :If 0=↑⍴file_name←DialogOpenFile
1480 ⍝ Failure: No File Name. Exit the program.
1481 r←0 'No File Name' '' ⋄ →0
1482 :Else
1483 ⍝ Do Nothing, 'file_name' is not Empty.
1484 :End
1485 :ElseIf ~∆IsString file_name
1486 ⍝ Failure: 'file_name' Not Characters.
1487 r←0 'File Name Must Be Characters'file_name ⋄ →0
1488 :Else
1489 ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty.
1490 :End
1491
1492 ⍝ MAKE THE CALL
1493 :Trap 0
1494 :If 0=↑FileExists file_name
1495 ⍝ Failure: The File Name does not exists or is ill formed.
1496 r←0 'File Name Does Not Exists'file_name
1497 :Else
1498 ⍝ Success: The File Name Exists.
1499 ⍝ STE.Default = Default Encoding Page On Your Computer
1500 ⎕USING←',mscorlib.dll'
1501 r←1(System.IO.File.ReadAllText((⊂,file_name),(System.Text.Encoding.Default)))file_name
1502 :End
1503 :Else
1504 ⍝ Failure: Unexpected Error While Reading the File
1505 r←0 GetLastError file_name
1506 :EndTrap
1507 ∇
1508
1509 ∇ r←text TFileWrite file_name;⎕USING
1510 :Access Public Shared
1511 ⍝ Creates a New File, Writes The Text to The File Using The Default Windows Code Page.
1512 ⍝ If the target file already exists, it is overwritten.
1513 ⍝ The file handle is guaranteed to be closed by this method, even if exceptions are raised.
1514 ⍝ Use UFileWrite when Writing a File to be Exchange Outside Your Country (Code Page May be Different)
1515 ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name.
1516 ⍝
1517 ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile
1518 ⍝ text = Vector of Characters
1519 ⍝ r = 1 (text) (File Name), if Successfull
1520 ⍝ r = 0 (ERROR) (File Name), if Failure
1521 ⍝
1522 ⍝ Example: R ← 'ABCDEF' DIO.TFileWrite 'C:\MyTextFile'
1523 ⍝ Success: R ← 1 'ABCDEF' 'C:\MyTextFile'
1524 ⍝ Failure: R ← 0 'Error Description' 'C:\MyTextFile'
1525 ⍝
1526 ⍝ Example: R ← 'ABCDEF' DIO.TFileWrite '' ⍝ Select the File to Write Using the Open File Dialog
1527
1528 ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTERS
1529 :If 0=↑⍴file_name ⍝ Empty ?
1530 ⍝ Call the Open File Dialog
1531 :If 0=↑⍴file_name←DialogOpenFile
1532 ⍝ Failure: No File Name. Exit the program.
1533 r←0 'No File Name' '' ⋄ →0
1534 :Else
1535 ⍝ Do Nothing, 'file_name' is not Empty.
1536 :End
1537 :ElseIf ~∆IsString file_name
1538 ⍝ Failure: 'file_name' Not Characters.
1539 r←0 'File Name Must Be Characters'file_name ⋄ →0
1540 :Else
1541 ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty.
1542 :End
1543
1544 ⍝ VERIFY IF 'text' IS CHARACTERS
1545 :If ~∆IsString text
1546 ⍝ 'text' Must be Characters
1547 r←0 'The Text Must be Characters'file_name ⋄ →0
1548 :End
1549
1550 ⍝ MAKE THE CALL
1551 :Trap 0
1552 ⍝ STE.Default = Default Encoding Page On Your Computer
1553 ⎕USING←',mscorlib.dll'
1554 System.IO.File.WriteAllText((⊂,file_name),(⊂,text),(System.Text.Encoding.Default))
1555 r←1 text file_name
1556 :Else
1557 ⍝ Failure: Unexpected Error While Writing the File
1558 r←0 GetLastError file_name
1559 :EndTrap
1560 ∇
1561
1562 ∇ r←text UFileAppend file_name;⎕USING
1563 :Access Public Shared
1564 ⍝ Appends The Specified Text to a File Using The UTF-8 Encoding.
1565 ⍝ The file is created if it does not already exist.
1566 ⍝ The file handle is guaranteed to be closed by this method, even if exceptions are raised.
1567 ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name.
1568 ⍝
1569 ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile
1570 ⍝ text = Vector of Characters
1571 ⍝ r = 1 (text) (File Name), if Successfull
1572 ⍝ r = 0 (ERROR) (File Name), if Failure
1573 ⍝
1574 ⍝ Example: R ← 'ABCDEF' DIO.UFileAppend 'C:\MyTextFile.txt'
1575 ⍝ Success: R ← 1 'ABCDEF' 'C:\MyTextFile.txt'
1576 ⍝ Failure: R ← 0 'Error Description' 'C:\MyTextFile.txt'
1577 ⍝
1578 ⍝ Example: R ← 'ABCDEF' DIO.UFileAppend '' ⍝ Select the File to Write Using the Open File Dialog
1579
1580 ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTERS
1581 :If 0=↑⍴file_name ⍝ Empty ?
1582 ⍝ Call the Open File Dialog
1583 :If 0=↑⍴file_name←DialogOpenFile
1584 ⍝ Failure: No File Name. Exit the program.
1585 r←0 'No File Name' '' ⋄ →0
1586 :Else
1587 ⍝ Do Nothing, 'file_name' is not Empty.
1588 :End
1589 :ElseIf ~∆IsString file_name
1590 ⍝ Failure: 'file_name' Not Characters.
1591 r←0 'File Name Must Be Characters'file_name ⋄ →0
1592 :Else
1593 ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty.
1594 :End
1595
1596 ⍝ VERIFY IF 'text' IS CHARACTERS
1597 :If ~∆IsString text
1598 ⍝ 'text' Must be Characters
1599 r←0 'The Text Must be Characters'file_name ⋄ →0
1600 :End
1601
1602 ⍝ MAKE THE CALL
1603 :Trap 0
1604 ⍝ STE.UTF8 = UTF-8 Encoding
1605 ⎕USING←',mscorlib.dll'
1606 System.IO.File.AppendAllText((⊂,file_name),(⊂,text),(System.Text.Encoding.UTF8))
1607 r←1 text file_name
1608 :Else
1609 ⍝ Failure: Unexpected Error While Writing the File
1610 r←0 GetLastError file_name
1611 :EndTrap
1612 ∇
1613
1614 ∇ r←lines UFileAppendAllLines file_name;⎕USING
1615 :Access Public Shared
1616 ⍝ Appends lines to a file using UTF8 encoding, and then closes the file.
1617 ⍝ If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file.
1618 ⍝ The file handle is guaranteed to be closed by this method, even if exceptions are raised.
1619 ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name.
1620 ⍝
1621 ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile
1622 ⍝ lines = Vector where each element is a line
1623 ⍝ r = 1 (lines) (File Name), if Successfull
1624 ⍝ r = 0 (ERROR) (File Name), if Failure
1625 ⍝
1626 ⍝ Example: R ← ('line 1' 'line 2') DIO.UFileWriteAllLines 'C:\MyTextFile'
1627 ⍝ Success: R ← 1 ('line 1' 'line 2') 'C:\MyTextFile'
1628 ⍝ Failure: R ← 0 'Error Description' 'C:\MyTextFile'
1629 ⍝
1630 ⍝ Example: R ← ('line 1' 'line 2') DIO.UFileWriteAllLines '' ⍝ Select the File to Write Using the Open File Dialog
1631
1632 ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTERS
1633 :If 0=↑⍴file_name ⍝ Empty ?
1634 ⍝ Call the Open File Dialog
1635 :If 0=↑⍴file_name←DialogOpenFile
1636 ⍝ Failure: No File Name. Exit the program.
1637 r←0 'No File Name' '' ⋄ →0
1638 :Else
1639 ⍝ Do Nothing, 'file_name' is not Empty.
1640 :End
1641 :ElseIf ~∆IsString file_name
1642 ⍝ Failure: 'file_name' Not Characters.
1643 r←0 'File Name Must Be Characters'file_name ⋄ →0
1644 :Else
1645 ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty.
1646 :End
1647
1648 ⍝ MAKE THE CALL
1649 :Trap 0
1650 ⍝ Convert the lines to a .Net String[] (String Array)
1651 lines←GetNetStringArray lines
1652
1653 ⍝ STE.UTF8 = UTF-8 Encoding
1654 ⎕USING←',mscorlib.dll'
1655 System.IO.File.AppendAllLines((⊂,file_name),(lines),(System.Text.Encoding.UTF8))
1656 r←1 lines file_name
1657 :Else
1658 ⍝ Failure: Unexpected Error While Writing the File
1659 r←0 GetLastError file_name
1660 :EndTrap
1661 ∇
1662
1663 ∇ r←UFileRead file_name;⎕USING
1664 :Access Public Shared
1665 ⍝ Reads The Contents of a Unicode Text File.
1666 ⍝ This method attempts to automatically detect the encoding of a file based on the
1667 ⍝ presence of byte order marks (BOM). Encoding formats UTF-8 and UTF-32 (both big-endian
1668 ⍝ and little-endian) can be detected.
1669 ⍝ The file handle is guaranteed to be closed by this method, even if exceptions are raised.
1670 ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name.
1671 ⍝
1672 ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile
1673 ⍝ r = 1 (text) (File Name), if Successfull
1674 ⍝ r = 0 (ERROR) (File Name), if Failure
1675 ⍝
1676 ⍝ Example: R ← DIO.UFileRead 'C:\MyTextFile.txt'
1677 ⍝ Success: R ← 1 text 'C:\MyTextFile.txt'
1678 ⍝ Failure: R ← 0 'Error Description' 'C:\MyTextFile.txt'
1679 ⍝
1680 ⍝ Example: R ← DIO.UFileRead '' ⍝ To Select the File to Read Using the Open File Dialog
1681
1682 ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTERS
1683 :If 0=↑⍴file_name ⍝ Empty ?
1684 ⍝ Call the Open File Dialog
1685 :If 0=↑⍴file_name←DialogOpenFile
1686 ⍝ Failure: No File Name. Exit the program.
1687 r←0 'No File Name' '' ⋄ →0
1688 :Else
1689 ⍝ Do Nothing, 'file_name' is not Empty.
1690 :End
1691 :ElseIf ~∆IsString file_name
1692 ⍝ Failure: 'file_name' Not Characters.
1693 r←0 'File Name Must Be Characters'file_name ⋄ →0
1694 :Else
1695 ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty.
1696 :End
1697
1698 ⍝ MAKE THE CALL
1699 :Trap 0
1700 :If 0=↑FileExists file_name
1701 ⍝ Failure: The File Name does not exists or is ill formed.
1702 r←0 'File Name Does Not Exists'file_name
1703 :Else
1704 ⍝ Success: The File Name Exists.
1705 ⎕USING←',mscorlib.dll'
1706 r←1(System.IO.File.ReadAllText(⊂,file_name))file_name
1707 :End
1708 :Else
1709 ⍝ Failure: Unexpected Error While Reading the File
1710 r←0 GetLastError file_name
1711 :EndTrap
1712 ∇
1713
1714 ∇ r←UFileReadAllLines file_name;⎕USING
1715 :Access Public Shared
1716 ⍝ Opens a text file, reads all lines of the file, and then closes the file.
1717 ⍝ The file handle is guaranteed to be closed by this method, even if exceptions are raised.
1718 ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name.
1719 ⍝
1720 ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile
1721 ⍝ r = 1 (lines) (File Name), if Successfull ⍝ lines is a vector where each element is a line
1722 ⍝ r = 0 (Error Description) (File Name), if Failure
1723 ⍝
1724 ⍝ Example: R ← DIO.TFileReadAllLines 'C:\MyTextFile.txt'
1725 ⍝ Success: R ← 1 (lines) 'C:\MyTextFile.txt'
1726 ⍝ Failure: R ← 0 (ERROR) 'C:\MyTextFile.txt'
1727 ⍝
1728 ⍝ Example: R ← DIO.TFileReadAllLines '' ⍝ To Select the File to Read Using the Open File Dialog
1729
1730 ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTERS
1731 :If 0=↑⍴file_name ⍝ Empty ?
1732 ⍝ Call the Open File Dialog
1733 :If 0=↑⍴file_name←DialogOpenFile
1734 ⍝ Failure: No File Name. Exit the program.
1735 r←0 'No File Name' '' ⋄ →0
1736 :Else
1737 ⍝ Do Nothing, 'file_name' is not Empty.
1738 :End
1739 :ElseIf ~∆IsString file_name
1740 ⍝ Failure: 'file_name' Not Characters.
1741 r←0 'File Name Must Be Characters'file_name ⋄ →0
1742 :Else
1743 ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty.
1744 :End
1745
1746 ⍝ MAKE THE CALL
1747 :Trap 0
1748 :If 0=↑FileExists file_name
1749 ⍝ Failure: The File Name does not exists or is ill formed.
1750 r←0 'File Name Does Not Exists'file_name
1751 :Else
1752 ⍝ Success: The File Name Exists.
1753 ⎕USING←',mscorlib.dll'
1754 r←1(System.IO.File.ReadAllLines(⊂,file_name))file_name
1755 :End
1756 :Else
1757 ⍝ Failure: Unexpected Error While Reading the File
1758 r←0 GetLastError file_name
1759 :EndTrap
1760 ∇
1761
1762 ∇ r←text UFileWrite file_name;⎕USING
1763 :Access Public Shared
1764 ⍝ Creates a New File, Writes The Specified Text to The File Using The UTF8 Encoding.
1765 ⍝ If The Target File Already Exists, it is Overwritten.
1766 ⍝ The file handle is guaranteed to be closed by this method, even if exceptions are raised.
1767 ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name.
1768 ⍝
1769 ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile
1770 ⍝ text = Vector of Characters
1771 ⍝ r = 1 (text) (File Name), if Successfull
1772 ⍝ r = 0 (ERROR) (File Name), if Failure
1773 ⍝
1774 ⍝ Example: R ← 'ABCDEF' DIO.UFileWrite 'C:\MyTextFile.txt'
1775 ⍝ Success: R ← 1 'ABCDEF' 'C:\MyTextFile.txt'
1776 ⍝ Failure: R ← 0 'Error Description' 'C:\MyTextFile.txt'
1777 ⍝
1778 ⍝ Example: R ← 'ABCDEF' DIO.UFileWrite '' ⍝ Select the File to Write Using the Open File Dialog
1779
1780 ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTERS
1781 :If 0=↑⍴file_name ⍝ Empty ?
1782 ⍝ Call the Open File Dialog
1783 :If 0=1↑⍴file_name←DialogOpenFile
1784 ⍝ Failure: No File Name. Exit the program.
1785 r←0 'No File Name' '' ⋄ →0
1786 :Else
1787 ⍝ Do Nothing, 'file_name' is not Empty.
1788 :End
1789 :ElseIf ~∆IsString file_name
1790 ⍝ Failure: File Name Not Characters.
1791 r←0 'File Name Must Be Characters'file_name ⋄ →0
1792 :Else
1793 ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty.
1794 :End
1795
1796 ⍝ VERIFY IF 'text' IS CHARACTERS
1797 :If ~∆IsString text
1798 ⍝ 'text' Must be Characters
1799 r←0 'The Text Must be Characters'file_name ⋄ →0
1800 :End
1801
1802 ⍝ MAKE THE CALL
1803 :Trap 0
1804 ⍝ STE.UTF8 = UTF-8 Encoding
1805 ⎕USING←',mscorlib.dll'
1806 System.IO.File.WriteAllText((⊂,file_name),(⊂,text),(System.Text.Encoding.UTF8))
1807 r←1 text file_name
1808 :Else
1809 ⍝ Failure: Unexpected Error While Writing the File
1810 r←0 GetLastError file_name
1811 :EndTrap
1812 ∇
1813
1814 ∇ r←lines UFileWriteAllLines file_name;⎕USING
1815 :Access Public Shared
1816 ⍝ Creates a new file, writes the specified individual lines to the file by using UTF8 encoding, and then closes the file.
1817 ⍝ If the target file already exists, it is overwritten.
1818 ⍝ The file handle is guaranteed to be closed by this method, even if exceptions are raised.
1819 ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name.
1820 ⍝
1821 ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile
1822 ⍝ lines = Vector where each element is a line
1823 ⍝ r = 1 (lines) (File Name), if Successfull
1824 ⍝ r = 0 (ERROR) (File Name), if Failure
1825 ⍝
1826 ⍝ Example: R ← ('line 1' 'line 2') DIO.UFileWriteAllLines 'C:\MyTextFile'
1827 ⍝ Success: R ← 1 ('line 1' 'line 2') 'C:\MyTextFile'
1828 ⍝ Failure: R ← 0 'Error Description' 'C:\MyTextFile'
1829 ⍝
1830 ⍝ Example: R ← ('line 1' 'line 2') DIO.UFileWriteAllLines '' ⍝ Select the File to Write Using the Open File Dialog
1831
1832 ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTERS
1833 :If 0=↑⍴file_name ⍝ Empty ?
1834 ⍝ Call the Open File Dialog
1835 :If 0=↑⍴file_name←DialogOpenFile
1836 ⍝ Failure: No File Name. Exit the program.
1837 r←0 'No File Name' '' ⋄ →0
1838 :Else
1839 ⍝ Do Nothing, 'file_name' is not Empty.
1840 :End
1841 :ElseIf ~∆IsString file_name
1842 ⍝ Failure: 'file_name' Not Characters.
1843 r←0 'File Name Must Be Characters'file_name ⋄ →0
1844 :Else
1845 ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty.
1846 :End
1847
1848 ⍝ MAKE THE CALL
1849 :Trap 0
1850 ⍝ STE.UTF8 = UTF-8 Encoding
1851 ⎕USING←',mscorlib.dll'
1852 System.IO.File.WriteAllLines((⊂,file_name),(⊂,lines),(System.Text.Encoding.UTF8))
1853 r←1 lines file_name
1854 :Else
1855 ⍝ Failure: Unexpected Error While Writing the File
1856 r←0 GetLastError file_name
1857 :EndTrap
1858 ∇
1859
1860 ∇ ZipCompressFile fileName;sfDir;zipArchive;⎕USING
1861 :Access Public
1862 ⍝ Compress a file using Syncfusion Zip namespace.
1863
1864 ⍝ Set ⎕USING for the Syncfusion dll.
1865 sfDir←sfDir←'Syncfusion/4.5/' ⍝ Location of the Syncfusion dll's
1866 ⎕USING←'Syncfusion.Compression.Zip,',sfDir,'Syncfusion.Compression.Base.dll'
1867
1868 ⍝ Get a ZipArchive
1869 zipArchive←⎕NEW ZipArchive
1870 zipArchive.DefaultCompressionLevel←zipArchive.DefaultCompressionLevel.Best
1871
1872 ⍝ Add the file to the ZipArchive
1873 {}zipArchive.AddFile(⊂,fileName)
1874
1875 ⍝ Change the fileName extension to zip
1876 fileName←((fileName⍳'.')↑fileName),'zip'
1877
1878 ⍝ Save the zipped file
1879 zipArchive.Save(⊂,fileName)
1880
1881 ⍝ Clean-up
1882 zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL
1883 ∇
1884
1885 :EndNamespace
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.