Deleting a Directory

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Deleting a Directory

Jeffrey Odell-2
I feel like a dunce, but I can't locate how to determine if a directory
exists and, if so, delete it.  File>>#exists: seems to tell me if the
directory is there, but I haven't found method to delete the directory.

Help the terminally clueless, please!

jlo


Reply | Threaded
Open this post in threaded view
|

Re: Deleting a Directory

Dmitry Zamotkin-3
Hi Jeffrey,

> I feel like a dunce, but I can't locate how to determine if a directory
> exists and, if so, delete it.  File>>#exists: seems to tell me if the
> directory is there, but I haven't found method to delete the directory.

I use next code:

File class>>removeDirectory: path
"Delete directory with files and folders within"
 | kernel |

 self for: '*.*' inAndBelow: path do: [ :filepath :filedata |
  |filename|
  filename := filedata fileName.
  (filename = '.' or: [filename = '..']) ifFalse: [
   ( filedata dwFileAttributes allMask: 16r10 ) "FILE_ATTRIBUTE_DIRECTORY"
    ifTrue: [ self removeDirectory: filepath, '\', filename]
    ifFalse: [ self delete: filepath, '\', filename]
  ]
 ].
 kernel := KernelLibrary default.
 ^(kernel removeDirectory: path)
  or: [ | err |
   err := kernel getLastError.
   kernel systemError: err.]

KernelLibrary>>removeDirectory: path
"Delete an empty directory only"
 <stdcall: bool RemoveDirectoryA lpstr>
 ^self invalidCall


--
Dmitry Zamotkin


Reply | Threaded
Open this post in threaded view
|

Re: Deleting a Directory

Ian Bartholomew
In reply to this post by Jeffrey Odell-2
Jeffrey,

> I feel like a dunce, but I can't locate how to determine if a directory
> exists and, if so, delete it.  File>>#exists: seems to tell me if the
> directory is there, but I haven't found method to delete the directory.

I can't help much with the File methods but if you are playing around with
Folders then you might want to investigate the Windows scripting control
which has much more control over the FileSystem. I now use it in preference
to the "normal" classes for anything other than simple File i/o. As a demo -

First, load the scripting library. I normally do this in a dedicated
FileSystem package script

tlb := AXTypeLibraryAnalyzer open: 'scrrun.dll'.
tlb prefix: ''.
tlb generateInterfaceWrappers.

Now create a FileSystem object. Again, I normally have a FileSystem class
with a singleton instance

iFileSystem := IFileSystem createObject: 'Scripting.FileSystemObject'.

You can now work with the FileSystem.

iFileSystem createFolder: 'c:\fred'.
iFileSystem createFolder: 'c:\fred\bert'.

iFileSystem folderExists: 'c:\fred\bert'.

iFileSystem deleteFolder: 'c:\fred\bert' force: true.
iFileSystem deleteFolder: 'c:\fred' force: true.

You can also create other objects, representing Files and Folders, to give
you more control, via #getFolder and #getFile.

Regards
    Ian


Reply | Threaded
Open this post in threaded view
|

Re: Deleting a Directory

Jeffrey Odell-2
In reply to this post by Dmitry Zamotkin-3
Thanks - this is a handy extension - I was looking for the Windows API call
this AM when I saw your response!

jlo

"Dmitry Zamotkin" <[hidden email]> wrote in message
news:3c3b1215$1@tobjects....

> Hi Jeffrey,
>
> > I feel like a dunce, but I can't locate how to determine if a directory
> > exists and, if so, delete it.  File>>#exists: seems to tell me if the
> > directory is there, but I haven't found method to delete the directory.
>
> I use next code:
>
> File class>>removeDirectory: path
> "Delete directory with files and folders within"
>  | kernel |
>
>  self for: '*.*' inAndBelow: path do: [ :filepath :filedata |
>   |filename|
>   filename := filedata fileName.
>   (filename = '.' or: [filename = '..']) ifFalse: [
>    ( filedata dwFileAttributes allMask: 16r10 ) "FILE_ATTRIBUTE_DIRECTORY"
>     ifTrue: [ self removeDirectory: filepath, '\', filename]
>     ifFalse: [ self delete: filepath, '\', filename]
>   ]
>  ].
>  kernel := KernelLibrary default.
>  ^(kernel removeDirectory: path)
>   or: [ | err |
>    err := kernel getLastError.
>    kernel systemError: err.]
>
> KernelLibrary>>removeDirectory: path
> "Delete an empty directory only"
>  <stdcall: bool RemoveDirectoryA lpstr>
>  ^self invalidCall
>
>
> --
> Dmitry Zamotkin
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Deleting a Directory

Jeffrey Odell-2
In reply to this post by Ian Bartholomew
Of course!

I wrapped this control in our VisualAge application.  We use it for
automating software updates - our software runs a predefined script on a
server as startup.  This script gets a parameter specifying the version of
the software running.  The script is then written/customized by a system
administrator, who can update it as needs change.  Seems perfectly fine to
use it in Dolphin, especially since it is a Windows-specific implementation.

Thanks for the idea - a very good one.

jlo

"Ian Bartholomew" <[hidden email]> wrote in message
news:tTy_7.6285$XN3.384796@wards...
> Jeffrey,
>
> > I feel like a dunce, but I can't locate how to determine if a directory
> > exists and, if so, delete it.  File>>#exists: seems to tell me if the
> > directory is there, but I haven't found method to delete the directory.
>
> I can't help much with the File methods but if you are playing around with
> Folders then you might want to investigate the Windows scripting control
> which has much more control over the FileSystem. I now use it in
preference
> to the "normal" classes for anything other than simple File i/o. As a
demo -

>
> First, load the scripting library. I normally do this in a dedicated
> FileSystem package script
>
> tlb := AXTypeLibraryAnalyzer open: 'scrrun.dll'.
> tlb prefix: ''.
> tlb generateInterfaceWrappers.
>
> Now create a FileSystem object. Again, I normally have a FileSystem class
> with a singleton instance
>
> iFileSystem := IFileSystem createObject: 'Scripting.FileSystemObject'.
>
> You can now work with the FileSystem.
>
> iFileSystem createFolder: 'c:\fred'.
> iFileSystem createFolder: 'c:\fred\bert'.
>
> iFileSystem folderExists: 'c:\fred\bert'.
>
> iFileSystem deleteFolder: 'c:\fred\bert' force: true.
> iFileSystem deleteFolder: 'c:\fred' force: true.
>
> You can also create other objects, representing Files and Folders, to give
> you more control, via #getFolder and #getFile.
>
> Regards
>     Ian
>
>
>
>