Managing Files and Directories

The DirectoryInfo class in System.IO is useful for getting information about files and directories and to deal with them: rename, move to, delete, create...

Here you see a sample function which should run on any system with Dyalog Version 11 and at least .NET 1.1

⍝ Version 1.3 from 2006-12-22 ⋄ Kai Jaeger ⋄ APL Team Ltd

 ⎕ML ⎕IO←1

⍝ The directory we want deal with:
 directory←2 ⎕NQ'.' 'GetEnvironment' 'Dyalog'

⍝ Establish a .NET object reference on this:
 myDirObj←⎕NEW System.IO.DirectoryInfo(⊂directory) ⍝ Version 11: enclose argument!

⍝ Get a list of all files in that directory:
 ⎕←listOfFiles←myDirObj.GetFiles ⍬ ⍝ it's a method: needs an argument!

⍝ Make simple strings from the result and display as a simple matrix:
⍝ Note that listOfFiles is _not_ a vector of string but an array of objects.
⍝ Therefore, we have to ⍕¨ them to get a string for every object. This works

⍝ Note that ⍕¨ is the same as this:

⍝ Get the LastWriteTime for all files in that directory:

⍝ The same with a different technique:
 ⎕←myDirObj.(GetFiles ⍬).LastWriteTime

⍝ The same but with UTC time instead:
 myDirObj.(GetFiles ⍬).LastWriteTimeUtc

⍝ Check existence:
 ⎕←'Does not exist!' 'Exists'⊃⍨1+{⍵.Exists}⎕NEW System.IO.DirectoryInfo(⊂directory)
 ⎕←'Does not exist!' 'Exists'⊃⍨1+{⍵.Exists}⎕NEW System.IO.DirectoryInfo(⊂'Does HopefullyNotExist')
 ⎕←myDirObj.Parent    ⍝ No argument ...
 ⎕←myDirObj.Root      ⍝ ... because these ...
 ⎕←myDirObj.Name      ⍝ ... area  all ...
 ⎕←myDirObj.FullName  ⍝ ... properties

⍝ Use a search pattern:

⍝ Possible also for sub-directories:
 ⎕←myDirObj.GetDirectories ⍬

⍝ And this in turn is possible with search patterns as well:

⍝ Create a new sub-directory
 subdir←14 0⍕100⊥¯1↓⎕TS

⍝ Create many levels of sub-dirs in one go:

⍝ Get sub-directories of this:
 ⎕←subdirObj.GetDirectories ⍬

⍝ Rename a file or directory (or move it):
⍝ For this, we need an instance of the "DirectoryEntry" class pointing
⍝ to the parent of the directory we want rename
 di←⎕NEW System.IO.DirectoryInfo(⊂directory,subdir)

⍝ The same with a slightly different technique; ⍺=old name, ⍵=new name
 (directory,(¯1↓subdir),'_',⎕AN){(⎕NEW System.IO.DirectoryInfo(⊂⍺)).MoveTo⊂⍵}directory,(¯1↓subdir),'___',⎕AN

⍝ Re-establish reference for next and final setp:
 subdirObj←⎕NEW System.IO.DirectoryInfo(⊂directory,(¯1↓subdir),'___',⎕AN)

⍝ Delete the newly introduced sub-dir:
 {0::⎕EXCEPTION ⋄ subdirObj.Delete ⍵}⍬ ⍝ fails because not empty!

⍝ Delete the newly introduced sub-dir along with all the sub-dirs:
⍝ Before executing this line, start Windows Explorer and look into
⍝ that directory. As long as the Windows Explorer is looking into the
⍝ directory, it cannot be deleted. You will therefore get an Exception:
     :Trap 90
         subdirObj.Delete 1
         ⎕←'Delete directory has failed; to try again press <Enter>' ⋄ {}⍞
 :Until successFlag

⍝ End

Nice and easy to use. However, if you need to deal with large numbers of files, watch out! When I tried to deal with 90,000 files the other day, these methods proved to be inadequate. For this, look at the WinFile class.

Author: KaiJaeger

CategoryDotNet - CategoryDyalogDotNet - CategoryDyalogExamplesDotNet

DirectoryInfo (last edited 2015-04-05 01:18:35 by PierreGilbert)