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

You are not allowed to attach a file to this page.