:Namespace netDIO ⍝ Disk And File Input/Output Utility Using .Net. ⍝ Check The Comments of Each Methods For More Information. ⍝ Requirements: v12.1 Unicode and .Net 2.0 ⍝ ⍝ Methods in DIO: ⍝ BFileRead - Reads The Contents of a Binary File Into a Character Vector Without Encoding. ⍝ BFileWrite - Creates a New File, Write The Specified Character Vector to The File Without Encoding. ⍝ DialogFolder - Selects a Directory with FolderBrowserDialog. ⍝ DialogOpenFile - Selects a File to Open With OpenFileDialog. ⍝ DialogSaveFile - Selects a File to Save With SaveFileDialog. ⍝ DirCopy - Copies a Directory And Its Contents. SubDirectories Are Not Copied. ⍝ DirCreate - Creates All Directories And Subdirectories as Specified By Path. ⍝ DirDelete - Deletes The Specified Directory And Any Subdirectories in The Directory. ⍝ DirExists - Determines Whether The Given Path Refers to an Existing Directory on Disk. ⍝ DirMove - Moves a Directory And Its Contents. SubDirectories Are Moved. ⍝ DirRename - Renames a Directory (This Method is Identical to DirMove). ⍝ DirSize - Returns The Size in Bytes of a Directory And It's Subdirectories. ⍝ EncodingDetector - Returns The Encoding of a File And it's Content. ⍝ FileCopy - Copies an Existing File to a New File. ⍝ FileDelete - Deletes a File. ⍝ FileExists - Verify if One or Many File(s) Exists. ⍝ FileMove - Moves a File to a New Location. ⍝ FileRename - Renames a File (This Method is Identical to FileMove). ⍝ FileSize - Returns The Size of a File in Bytes. ⍝ FileVersion - Returns The File Version of a File in Characters. ⍝ GetAttributes - Returns The Attributes of a File in Numeric And Litteral Form. ⍝ GetCreationTime - Returns The Creation Date And Time of a File as a DateTime Object. ⍝ GetDirCurrent - Returns The Current Directory Path. ⍝ GetDirectories - Returns The SubDirectories of a Directory. ⍝ GetDrives - Returns The Names of The Logical Drives on This Computer as ":\". ⍝ GetExtension - Returns The Extension of a Path String. ⍝ GetFiles - Returns The Names of Files in a Directory. ⍝ GetTempFileName - Returns a Uniquely Named Temporary File on Disk With Full Path. ⍝ GetTempPath - Returns The Path of The Current System's Temporary Folder. ⍝ NS2File - Function to save and retrieve a Namespace (NS) under a format compatible with C#. ⍝ SetAttributes - Sets The Attributes of a File. ⍝ SetDirCurrent - Sets The Current Directory Path. ⍝ TFileAppend - Appends The Specified Text to a File Using The Default Windows Code Page. ⍝ TFileRead - Reads The Contents of a Text File Using the Default Windows Code Page. ⍝ TFileWrite - Creates a New File, Writes The Text Using The Default Windows Code Page. ⍝ UFileAppend - Appends The Specified Text to a File Using The UTF-8 Encoding. ⍝ UFileAppendAllLines- Appends lines to a file using UTF8 encoding, and then closes the file. ⍝ UFileRead - Reads The Contents of a Unicode Text File. ⍝ UFileReadAllLines - Opens a text file, reads all lines of the file, and then closes the file. ⍝ UFileWrite - Creates a New File, Writes The Text Using The UTF8 Encoding. ⍝ UFileWriteAllLines - Creates a new file, writes the specified individual lines to the file by using UTF8 encoding. ⍝ ZipCompressFile - Compress a file using Syncfusion Zip namespace. ⍝ Version 1.0 February 2015, Pierre Gilbert ⍝ ⍝ Version 1.1 August 2015 ⍝ Method Added: UFileAppendAllLines (⎕IO ⎕ML ⎕WX)←1 3 3 ∆DBR←{ (~(∧\' '=⍵)∨(⌽∧\⌽' '=⍵))/⍵} ∆IsString←{ 0 2∊⍨10|⎕DR ⍵} ∇ r←BFileRead file_name;BA;FS;sink;size;⎕USING :Access Public Shared ⍝ Reads The Contents of a Binary File Into a Character Vector Without Encoding. ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name. ⍝ ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile ⍝ r = 1 (Bytes as 8 bits characters) (File Name) if Successfull ⍝ r = 0 (Error Description) (File Name) if Failure ⍝ ⍝ Example: R ← DIO.BFileRead 'C:\MyBinaryFile' ⍝ Success: R ← 1 (Bytes as 8 bits characters) 'C:\MyBinaryFile' ⍝ Failure: R ← 0 'File Name Does Not Exists' 'C:\MyBinaryFile' ⍝ ⍝ Example: R ← DIO.BFileRead '' ⍝ Select the File to Read Using the Open File Dialog ⍝ VERIFY IF 'file_name' IS EMPTY OR HAS CHARACTERS :If 0=↑⍴file_name←∊file_name ⍝ Empty ? ⍝ Call the Open File Dialog :If 0=↑⍴file_name←DialogOpenFile ⍝ Failure: No File Name. Exit the program. r←0 'No File Name' '' ⋄ →0 :Else ⍝ Do Nothing, 'file_name' is not Empty. :End :ElseIf ~∆IsString file_name ⍝ Failure: 'file_name' Not Characters. r←0 'File Name Must Be Characters'file_name ⋄ →0 :Else ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty. :End ⍝ MAKE THE CALL :Trap 0 ⍝ No Conversion is made with this Call. ⎕USING←',mscorlib.dll' r←1(⎕UCS System.IO.File.ReadAllBytes(⊂,file_name))file_name :Else ⍝ Failure: Unexpected Error While Reading the File r←0 GetLastError file_name :EndTrap ∇ ∇ r←data BFileWrite file_name;⎕USING :Access Public Shared ⍝ Creates a New File, Write The Specified Character Vector to The File Without Encoding ⍝ And Then Close the File. If the Target File Already Exists, It Is Overwritten. ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name. ⍝ ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile ⍝ data = Vector of 8 bits Characters ⍝ ⍝ r = 1 (data) (File Name) if Successfull ⍝ r = 0 (Error Description) (File Name) if Failure ⍝ ⍝ Example: R ← 'ABCDEF' DIO.BFileWrite 'C:\MyBinaryFile' ⍝ Success: R ← 1 'ABCDEF' 'C:\MyBinaryFile' ⍝ Failure: R ← 0 'File Name Does Not Exists' 'C:\MyBinaryFile' ⍝ ⍝ Example: R ← 'ABCDEF' DIO.BFileWrite '' ⍝ Select the File to Write Using the Open File Dialog ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTER :If 0=↑⍴file_name←∊file_name ⍝ Empty ? ⍝ Call the Open File Dialog :If 0=↑⍴file_name←DialogOpenFile ⍝ Failure: No File Name. Exit the program. r←0 'No File Name' '' ⋄ →0 :Else ⍝ Do Nothing, 'file_name' is not Empty. :End :ElseIf ~∆IsString file_name ⍝ Failure: File Name Not Characters. r←0 'The File Name Must Be Characters'file_name ⋄ →0 :Else ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty. :End ⍝ VERIFY IF 'data' IS CHARACTER :If ~∆IsString data ⍝ 'data' Must be 8 bits Characters r←0 'The Data Must be 8 bits Characters'file_name ⋄ →0 :End ⍝ MAKE THE CALL :Trap 0 ⎕USING←',mscorlib.dll' System.IO.File.WriteAllBytes((⊂,file_name),(⊂⎕UCS data)) r←1 data file_name :Else ⍝ Failure: Unexpected Error While Writing the File r←0 GetLastError file_name :End ∇ ∇ path←DialogFolder;DR;FBD;⎕USING :Access Public Shared ⍝ Selects a Directory with FolderBrowserDialog. :Trap 0 ⎕USING←',System.Windows.Forms.dll' FBD←⎕NEW System.Windows.Forms.FolderBrowserDialog DR←System.Windows.Forms.DialogResult ⍝ Shows the Dialog as Modal and Owned by the Current APL Process :If DR.OK=FBD.ShowDialog ⍬ path←FBD.SelectedPath :Else path←'' :End FBD.Dispose ⍝ With ShowDialog the form is not dispose automatically. :Else ⍝ Unexpected Error ⎕←GetLastError path←'' :EndTrap ∇ ∇ file_name←DialogOpenFile;DR;OFD;⎕USING :Access Public Shared :Trap 0 ⍝ Selects a File to Open With OpenFileDialog. ⎕USING←',System.Windows.Forms.dll' OFD←⎕NEW System.Windows.Forms.OpenFileDialog DR←System.Windows.Forms.DialogResult OFD.Title←'Select a File' OFD.CheckFileExists←0 OFD.InitialDirectory←'::{20D04FE0-3AEA-1069-A2D8-08002B30309D}' ⍝ My Computer ⍝ OFD.InitialDirectory←'::{450D8FBA-AD25-11D0-98A8-0800361B1103}' ⍝ My Documents ⍝ OFD.InitialDirectory←'::{208D2C60-3AEA-1069-A2D7-08002B30309D}' ⍝ My Network Places ⍝ Shows the Dialog as Modal and Owned by the Current APL Process :If DR.OK=OFD.ShowDialog ⍬ file_name←OFD.FileName :Else file_name←'' :End OFD.Dispose ⍝ With ShowDialog the form is not dispose automatically. :Else ⍝ Unexpected Error ⎕←GetLastError file_name←'' :EndTrap ∇ ∇ file_name←DialogSaveFile;DR;SFD;⎕USING :Access Public Shared ⍝ Selects a File to Save With SaveFileDialog. :Trap 0 ⎕USING←',System.Windows.Forms.dll' SFD←⎕NEW System.Windows.Forms.SaveFileDialog DR←System.Windows.Forms.DialogResult SFD.Title←'Save As' SFD.OverwritePrompt←1 SFD.InitialDirectory←'::{20D04FE0-3AEA-1069-A2D8-08002B30309D}' ⍝ My Computer ⍝ OFD.InitialDirectory←'::{450D8FBA-AD25-11D0-98A8-0800361B1103}' ⍝ My Documents ⍝ OFD.InitialDirectory←'::{208D2C60-3AEA-1069-A2D7-08002B30309D}' ⍝ My Network Places ⍝ Shows the Dialog as Modal and Owned by the Current APL Process :If DR.OK=SFD.ShowDialog ⍬ file_name←SFD.FileName :Else file_name←'' :End SFD.Dispose ⍝ With ShowDialog the form is not dispose automatically. :Else ⍝ Unexpected Error ⎕←GetLastError file_name←'' :EndTrap ∇ ∇ r←DirCopy(source_path target_path);i;inter;source_file;source_files;target_file;⎕USING :Access Public Shared ⍝ Copies a Directory And Its Contents to a New Location. SubDirectories Are Not Copied. ⍝ The Copy is Made Synchronously Without Animation. ⍝ ⍝ source_path = Source Directory Path ⍝ target_path = Target Directory Path ⍝ ⍝ r = 1 (source_path target_path), if successfull ⍝ r = 0 (ERROR) (source_path target_path), if failure ⍝ CHECK IF SOURCE DIRECTORY EXISTS :If 0=↑DirExists source_path r←0 'Source Directory Does Not Exists'(source_path target_path) ⋄ →0 :End ⍝ GET THE FILE NAMES OF THE SOURCE DIRECTORY :If 1=1↑inter←GetFiles source_path'*' 'TopDirectoryOnly' source_files←2⊃inter ⍝ File Names Without the Source Path :Else r←inter ⋄ →0 :End ⍝ CREATE A NEW TARGET DIRECTORY, IF NECESSARY :If 0=↑DirExists target_path :AndIf 0=↑DirCreate target_path r←0 'Not Able to Create the Target Directory'target_path ⋄ →0 :End ⍝ COPY THE FILES FROM THE SOURCE TO THE TARGET DIRECTORY :Trap 0 ⎕USING←',mscorlib.dll' :For i :In ⍳⍴source_files ⍝ File Name With the Source Directory Path source_file←System.IO.Path.Combine((⊂,source_path),(⊂,i⊃source_files)) ⍝ File Name With the Target Directory Path target_file←System.IO.Path.Combine((⊂,target_path),(⊂,i⊃source_files)) ⍝ 1 = Overwrite Permitted System.IO.File.Copy((⊂,source_file),(⊂,target_file),1) :EndFor r←1(source_path target_path) :Else ⍝ Unexpected Error r←0 GetLastError(source_path target_path) :End ∇ ∇ r←DirCreate path;⎕USING :Access Public Shared ⍝ Creates All Directories And Subdirectories as Specified By Path. ⍝ ⍝ r = 1 (path), if successfull ⍝ r = 0 (ERROR) (path), if Failure :If ~∆IsString path←∊path r←0 'Argument Must Be Characters'path ⋄ →0 :End ⍝ MAKE THE CALL :Trap 0 ⎕USING←',mscorlib.dll' r←1((System.IO.Directory.CreateDirectory(⊂,path)).FullName) :Else ⍝ Unexpected Error While Creating the Directory r←0 GetLastError path :EndTrap ∇ ∇ r←DirDelete path;⎕USING :Access Public Shared ⍝ Deletes The Specified Directory And Any Subdirectories in The Directory. ⍝ Will Erase the Directory Even if Not Empty. ⍝ ⍝ r = 1 (path), if Successfull ⍝ r = 0 (ERROR) (path), if Failure ⍝ CHECK IF DIRECTORY EXISTS :If 0=↑DirExists path ⍝ The Directory Does Not Exists. No Need to Erase It. r←1 path ⋄ →0 :End ⍝ MAKE THE CALL :Trap 0 ⎕USING←',mscorlib.dll' System.IO.Directory.Delete((⊂,path),1) r←1 path :Else ⍝ Unexpected Error While Deleting the Directory r←0 GetLastError path :EndTrap ∇ ∇ r←DirExists path;⎕USING :Access Public Shared ⍝ Determines Whether The Given Path Refers to an Existing Directory on Disk. ⍝ ⍝ r = 1 (path), if Exists ⍝ r = 0 (ERROR) (path), if Failure :If ~∆IsString path←∊path r←0 'Argument Must Be Characters'path ⋄ →0 :End ⍝ MAKE THE CALL :Trap 0 ⎕USING←',mscorlib.dll' r←(System.IO.Directory.Exists(⊂,path))path :Else ⍝ Unexpected Error r←0 GetLastError path :End ∇ ∇ r←DirMove(source_path target_path);⎕USING :Access Public Shared ⍝ Moves a Directory And Its Contents to a New Location. SubDirectories Are Moved. ⍝ This Method is Identical to DirRename. ⍝ ⍝ source_path = Source Directory Path ⍝ target_path = Target Directory Path ⍝ ⍝ r = 1 (source_path target_path), if successfull ⍝ r = 0 (ERROR) (source_path target_path), if failure ⍝ CHECK IF SOURCE DIRECTORY EXIST :If 0=↑DirExists source_path r←0 'Source Directory Does Not Exists'(source_path target_path) ⋄ →0 :End ⍝ CHECK IF TARGET DIRECTORY EXIST :If 1=↑DirExists target_path r←0 'Target Directory Must Not Exists'(source_path target_path) ⋄ →0 :End ⍝ MAKE THE CALL :Trap 0 ⎕USING←',mscorlib.dll' System.IO.Directory.Move((⊂,source_path),(⊂,target_path)) r←1(source_path target_path) :Else ⍝ Unexpected Error While Moving the Directory r←0 GetLastError(source_path target_path) :EndTrap ∇ ∇ r←DirRename(actual_path new_path);⎕USING :Access Public Shared ⍝ Renames a Directory (This Method is Identical to DirMove). ⍝ ⍝ actual_path = Actual Directory Path ⍝ new_path = New Directory Path ⍝ ⍝ r = 1 (actual_path new_path), if successfull ⍝ r = 0 (ERROR) (arg), if failure ⍝ CHECK IF ACTUAL DIRECTORY EXIST :If 0=↑DirExists actual_path r←0 'Actual Directory Does Not Exists'(actual_path new_path) ⋄ →0 :End ⍝ CHECK IF NEW DIRECTORY EXIST :If 1=↑DirExists new_path r←0 'New Directory Must Not Exists'(actual_path new_path) ⋄ →0 :End ⍝ MAKE THE CALL :Trap 0 ⎕USING←',mscorlib.dll' System.IO.Directory.Move((⊂,actual_path),(⊂,new_path)) r←1(actual_path new_path) :Else ⍝ Unexpected Error While Renaming the Directory r←0 GetLastError(actual_path new_path) :EndTrap ∇ ∇ r←DirSize arg;directory_info;files_info;i;infos;IODI;path;search_pattern;test;⎕USING :Access Public Shared ⍝ Returns The Size in Bytes of a Directory And It's Subdirectories. ⍝ ⍝ arg[1] = The fully qualified Directory Path ⍝ arg[2] = [Optional] The search string to match against the names of directories found ⍝ r = 1 (Size in Bytes of the Directories), if Successfull ⍝ r = 0 (ERROR) (arg), if Failure ⍝ ⍝ EXAMPLES: r ← DIO.DirSize Path [SearchPattern] ⍝ ⍝ EXAMPLES: ⍝ r←DIO.DirSize 'C:\MyDir' ⍝ Size of All Directories of 'C:\MyDir' ⍝ r←DIO.DirSize 'C:\MyDir' 'p*' ⍝ Size of 'C:\MyDir' and SubDirectories Name beginning with 'p' ⍝ PARSE THE ARGUMENT :If 2=≡arg ⍝ Nested ? :Select ↑⍴arg :CaseList 0 1 path←arg←∊arg search_pattern←'*' :Case 2 (path search_pattern)←arg :Else r←0 'Argument Ill Formed'arg ⋄ →0 :EndSelect :ElseIf ∆IsString arg ⍝ Characters ? path←arg search_pattern←'*' :Else r←0 'Argument Ill Formed'arg ⋄ →0 :End ⍝ TEST IF TOP DIRECTORY EXISTS :If 0=↑DirExists path r←0 'Directory Does Not Exists'arg ⋄ →0 :End ⍝ FIND SIZE OF TOP DIRECTORY AND SUBDIRECTORIES :Trap 0 ⎕USING←',mscorlib.dll' ⍝ Directory Structure for Top Directory directory_info←,⎕NEW System.IO.DirectoryInfo(⊂,path) ⍝ Iterate to find the subdirectories's size r←0 :While 0≠⍴directory_info IODI←1⊃directory_info directory_info←1↓directory_info :Trap 0 infos←IODI.GetFileSystemInfos(⊂,search_pattern) :Else ⍝ Directory That Can't be Access 'Directory Not Accessible: ',IODI.FullName :Continue :End :If 0≠⍴infos ⍝ Not an empty directory files_info←(test←infos.GetType∊System.IO.FileInfo)/infos :For i :In ⍳⍴files_info :Trap 0 r+←{Convert.ToDouble ⍵.Length}(i⊃files_info) :Else ⍝ File That Can't be Read 'File Not Accessible: ',(i⊃files_info).FullName :Continue :EndTrap :End directory_info←directory_info,(~test)/infos :Else ⍝ Do Nothing - Empty Directory :End :EndWhile r←1 r :Else ⍝ Unexpected Error r←0 GetLastError arg :End ∇ ∇ r←EncodingDetector file_name;BA;bytes;ENC;inter;I2;string;z;⎕USING :Access Public Shared ⍝ Returns The Encoding of a File And it's Content. ⍝ Detected From Their BOM: UTF-32LE, UTF-32BE, UTF-8, UTF-16LE, UTF-16BE. ⍝ An UTF-8 File Without BOM or ASCII Encoding Can Also be Detected. ⍝ This method could also be use to read a text file when the encoding is unknown. ⍝ ⍝ file_name = Fully Qualified File Name ⍝ r = 1 (ENCODING NAME) (file content) (file_name),if Success ⍝ r = 0 (Exception) file_name ,if Failure :Trap 0 ⍝ GET THE BINARY VALUE OF THE FILE :If 1=↑inter←BFileRead file_name bytes←2⊃inter ⍝ Character Vector BA←⎕UCS bytes ⍝ Numeric Vector ⍝ Value file_name←3⊃inter ⍝ In Case file_name Was Empty :Else r←0(2⊃inter)file_name ⋄ →0 :End ⎕USING←',mscorlib.dll' ⍝ DETECTION WITH THE BYTE ORDER MARK (BOM) :If 255 254 0 0≡4⍴BA ⍝ 0xFF FE 00 00 ⍝ UTF-32LE Detected ENC←⎕NEW System.Text.UTF32Encoding(0 1 1) string←ENC.GetString((⊂,BA),4,¯4+⍴BA) r←1 'UTF-32LE'string file_name ⋄ →0 :ElseIf 0 0 254 255≡4⍴BA ⍝ 0x00 00 FE FF ⍝ UTF-32BE Detected ENC←⎕NEW System.Text.UTF32Encoding(1 1 1) string←ENC.GetString((⊂,BA),4,¯4+⍴BA) r←1 'UTF-32BE'string file_name ⋄ →0 :ElseIf 239 187 191≡3⍴BA ⍝ 0xEF BB BF ⍝ UTF-8 Detected ENC←⎕NEW System.Text.UTF8Encoding(1 1) string←ENC.GetString((⊂,BA),3,¯3+⍴BA) r←1 'UTF-8'string file_name ⋄ →0 :ElseIf 255 254≡2⍴BA ⍝ 0xFF FE ⍝ UTF-16LE Detected ENC←⎕NEW System.Text.UnicodeEncoding(0 1 1) string←ENC.GetString((⊂,BA),2,¯2+⍴BA) r←1 'UTF-16LE'string file_name ⋄ →0 :ElseIf 254 255≡2⍴BA ⍝ 0xFE FF ⍝ UTF-16BE Detected ENC←⎕NEW System.Text.UnicodeEncoding(1 1 1) string←ENC.GetString((⊂,BA),2,¯2+⍴BA) r←1 'UTF-16BE'string file_name ⋄ →0 :End ⍝ If there is no BOM the only way to test is to try to Decode ⍝ and verify if there is an error or not doing so. ⍝ *** DECODE TEST FOR UTF-8 WITHOUT BOM *** ⍝ CREATE THE MULTIBYTE SEQUENCES →(∨/254 255∊BA)/ASCII ⍝ 0xFE and 0xFF not permitted z←(BA≥128)∧(BA≤191) ⍝ Multibyte are 1 and BA≤127 are 0 z←z+2×(BA≥192)∧(BA≤223) ⍝ 2 Multibyte Indicator z←z+3×(BA≥224)∧(BA≤239) ⍝ 3 Multibyte Indicator z←z+4×(BA≥240)∧(BA≤247) ⍝ 4 Multibyte Indicator z←z+5×(BA≥248)∧(BA≤251) ⍝ 5 Multibyte Indicator z←z+6×(BA≥252)∧(BA≤253) ⍝ 6 Multibyte Indicator ⍝ CONVERT TO CHARACTERS z←⎕UCS z ∆SS←{⎕ML←3 ⍝ Approx alternative to xutils' ss. srce find repl←,¨⍵ ⍝ Source, find and replace vectors. cvex←{(+\find⍷⍵)⊂⍵}find,srce ⍝ Partitioned at find points. (⍴repl)↓∊{repl,(⍴find)↓⍵}¨cvex} ⍝ REPLACE THE MULTIBYTE SEQUENCE WITH ZERO'S z←∆SS z(⎕UCS 2 1)(⎕UCS 0 0) ⍝ 2 Multibyte Sequences z←∆SS z(⎕UCS 3 1 1)(⎕UCS 0 0 0) ⍝ 3 Multibyte Sequences z←∆SS z(⎕UCS 4 1 1 1)(⎕UCS 0 0 0 0) ⍝ 4 Multibyte Sequences z←∆SS z(⎕UCS 5 1 1 1 1)(⎕UCS 0 0 0 0 0) ⍝ 5 Multibyte Sequences z←∆SS z(⎕UCS 6 1 1 1 1 1)(⎕UCS 0 0 0 0 0 0) ⍝ 6 Multibyte Sequences ⍝ CONVERT TO NUMBERS z←⎕UCS z ⍝ THE SUM SHOULD BE 0 IF UTF-8 ENCODING :If 0=+/z ⍝ UTF-8 Encoding With No BOM Detected ENC←⎕NEW System.Text.UTF8Encoding(1 1) string←ENC.GetString((⊂,BA),0,⍴BA) r←1 'UTF-8NoBOM'string file_name ⋄ →0 :End ASCII: ⍝ *** DECODE TEST FOR ASCII *** :If 1=∧/BA≤127 ⍝ ASCII DETECTED ENC←⎕NEW System.Text.ASCIIEncoding string←ENC.GetString((⊂,BA),0,⍴BA) r←1 'ASCII'string file_name ⋄ →0 :End ⍝ When All The Other Test Fails, We Use Then The Default Page of The Computer ENC←System.Text.Encoding string←ENC.Default.GetString((⊂,BA),0,⍴BA) r←1 'DEFAULT'string file_name :Else ⍝ Unexpected Error While Searching the Encoding r←0 GetLastError file_name :EndTrap ∇ ∇ r←FileCopy(source_FileName target_FileName);target_path;⎕USING :Access Public Shared ⍝ Copies an Existing File to a New File. ⍝ Overwriting a file of the same name is allowed. ⍝ ⍝ r = 1 (source_FileName target_FileName), if successfull ⍝ r = 0 (ERROR) (source_FileName target_FileName), if failure ⍝ CHECK IF SOURCE FILE EXISTS :If 0=↑FileExists source_FileName r←0 'Source File Does Not Exists'(source_FileName target_FileName) ⋄ →0 :End ⎕USING←',mscorlib.dll' ⍝ GET THE TARGET DIRECTORY FROM THE TARGET FILE NAME :Trap 0 target_path←System.IO.Path.GetDirectoryName(⊂,target_FileName) :Else r←0 'Not Able to Get the Target Directory'(source_FileName target_FileName) ⋄ →0 :EndTrap ⍝ CREATE A NEW TARGET DIRECTORY, IF IT DOES NOT EXISTS :If 0=↑DirExists target_path :AndIf 0=↑DirCreate target_path r←0 'Not Able to Create the Target Directory'(source_FileName target_FileName) ⋄ →0 :End ⍝ COPY THE FILE :Trap 0 System.IO.File.Copy((⊂,source_FileName),(⊂,target_FileName),1) ⍝ 1 = Overwrite Permitted r←1(source_FileName target_FileName) :Else r←0 GetLastError(source_FileName target_FileName) :End ∇ ∇ r←FileDelete file_name;⎕USING :Access Public Shared ⍝ Deletes a File. ⍝ ⍝ file_name = Fully Qualified File Name ⍝ r = 1 (file_name), if Successfull ⍝ r = 0 (ERROR) (file_name) ,if Failure :If ~∆IsString file_name ⍝ Failure: 'file_name' Not Characters. r←0 'File Name Must Be Characters'file_name ⋄ →0 :End ⍝ MAKE THE CALL :Trap 0 ⎕USING←',mscorlib.dll' System.IO.File.Delete(⊂,file_name) r←1 file_name :Else r←0 GetLastError file_name :EndTrap ∇ ∇ r←FileExists file_name;i;⎕USING :Access Public Shared ⍝ Verify if One or Many File(s) Exists. ⍝ ⍝ file_name = One or More Fully Qualified File Name(s) ⍝ r = 1 if Exists, 0 Otherwise ⍝ ⍝ Example: r ← DIO.FileExists 'C:\MyDir\MyFile1.ext' 'C:\MyDir\MyFile2.ext' ⍝ PARSE THE ARGUMENT :If 2=≡file_name ⍝ Nested ? ⍝ Nested File Name. Replace the Non-Character Names with '' file_name[{(1=⍵)/⍳⍴,⍵}~∆IsString¨file_name]←⊂,'' :ElseIf ∆IsString file_name ⍝ Characters Not Nested ? ⍝ Vector of Characters, only one File Name file_name←,⊂,file_name :Else ⍝ Vector of Non-Character. No need to make the Call. r←(⍴,file_name)⍴0 ⋄ →0 :End ⍝ MAKE THE CALL :Trap 0 ⎕USING←',mscorlib.dll' r←System.IO.File.Exists¨⊂¨,¨file_name ⍝ File(s) Exist(s) or Name(s) are Valid :Else ⍝ Failure: Unexpected Error. ⎕←'#.UTIL.DIO.FileExists Error: ' ⎕←GetLastError :End ∇ ∇ r←FileMove(source_FileName target_FileName);target_path;⎕USING :Access Public Shared ⍝ Moves a File to a New Location. ⍝ Target Directory Is Created if Inexistant. ⍝ Target File Name Is Deleted if Existant. ⍝ ⍝ r = 1 (source_FileName target_FileName), if successfull ⍝ r = 0 (ERROR) (source_FileName target_FileName), if failure ⍝ CHECK IF SOURCE FILE EXISTS :If 0=↑FileExists source_FileName r←0 'Source File Does Not Exists'(source_FileName target_FileName) ⋄ →0 :End ⎕USING←',mscorlib.dll' ⍝ GET THE TARGET DIRECTORY FROM THE TARGET FILE NAME :Trap 0 target_path←System.IO.Path.GetDirectoryName(⊂,target_FileName) :Else r←0 'Not Able to Obtain the Target Directory'(source_FileName target_FileName) ⋄ →0 :EndTrap ⍝ CREATE A NEW TARGET DIRECTORY, IF IT DOES NOT EXISTS :If 0=↑DirExists target_path :AndIf 0=↑DirCreate target_path r←0 'Not Able to Create the Target Directory'(source_FileName target_FileName) ⋄ →0 :End ⍝ ERASE THE TARGET FILE IF EXISTING :If 1=↑FileExists target_FileName :AndIf 0=↑FileDelete target_FileName r←0 'Not Able to Erase the Target File'(source_FileName target_FileName) ⋄ →0 :End ⍝ MOVE THE FILE :Trap 0 System.IO.File.Move((⊂,source_FileName),(⊂,target_FileName)) r←1(source_FileName target_FileName) :Else ⍝ Unexpected Error While Moving the File r←0 GetLastError(source_FileName target_FileName) :End ∇ ∇ r←FileRename(old_FileName new_FileName);target_path;⎕USING :Access Public Shared ⍝ Renames a File (This Method is Identical to FileMove). ⍝ New File Name Directory Is Created if Inexistant. ⍝ New File Name Is Deleted if Existant. ⍝ ⍝ r = 1 (old_FileName new_FileName), if successfull ⍝ r = 0 (ERROR) (old_FileName new_FileName), if failure ⍝ CHECK IF OLD FILE NAME EXISTS :If 0=↑FileExists old_FileName r←0 'Old File Name Does Not Exists'(old_FileName new_FileName) ⋄ →0 :End ⎕USING←',mscorlib.dll' ⍝ GET THE TARGET DIRECTORY FROM THE NEW FILE NAME :Trap 0 target_path←System.IO.Path.GetDirectoryName(⊂,new_FileName) :Else r←0 'Not Able to Obtain the New File Directory'(old_FileName new_FileName) ⋄ →0 :EndTrap ⍝ CREATE A NEW TARGET DIRECTORY, IF IT DOES NOT EXISTS :If 0=↑DirExists target_path :AndIf 0=↑DirCreate target_path r←0 'Not Able to Create the New File Directory'(old_FileName new_FileName) ⋄ →0 :End ⍝ ERASE THE TARGET FILE IF EXISTING :If 1=↑FileExists new_FileName :AndIf 0=↑FileDelete new_FileName r←0 'Not Able to Erase the New File'(old_FileName new_FileName) ⋄ →0 :End ⍝ RENAME THE FILE :Trap 0 System.IO.File.Move((⊂,old_FileName),(⊂,new_FileName)) r←1(old_FileName new_FileName) :Else ⍝ Unexpected Error While Moving the File r←0 GetLastError(old_FileName new_FileName) :End ∇ ∇ r←FileSize file_name;IOFI;⎕USING :Access Public Shared ⍝ Returns The Size of a File in Bytes. ⍝ ⍝ file_name = Fully Qualified File Name ⍝ r = 1 (Size in Bytes of the File) file_name, if Successfull ⍝ r = 0 (ERROR) file_name, if Failure. ⍝ ⍝ EXAMPLE: r ← DIO.FileSize 'C:\MyDir\MyFile.ext' ⍝ CHECK IF FILE EXISTS :If 0=↑FileExists file_name r←0 'File Does Not Exists'file_name ⋄ →0 :End ⍝ MAKE THE CALL :Trap 0 ⎕USING←',mscorlib.dll' IOFI←⎕NEW System.IO.FileInfo(⊂,file_name) r←1(Convert.ToDouble(IOFI.Length))file_name :Else ⍝ Unexpected Error While Obtaining The File Size r←0 GetLastError file_name :End ∇ ∇ r←FileVersion file_name;FVI;v;⎕USING :Access Public Shared ⍝ Returns The File Version of a File in Characters. ⍝ ⍝ file_name = Fully Qualified File Name ⍝ r = 1 (Version in Characters or '' if empty) (File Name) if Successfull ⍝ r = 0 (Error Description) (File Name) if Failure ⍝ CHECK IF FILE EXISTS :If 0=↑FileExists file_name r←0 'File Does Not Exists'file_name ⋄ →0 :End ⍝ MAKE THE CALL :Trap 0 6 ⍝ FileVersionInfo Object is Returned ⎕USING←',System.dll' FVI←System.Diagnostics.FileVersionInfo.GetVersionInfo(⊂,file_name) ⍝ The File Version is a Vector of Characters r←1(FVI.FileVersion)file_name :Case 6 ⍝ VALUE ERROR. There is No File Version Available r←1 ''file_name :Else ⍝ Unexpected Error While Obtaining The File Version r←0 GetLastError file_name :EndTrap ∇ ∇ r←GetAttributes file_name;attributes;inter;⎕USING :Access Public Shared ⍝ Returns The Attributes of a File in Numeric And Litteral Form. ⍝ ⍝ file_name = Fully Qualified File Name ⍝ r = 1 (Numeric Attributes) (Litteral Attributes) (file_name), if Successfull ⍝ r = 0 (Error Description) (file_name), if Failure ⍝ ⍝ Attributes Are: ⍝ 1 = ReadOnly ⍝ 2 = Hidden ⍝ 4 = System ⍝ 16 = Directory ⍝ 32 = Archive ⍝ 64 = Device ⍝ 128 = Normal ⍝ 256 = Temporary ⍝ 512 = SparseFile ⍝ 1024 = ReparsePoint ⍝ 2048 = Compressed ⍝ 4096 = Offline ⍝ 8192 = NotContentIndexed ⍝ 16384 = Encrypted ⍝ CHECK IF FILE EXISTS :If 0=↑FileExists file_name r←0 'File Does Not Exists'file_name ⋄ →0 :End ⍝ MAKE THE CALL :Trap 0 ⎕USING←',mscorlib.dll' attributes←System.IO.File.GetAttributes(⊂,file_name) :Else ⍝ Unexpected Error While Obtaining the Attributes r←0 GetLastError file_name ⋄ →0 :End ⍝ FIND THE NUMERICAL VALUES ATTRIBUTES inter←⌽(15⍴2)⊤attributes.value__ inter←,2*(-⎕IO)+inter/⍳⍴inter ⍝ RETURNED VALUE IS THE NUMERICAL AND LITTERAL ATTRIBUTES r←1 inter(attributes.ToString ⍬)file_name ∇ ∇ r←GetCreationTime file_name;⎕USING :Access Public Shared ⍝ Returns The Creation Date And Time of a File as a DateTime Object. ⍝ ⍝ file_name = Fully Qualified File Name ⍝ r = 1 ([.net:DateTime]) (file_name), if Successfull ⍝ r = 0 (ERROR) (file_name) ,if Failure ⍝ CHECK IF FILE EXISTS :If 0=↑FileExists file_name r←0 'File Does Not Exists'file_name ⋄ →0 :End ⍝ MAKE THE CALL :Trap 0 ⎕USING←',mscorlib.dll' r←1(System.IO.File.GetCreationTime(⊂,file_name))file_name :Else r←0 GetLastError file_name :EndTrap ∇ ∇ r←GetDirCurrent;⎕USING :Access Public Shared ⍝ Returns The Current Directory Path. ⍝ ⍝ r = 1 (Current Directory), if Successfull ⍝ r = 0 (ERROR), if Failure ⍝ MAKE THE CALL :Trap 0 ⎕USING←',mscorlib.dll' r←1(System.IO.Directory.GetCurrentDirectory) :Else ⍝ Unexpected Error r←0 GetLastError :End ∇ ∇ r←GetDirectories arg;arg_save;path;search_option;search_pattern;⎕USING :Access Public Shared ⍝ Returns The SubDirectories of a Directory. ⍝ ⍝ USAGE: r ← DIO.GetDirectories Path [SearchPattern] [SearchOption] ⍝ ⍝ Path = Directory Path to search ⍝ SearchPattern = The search string to match against the names of directories found ⍝ SearchOption = 'TopDirectoryOnly' to search only the specified directory (default) ⍝ 'AllDirectories' to search all subdirectories ⍝ r = 1 (Nested vector with the fully qualified directory names) arg, if Successfull ⍝ r = 0 (ERROR) arg, if Failure ⍝ ⍝ EXAMPLES: ⍝ r←DIO.GetDirectories 'C:\MyDir' '*' 'AllDirectories' ⍝ All Subdirectories of 'C:\MyDir' ⍝ r←DIO.GetDirectories 'C:\MyDir' 'p*' ⍝ Subdirectories of 'C:\MyDir' beginning with 'p' ⍝ PARSE THE ARGUMENT search_pattern←'*.*' :If 2=≡arg_save←arg ⍝ Nested ? :Select ↑⍴arg :CaseList 0 1 path←∊arg :Case 2 (path search_pattern)←arg :Case 3 (path search_pattern search_option)←arg :Else r←0 'Argument Ill Formed'arg ⋄ →0 :EndSelect :ElseIf ∆IsString arg ⍝ Characters ? path←arg :Else r←0 'Argument Ill Formed'arg ⋄ →0 :End ⍝ TEST IF DIRECTORY EXISTS :If 0=↑DirExists path r←0 'Directory Does Not Exists'arg_save ⋄ →0 :End ⎕USING←',mscorlib.dll' ⍝ TEST IF SEARCH OPTION IS VALID :If 0≠⎕NC'search_option' ⍝ Exists ? ⍝ search_option is defined :If ∆IsString search_option ⍝ Characters ? :If 1=(⊂search_option)∊System.IO.SearchOption.⎕NL ¯2 ⍝ Valid Search Option search_option←⍎'System.IO.SearchOption.',search_option (3⊃arg)←search_option :Else ⍝ Search Option is not Valid ⍝ 'AllDirectories' or 'TopDirectoryOnly' are Valid r←0 'Search Option is Not Valid'arg ⋄ →0 :End :Else ⍝ Search Option is not Valid ⍝ 'AllDirectories' or 'TopDirectoryOnly' are Valid r←0 'Search Option is Not Valid'arg ⋄ →0 :End :Else ⍝ search_option is not defined search_option←System.IO.SearchOption.TopDirectoryOnly :End ⍝ MAKE THE CALL :Trap 0 r←1(System.IO.Directory.GetDirectories((⊂,path),(⊂,search_pattern),(search_option)))arg :Else ⍝ Failure: Unexpected Error r←0 GetLastError arg :End ∇ ∇ r←GetDrives;⎕USING :Access Public Shared ⍝ Returns The Names of The Logical Drives on This Computer in The Form ":\". ⍝ ⍝ r = 1 (Logical Drives), if Successfull ⍝ r = 0 (ERROR), if Failure ⍝ MAKE THE CALL :Trap 0 ⎕USING←',mscorlib.dll' r←1(System.IO.Directory.GetLogicalDrives) :Else ⍝ Unexpected Error While Obtaining the Logical Drives r←0 GetLastError :End ∇ ∇ r←GetExtension file_name;⎕USING :Access Public Shared ⍝ Returns The Extension of a Path String. ⍝ Will Return the extension of a file name even if the file does not exists ⍝ ⍝ file_name = Fully Qualified File Name ⍝ r = (Extension), if Successfull ⍝ r = '', if Failure ⍝ MAKE THE CALL :Trap 0 ⎕USING←',mscorlib.dll' r←System.IO.Path.GetExtension(⊂,file_name) :Else ⍝ Unexpected Error While Getting the Extension r←'' :End ∇ ∇ r←GetFiles arg;arg_save;path;rLong;rShort;search_option;search_pattern;⎕USING :Access Public Shared ⍝ Returns The Names of Files in a Directory. ⍝ ⍝ USAGE: r ← DIO.GetFiles Path [SearchPattern] [SearchOption] ⍝ ⍝ Path = Directory Path to search ⍝ SearchPattern = The search string to match against the names of files found ⍝ SearchOption = 'TopDirectoryOnly' to search only the specified directory (default) ⍝ 'AllDirectories' to search all subdirectories ⍝ ⍝ r = 1 (Nested vector with the SHORT files names) (Nested vector with the LONG files names) arg, if Successfull ⍝ r = 0 (ERROR) arg, if Failure ⍝ ⍝ EXAMPLES: ⍝ r←DIO.GetFiles 'C:\MyDir' '*' 'AllDirectories' ⍝ All Files in All Subdirectories of 'C:\MyDir' ⍝ r←DIO.GetFiles 'C:\MyDir' 'p*' ⍝ Files of 'C:\MyDir' beginning with 'p' ⍝ r←DIO.GetFiles 'C:\MyDir' '*.exe' ⍝ Files of 'C:\MyDir' ending with '.exe' ⍝ PARSE THE ARGUMENT search_pattern←'*.*' :If 2=≡arg_save←arg :Select ↑⍴arg :CaseList 0 1 path←∊arg :Case 2 (path search_pattern)←arg :Case 3 (path search_pattern search_option)←arg :Else r←0 'Argument Ill Formed'arg_save ⋄ →0 :EndSelect :ElseIf ∆IsString arg path←arg :Else r←0 'Argument Ill Formed'arg_save ⋄ →0 :End ⍝ TEST IF DIRECTORY EXISTS :If 0=↑DirExists path r←0 'Directory does not Exists'arg_save ⋄ →0 :End ⎕USING←',mscorlib.dll' ⍝ TEST IF SEARCH OPTION IS VALID :If 0≠⎕NC'search_option' ⍝ Exists ? ⍝ search_option is defined :If ∆IsString search_option ⍝ Characters ? :If 1=(⊂search_option)∊System.IO.SearchOption.⎕NL ¯2 ⍝ Valid Search Option search_option←⍎'System.IO.SearchOption.',search_option (3⊃arg)←search_option :Else ⍝ Search Option is not Valid ⍝ 'AllDirectories' or 'TopDirectoryOnly' are Valid r←0 'Search Option is Not Valid'arg ⋄ →0 :End :Else ⍝ Search Option is not Valid ⍝ 'AllDirectories' or 'TopDirectoryOnly' are Valid r←0 'Search Option is Not Valid'arg ⋄ →0 :End :Else ⍝ Default Value search_option←System.IO.SearchOption.TopDirectoryOnly :End ⍝ MAKE THE CALL :Trap 0 rLong←System.IO.Directory.GetFiles((⊂,path),(⊂,search_pattern),(search_option)) :If 0≠↑⍴rLong rShort←System.IO.Path.GetFileName¨⊂¨,¨rLong ⍝ To Removes the Directory Path rLong←∆DBR¨⊂[2]rLong[⍋rLong←⊃[2]rLong;] ⍝ Sorts the Names rShort←∆DBR¨⊂[2]rShort[⍋rShort←⊃[2]rShort;] ⍝ Sorts the Names :Else ⍝ The directory is empty rShort←rLong :End r←1 rShort rLong arg_save :Else ⍝ Failure: Unexpected Error r←0 GetLastError arg_save :End ∇ ∇ r←GetLastError :Access Public Shared ⍝ Returns the Last Error :If 90=⎕EN r←'EXCEPTION: ',⎕EXCEPTION.Message :Else r←(1⊃⎕DM),': ',∆DBR(2⊃⎕DM) :EndIf ∇ ∇ sa←GetNetStringArray apl;i;⎕USING ⍝ To get a .Net String[] ⎕USING←'System' apl←,apl sa←Array.CreateInstance(Type.GetType⊂'System.String')(⍴apl) :For i :In ⍳⍴apl sa.SetValue(i⊃apl)(i-1) :EndFor ∇ ∇ r←GetTempFileName;⎕USING :Access Public Shared ⍝ Returns a Uniquely Named, Zero-Byte Temporary File on Disk With Full Path. ⍝ ⍝ r = 1 (File Name), if Successfull ⍝ r = 0 (ERROR), if Failure ⍝ MAKE THE CALL :Trap 0 ⎕USING←',mscorlib.dll' r←1(System.IO.Path.GetTempFileName) :Else ⍝ Unexpected Error r←0 GetLastError :EndTrap ∇ ∇ r←GetTempPath;⎕USING :Access Public Shared ⍝ Returns The Path of The Current System's Temporary Folder. ⍝ ⍝ r = 1 (Path), if Successfull ⍝ r = 0 (ERROR), if Failure ⍝ MAKE THE CALL :Trap 0 ⎕USING←',mscorlib.dll' r←1(System.IO.Path.GetTempPath) :Else ⍝ Unexpected Error r←0 GetLastError :EndTrap ∇ ∇ r←{nsOrKey}NS2File fileName;binaryFormatter;case;fileStream;hashTable;key;ns;val;⎕USING :Access Public Shared ⍝ Function to save and retrieve a Namespace (NS) under a format compatible with C#. ⍝ Based on: http://forums.dyalog.com/viewtopic.php?f=18&t=213&p=856 ⍝ ⍝ fileName = Fully qualified file name ⍝ nsOrKey = Namespace to save under fileName ⍝ = Key of retreived NS saved under fileName ⍝ = if empty returns the full NS saved under fileName ⎕USING←0⍴⊂'' ⍝ To Speed up ⎕NC ⍝ Check for nsOrKey and select the case: :If 0=⎕NC'nsOrKey' ⍝ nsOrKey does not exist. Read the fileName and return a NameSpace. case←'ReadFile' :ElseIf 9=⎕NC'nsOrKey' ⍝ nsOrKey is a NameSpace. Save it under FileName. ns←nsOrKey ⋄ case←'SaveNS' :ElseIf ' '=↑1↑0⍴nsOrKey ⍝ nsOrKey is character(s). Read the fileName and return only this Key. key←nsOrKey ⋄ case←'ReturnKey' :Else r←0 'Left Argument Must be a NameSpace or a Character Vector(Key)' →0 :End ⍝ The only ⎕USING required subsequently. ⎕USING←',mscorlib.dll' ⍝ Required for all the cases. binaryFormatter←⎕NEW System.Runtime.Serialization.Formatters.Binary.BinaryFormatter :Select case :Case 'ReadFile' ⍝ Read the file and return a namespace :Trap 0 fileStream←⎕NEW System.IO.FileStream(fileName System.IO.FileMode.Open) hashTable←binaryFormatter.Deserialize fileStream ns←⎕NS'' :For key :In hashTable.Keys val←hashTable.get_Item⊂key ⍎'ns.',key,'←val' :End fileStream.Close r←1 ns :Else →ERROR :EndTrap :Case 'ReturnKey' :Trap 0 fileStream←⎕NEW System.IO.FileStream(fileName System.IO.FileMode.Open) hashTable←binaryFormatter.Deserialize fileStream :If ~hashTable.ContainsKey(⊂key) ⍝ Key is not present in HashTable r←0 'Invalid Key: ',∊key ⋄ fileStream.Close ⋄ →0 :Else ⍝ The Key is valid. val←hashTable.get_Item⊂key fileStream.Close r←1 val :End :Else →ERROR :EndTrap :Case 'SaveNS' ⍝ Save the NS to the file name. :Trap 0 hashTable←⎕NEW System.Collections.Hashtable :For key :In ns.⎕NL-2 hashTable.Add key(ns.⍎key) :End fileStream←⎕NEW System.IO.FileStream(fileName System.IO.FileMode.Create) binaryFormatter.Serialize fileStream hashTable r←1 fileStream.Length hashTable.Count ⍝ Bytes Written, Number of Items fileStream.Close :Else →ERROR :EndTrap :EndSelect →0 ERROR: ⍝ Show the Error: :If 90=⎕EN r←0('#.UTIL.DIO.NS2File EXCEPTION: ',⎕EXCEPTION.Message) :Else r←0((1⊃⎕DM),': ',{(~(∧\' '=⍵)∨(⌽∧\⌽' '=⍵))/⍵}(2⊃⎕DM)) :EndIf ⍝ Try to close the fileStream in case the error happened before closing it: :Trap 0 fileStream.Close :EndTrap ∇ ∇ r←new_attributes SetAttributes file_name;inter;old_attributes;⎕USING :Access Public Shared ⍝ Sets The Attributes of a File. ⍝ Positive Attributes Will be Added and Negative Attributes Removed ⍝ ⍝ file_name = Fully Qualified File Name ⍝ r = 1 (Numeric Attributes) (Litteral Attributes) (file_name), if Successfull ⍝ r = 0 (Error Description) (file_name), if Failure ⍝ ⍝ Attributes Are: ⍝ 1 = ReadOnly ⍝ 2 = Hidden ⍝ 4 = System ⍝ 16 = Directory ⍝ 32 = Archive ⍝ 64 = Device ⍝ 128 = Normal ⍝ 256 = Temporary ⍝ 512 = SparseFile ⍝ 1024 = ReparsePoint ⍝ 2048 = Compressed ⍝ 4096 = Offline ⍝ 8192 = NotContentIndexed ⍝ 16384 = Encrypted ⍝ CHECK IF FILE EXISTS :If 0=↑FileExists file_name r←0 'File Does Not Exists'file_name ⋄ →0 :End ⍝ GET THE CURRENT ATTRIBUTES :If 0=↑inter←GetAttributes file_name ⍝ Failure r←0(2⊃inter)file_name ⋄ →0 :Else ⍝ Success old_attributes←2⊃inter :End ⍝ REMOVE THE NEGATIVE ATTRIBUTES inter←old_attributes~|(new_attributes<0)/new_attributes ⍝ ADD THE POSITIVE ATTRIBUTES inter←inter,(new_attributes>0)/new_attributes ⍝ MAKE SURE THERE IS NO REPETITIONS inter←∪inter ⍝ ADD UP ALL THE ATTRIBUTES inter←+/inter ⍝ MAKE THE CALL :Trap 0 ⎕USING←',mscorlib.dll' System.IO.File.SetAttributes((⊂,file_name),(⍬⍴inter)) r←GetAttributes file_name :Else ⍝ Unexpected Error While Setting the Attributes r←0 GetLastError file_name :EndTrap ∇ ∇ r←SetDirCurrent path;⎕USING :Access Public Shared ⍝ Sets The Current Directory Path. ⍝ ⍝ r = 1 (Current Directory), if Successfull ⍝ r = 0 (ERROR) path, if Failure :If ~∆IsString path←∊path ⍝ Failure: 'path' Not Characters. r←0 'Path Must Be Characters'path ⋄ →0 :End ⍝ Check if 'path' Exists :If 0=↑DirExists path r←0 'Directory Does Not Exists'path ⋄ →0 :End ⍝ MAKE THE CALL :Trap 0 ⎕USING←',mscorlib.dll' System.IO.Directory.SetCurrentDirectory(⊂,path) r←1 path :Else ⍝ Unexpected Error r←0 GetLastError path :End ∇ ∇ r←text TFileAppend file_name;⎕USING :Access Public Shared ⍝ Appends The Specified Text to a File Using The Default Windows Code Page. ⍝ The file is created if it does not already exist. ⍝ The file handle is guaranteed to be closed by this method, even if exceptions are raised. ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name. ⍝ ⍝ file_name = Fully Qualified File Name, if Empty Use DialogOpenFile ⍝ text = Vector of Characters or String Object ⍝ r = 1 (text) (File Name), if Successfull ⍝ r = 0 (ERROR) (File Name), if Failure ⍝ ⍝ Example: R ← 'ABCDEF' DIO.TFileAppend 'C:\MyTextFile.txt' ⍝ Success: R ← 1 'ABCDEF' 'C:\MyTextFile.txt' ⍝ Failure: R ← 0 'Error Description' 'C:\MyTextFile.txt' ⍝ ⍝ Example: R ← 'ABCDEF' DIO.TFileAppend '' ⍝ Select the File to Write Using the Open File Dialog ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTERS :If 0=↑⍴file_name ⍝ Empty ? ⍝ Call the Open File Dialog :If 0=↑⍴file_name←DialogOpenFile ⍝ Failure: No File Name. Exit the program. r←0 'No File Name' '' ⋄ →0 :Else ⍝ Do Nothing, 'file_name' is not Empty. :End :ElseIf ~∆IsString file_name ⍝ Failure: 'file_name' Not Characters. r←0 'File Name Must Be Characters'file_name ⋄ →0 :Else ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty. :End ⍝ VERIFY IF 'text' IS CHARACTERS :If ~∆IsString text ⍝ 'text' Must be Characters r←0 'The Text Must be Characters'file_name ⋄ →0 :End ⍝ MAKE THE CALL :Trap 0 ⍝ System.Text.Encoding.Default = Default Encoding Page On This Computer ⎕USING←',mscorlib.dll' System.IO.File.AppendAllText((⊂,file_name),(⊂,text),(System.Text.Encoding.Default)) r←1 text file_name :Else ⍝ Failure: Unexpected Error While Writing the File r←0 GetLastError file_name :EndTrap ∇ ∇ r←TFileRead file_name;⎕USING :Access Public Shared ⍝ Reads The Contents of a Text File Using the Default Windows Code Page. ⍝ The file handle is guaranteed to be closed by this method, even if exceptions are raised. ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name. ⍝ ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile ⍝ r = 1 (text) (File Name), if Successfull ⍝ r = 0 (Error Description) (File Name), if Failure ⍝ ⍝ Example: R ← DIO.TFileRead 'C:\MyTextFile.txt' ⍝ Success: R ← 1 (text) 'C:\MyTextFile.txt' ⍝ Failure: R ← 0 (ERROR) 'C:\MyTextFile.txt' ⍝ ⍝ Example: R ← DIO.TFileRead '' ⍝ To Select the File to Read Using the Open File Dialog ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTERS :If 0=↑⍴file_name ⍝ Empty ? ⍝ Call the Open File Dialog :If 0=↑⍴file_name←DialogOpenFile ⍝ Failure: No File Name. Exit the program. r←0 'No File Name' '' ⋄ →0 :Else ⍝ Do Nothing, 'file_name' is not Empty. :End :ElseIf ~∆IsString file_name ⍝ Failure: 'file_name' Not Characters. r←0 'File Name Must Be Characters'file_name ⋄ →0 :Else ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty. :End ⍝ MAKE THE CALL :Trap 0 :If 0=↑FileExists file_name ⍝ Failure: The File Name does not exists or is ill formed. r←0 'File Name Does Not Exists'file_name :Else ⍝ Success: The File Name Exists. ⍝ STE.Default = Default Encoding Page On Your Computer ⎕USING←',mscorlib.dll' r←1(System.IO.File.ReadAllText((⊂,file_name),(System.Text.Encoding.Default)))file_name :End :Else ⍝ Failure: Unexpected Error While Reading the File r←0 GetLastError file_name :EndTrap ∇ ∇ r←text TFileWrite file_name;⎕USING :Access Public Shared ⍝ Creates a New File, Writes The Text to The File Using The Default Windows Code Page. ⍝ If the target file already exists, it is overwritten. ⍝ The file handle is guaranteed to be closed by this method, even if exceptions are raised. ⍝ Use UFileWrite when Writing a File to be Exchange Outside Your Country (Code Page May be Different) ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name. ⍝ ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile ⍝ text = Vector of Characters ⍝ r = 1 (text) (File Name), if Successfull ⍝ r = 0 (ERROR) (File Name), if Failure ⍝ ⍝ Example: R ← 'ABCDEF' DIO.TFileWrite 'C:\MyTextFile' ⍝ Success: R ← 1 'ABCDEF' 'C:\MyTextFile' ⍝ Failure: R ← 0 'Error Description' 'C:\MyTextFile' ⍝ ⍝ Example: R ← 'ABCDEF' DIO.TFileWrite '' ⍝ Select the File to Write Using the Open File Dialog ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTERS :If 0=↑⍴file_name ⍝ Empty ? ⍝ Call the Open File Dialog :If 0=↑⍴file_name←DialogOpenFile ⍝ Failure: No File Name. Exit the program. r←0 'No File Name' '' ⋄ →0 :Else ⍝ Do Nothing, 'file_name' is not Empty. :End :ElseIf ~∆IsString file_name ⍝ Failure: 'file_name' Not Characters. r←0 'File Name Must Be Characters'file_name ⋄ →0 :Else ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty. :End ⍝ VERIFY IF 'text' IS CHARACTERS :If ~∆IsString text ⍝ 'text' Must be Characters r←0 'The Text Must be Characters'file_name ⋄ →0 :End ⍝ MAKE THE CALL :Trap 0 ⍝ STE.Default = Default Encoding Page On Your Computer ⎕USING←',mscorlib.dll' System.IO.File.WriteAllText((⊂,file_name),(⊂,text),(System.Text.Encoding.Default)) r←1 text file_name :Else ⍝ Failure: Unexpected Error While Writing the File r←0 GetLastError file_name :EndTrap ∇ ∇ r←text UFileAppend file_name;⎕USING :Access Public Shared ⍝ Appends The Specified Text to a File Using The UTF-8 Encoding. ⍝ The file is created if it does not already exist. ⍝ The file handle is guaranteed to be closed by this method, even if exceptions are raised. ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name. ⍝ ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile ⍝ text = Vector of Characters ⍝ r = 1 (text) (File Name), if Successfull ⍝ r = 0 (ERROR) (File Name), if Failure ⍝ ⍝ Example: R ← 'ABCDEF' DIO.UFileAppend 'C:\MyTextFile.txt' ⍝ Success: R ← 1 'ABCDEF' 'C:\MyTextFile.txt' ⍝ Failure: R ← 0 'Error Description' 'C:\MyTextFile.txt' ⍝ ⍝ Example: R ← 'ABCDEF' DIO.UFileAppend '' ⍝ Select the File to Write Using the Open File Dialog ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTERS :If 0=↑⍴file_name ⍝ Empty ? ⍝ Call the Open File Dialog :If 0=↑⍴file_name←DialogOpenFile ⍝ Failure: No File Name. Exit the program. r←0 'No File Name' '' ⋄ →0 :Else ⍝ Do Nothing, 'file_name' is not Empty. :End :ElseIf ~∆IsString file_name ⍝ Failure: 'file_name' Not Characters. r←0 'File Name Must Be Characters'file_name ⋄ →0 :Else ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty. :End ⍝ VERIFY IF 'text' IS CHARACTERS :If ~∆IsString text ⍝ 'text' Must be Characters r←0 'The Text Must be Characters'file_name ⋄ →0 :End ⍝ MAKE THE CALL :Trap 0 ⍝ STE.UTF8 = UTF-8 Encoding ⎕USING←',mscorlib.dll' System.IO.File.AppendAllText((⊂,file_name),(⊂,text),(System.Text.Encoding.UTF8)) r←1 text file_name :Else ⍝ Failure: Unexpected Error While Writing the File r←0 GetLastError file_name :EndTrap ∇ ∇ r←lines UFileAppendAllLines file_name;⎕USING :Access Public Shared ⍝ Appends lines to a file using UTF8 encoding, and then closes the file. ⍝ If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file. ⍝ The file handle is guaranteed to be closed by this method, even if exceptions are raised. ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name. ⍝ ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile ⍝ lines = Vector where each element is a line ⍝ r = 1 (lines) (File Name), if Successfull ⍝ r = 0 (ERROR) (File Name), if Failure ⍝ ⍝ Example: R ← ('line 1' 'line 2') DIO.UFileWriteAllLines 'C:\MyTextFile' ⍝ Success: R ← 1 ('line 1' 'line 2') 'C:\MyTextFile' ⍝ Failure: R ← 0 'Error Description' 'C:\MyTextFile' ⍝ ⍝ Example: R ← ('line 1' 'line 2') DIO.UFileWriteAllLines '' ⍝ Select the File to Write Using the Open File Dialog ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTERS :If 0=↑⍴file_name ⍝ Empty ? ⍝ Call the Open File Dialog :If 0=↑⍴file_name←DialogOpenFile ⍝ Failure: No File Name. Exit the program. r←0 'No File Name' '' ⋄ →0 :Else ⍝ Do Nothing, 'file_name' is not Empty. :End :ElseIf ~∆IsString file_name ⍝ Failure: 'file_name' Not Characters. r←0 'File Name Must Be Characters'file_name ⋄ →0 :Else ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty. :End ⍝ MAKE THE CALL :Trap 0 ⍝ Convert the lines to a .Net String[] (String Array) lines←GetNetStringArray lines ⍝ STE.UTF8 = UTF-8 Encoding ⎕USING←',mscorlib.dll' System.IO.File.AppendAllLines((⊂,file_name),(lines),(System.Text.Encoding.UTF8)) r←1 lines file_name :Else ⍝ Failure: Unexpected Error While Writing the File r←0 GetLastError file_name :EndTrap ∇ ∇ r←UFileRead file_name;⎕USING :Access Public Shared ⍝ Reads The Contents of a Unicode Text File. ⍝ This method attempts to automatically detect the encoding of a file based on the ⍝ presence of byte order marks (BOM). Encoding formats UTF-8 and UTF-32 (both big-endian ⍝ and little-endian) can be detected. ⍝ The file handle is guaranteed to be closed by this method, even if exceptions are raised. ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name. ⍝ ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile ⍝ r = 1 (text) (File Name), if Successfull ⍝ r = 0 (ERROR) (File Name), if Failure ⍝ ⍝ Example: R ← DIO.UFileRead 'C:\MyTextFile.txt' ⍝ Success: R ← 1 text 'C:\MyTextFile.txt' ⍝ Failure: R ← 0 'Error Description' 'C:\MyTextFile.txt' ⍝ ⍝ Example: R ← DIO.UFileRead '' ⍝ To Select the File to Read Using the Open File Dialog ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTERS :If 0=↑⍴file_name ⍝ Empty ? ⍝ Call the Open File Dialog :If 0=↑⍴file_name←DialogOpenFile ⍝ Failure: No File Name. Exit the program. r←0 'No File Name' '' ⋄ →0 :Else ⍝ Do Nothing, 'file_name' is not Empty. :End :ElseIf ~∆IsString file_name ⍝ Failure: 'file_name' Not Characters. r←0 'File Name Must Be Characters'file_name ⋄ →0 :Else ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty. :End ⍝ MAKE THE CALL :Trap 0 :If 0=↑FileExists file_name ⍝ Failure: The File Name does not exists or is ill formed. r←0 'File Name Does Not Exists'file_name :Else ⍝ Success: The File Name Exists. ⎕USING←',mscorlib.dll' r←1(System.IO.File.ReadAllText(⊂,file_name))file_name :End :Else ⍝ Failure: Unexpected Error While Reading the File r←0 GetLastError file_name :EndTrap ∇ ∇ r←UFileReadAllLines file_name;⎕USING :Access Public Shared ⍝ Opens a text file, reads all lines of the file, and then closes the file. ⍝ The file handle is guaranteed to be closed by this method, even if exceptions are raised. ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name. ⍝ ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile ⍝ r = 1 (lines) (File Name), if Successfull ⍝ lines is a vector where each element is a line ⍝ r = 0 (Error Description) (File Name), if Failure ⍝ ⍝ Example: R ← DIO.TFileReadAllLines 'C:\MyTextFile.txt' ⍝ Success: R ← 1 (lines) 'C:\MyTextFile.txt' ⍝ Failure: R ← 0 (ERROR) 'C:\MyTextFile.txt' ⍝ ⍝ Example: R ← DIO.TFileReadAllLines '' ⍝ To Select the File to Read Using the Open File Dialog ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTERS :If 0=↑⍴file_name ⍝ Empty ? ⍝ Call the Open File Dialog :If 0=↑⍴file_name←DialogOpenFile ⍝ Failure: No File Name. Exit the program. r←0 'No File Name' '' ⋄ →0 :Else ⍝ Do Nothing, 'file_name' is not Empty. :End :ElseIf ~∆IsString file_name ⍝ Failure: 'file_name' Not Characters. r←0 'File Name Must Be Characters'file_name ⋄ →0 :Else ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty. :End ⍝ MAKE THE CALL :Trap 0 :If 0=↑FileExists file_name ⍝ Failure: The File Name does not exists or is ill formed. r←0 'File Name Does Not Exists'file_name :Else ⍝ Success: The File Name Exists. ⎕USING←',mscorlib.dll' r←1(System.IO.File.ReadAllLines(⊂,file_name))file_name :End :Else ⍝ Failure: Unexpected Error While Reading the File r←0 GetLastError file_name :EndTrap ∇ ∇ r←text UFileWrite file_name;⎕USING :Access Public Shared ⍝ Creates a New File, Writes The Specified Text to The File Using The UTF8 Encoding. ⍝ If The Target File Already Exists, it is Overwritten. ⍝ The file handle is guaranteed to be closed by this method, even if exceptions are raised. ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name. ⍝ ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile ⍝ text = Vector of Characters ⍝ r = 1 (text) (File Name), if Successfull ⍝ r = 0 (ERROR) (File Name), if Failure ⍝ ⍝ Example: R ← 'ABCDEF' DIO.UFileWrite 'C:\MyTextFile.txt' ⍝ Success: R ← 1 'ABCDEF' 'C:\MyTextFile.txt' ⍝ Failure: R ← 0 'Error Description' 'C:\MyTextFile.txt' ⍝ ⍝ Example: R ← 'ABCDEF' DIO.UFileWrite '' ⍝ Select the File to Write Using the Open File Dialog ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTERS :If 0=↑⍴file_name ⍝ Empty ? ⍝ Call the Open File Dialog :If 0=1↑⍴file_name←DialogOpenFile ⍝ Failure: No File Name. Exit the program. r←0 'No File Name' '' ⋄ →0 :Else ⍝ Do Nothing, 'file_name' is not Empty. :End :ElseIf ~∆IsString file_name ⍝ Failure: File Name Not Characters. r←0 'File Name Must Be Characters'file_name ⋄ →0 :Else ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty. :End ⍝ VERIFY IF 'text' IS CHARACTERS :If ~∆IsString text ⍝ 'text' Must be Characters r←0 'The Text Must be Characters'file_name ⋄ →0 :End ⍝ MAKE THE CALL :Trap 0 ⍝ STE.UTF8 = UTF-8 Encoding ⎕USING←',mscorlib.dll' System.IO.File.WriteAllText((⊂,file_name),(⊂,text),(System.Text.Encoding.UTF8)) r←1 text file_name :Else ⍝ Failure: Unexpected Error While Writing the File r←0 GetLastError file_name :EndTrap ∇ ∇ r←lines UFileWriteAllLines file_name;⎕USING :Access Public Shared ⍝ Creates a new file, writes the specified individual lines to the file by using UTF8 encoding, and then closes the file. ⍝ If the target file already exists, it is overwritten. ⍝ The file handle is guaranteed to be closed by this method, even if exceptions are raised. ⍝ If file_name is empty (''), DialogOpenFile is called to retrieve the file_name. ⍝ ⍝ file_name = Fully Qualified File Name, if Empty Uses DialogOpenFile ⍝ lines = Vector where each element is a line ⍝ r = 1 (lines) (File Name), if Successfull ⍝ r = 0 (ERROR) (File Name), if Failure ⍝ ⍝ Example: R ← ('line 1' 'line 2') DIO.UFileWriteAllLines 'C:\MyTextFile' ⍝ Success: R ← 1 ('line 1' 'line 2') 'C:\MyTextFile' ⍝ Failure: R ← 0 'Error Description' 'C:\MyTextFile' ⍝ ⍝ Example: R ← ('line 1' 'line 2') DIO.UFileWriteAllLines '' ⍝ Select the File to Write Using the Open File Dialog ⍝ VERIFY IF 'file_name' IS EMPTY OR CHARACTERS :If 0=↑⍴file_name ⍝ Empty ? ⍝ Call the Open File Dialog :If 0=↑⍴file_name←DialogOpenFile ⍝ Failure: No File Name. Exit the program. r←0 'No File Name' '' ⋄ →0 :Else ⍝ Do Nothing, 'file_name' is not Empty. :End :ElseIf ~∆IsString file_name ⍝ Failure: 'file_name' Not Characters. r←0 'File Name Must Be Characters'file_name ⋄ →0 :Else ⍝ Do Nothing 'file_name' is a Character Vector and Not Empty. :End ⍝ MAKE THE CALL :Trap 0 ⍝ STE.UTF8 = UTF-8 Encoding ⎕USING←',mscorlib.dll' System.IO.File.WriteAllLines((⊂,file_name),(⊂,lines),(System.Text.Encoding.UTF8)) r←1 lines file_name :Else ⍝ Failure: Unexpected Error While Writing the File r←0 GetLastError file_name :EndTrap ∇ ∇ ZipCompressFile fileName;sfDir;zipArchive;⎕USING :Access Public ⍝ Compress a file using Syncfusion Zip namespace. ⍝ Set ⎕USING for the Syncfusion dll. sfDir←sfDir←'Syncfusion/4.5/' ⍝ Location of the Syncfusion dll's ⎕USING←'Syncfusion.Compression.Zip,',sfDir,'Syncfusion.Compression.Base.dll' ⍝ Get a ZipArchive zipArchive←⎕NEW ZipArchive zipArchive.DefaultCompressionLevel←zipArchive.DefaultCompressionLevel.Best ⍝ Add the file to the ZipArchive {}zipArchive.AddFile(⊂,fileName) ⍝ Change the fileName extension to zip fileName←((fileName⍳'.')↑fileName),'zip' ⍝ Save the zipped file zipArchive.Save(⊂,fileName) ⍝ Clean-up zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL ∇ :EndNamespace