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