Discussion:
Deleting a empty folder
(too old to reply)
Ronnie MacGregor
2008-11-24 20:28:06 UTC
Permalink
On Mon, 24 Nov 2008 19:22:28 +0100
I have a routine that erases certain files form a folder
it checks if there are any left files on the folder.
If the folder is empty it tries to delete it but it does not do it
since it says it is in use....
How can I delete it, besides putting it on a list and next time
that I enter the application erase it ?
Most probably a timing issue.

You are probably trying to delete the directory before Windows has finished the
actual deletion of the files. Try introducing a delay.

Beware of users who have disc caching switched on for disk writes.
--
Ronnie MacGregor
Scotland

Ronnie at
dBASEdeveloper
dot co dot uk

www.dBASEdeveloper.co.uk
unknown
2008-11-24 22:36:37 UTC
Permalink
On Mon, 24 Nov 2008 19:22:28 +0100 "evilaro" <buzon[NO]@evilfoto.es>
Sender: "evilaro" <buzon[NO]@evilfoto.es>
wrote the following in:
Newsgroup: dbase.programming
I have a routine that erases certain files form a folder
it checks if there are any left files on the folder.
If the folder is empty it tries to delete it but it does not do it
since it says it is in use....
How can I delete it, besides putting it on a list and next time
that I enter the application erase it ?
You can use the FileSystemObject. It has a method fso.DeleteFolder() which will delete a folder
including any files and subfolders in the folder without warning.

Save and run the code below my signature as indicated. The code illustrates how to ues some features
of the FileSystemObject. You can as shown, also list and delete files in the folder if you want to.

To inspect the result of the code insert some returns.


Ivar B. Jessen

//-----
fso = new oleautoclient("Scripting.FileSystemObject")

// Create a folder for testing
cPath = "C:\TempX117"

if not fso.FolderExists(cPath)
fso.CreateFolder(cPath)
endif

// Add a file to the folder
copy file "delFolder.prg" to cPath + "\delFolder.prg"

// Get the number of files in the folder
fold = fso.GetFolder(cPath)
? fold.files.count

// List the files in the folder
for i = 0 to fold.files.count - 1
? fold.files[0].Name, fold.files[0].Type
next i

*return // uncomment to see result of code

// Delete the file in the folder
fold.files[0].delete()

// Delete the folder with all its files
fso.DeleteFolder(cPath)

fold = null
fso = null
//-----
evilaro
2008-11-25 16:45:56 UTC
Permalink
Ivar:

This is very useful:

I had one of the options but not all ;)

Is there a parameter to NOT delete a folder if
there is a file inside or another folder...

Thanks a lot.

Emilio
Post by unknown
Newsgroup: dbase.programming
I have a routine that erases certain files form a folder
it checks if there are any left files on the folder.
If the folder is empty it tries to delete it but it does not do it
since it says it is in use....
How can I delete it, besides putting it on a list and next time
that I enter the application erase it ?
You can use the FileSystemObject. It has a method fso.DeleteFolder() which
will delete a folder
including any files and subfolders in the folder without warning.
Save and run the code below my signature as indicated. The code
illustrates how to ues some features
of the FileSystemObject. You can as shown, also list and delete files in
the folder if you want to.
To inspect the result of the code insert some returns.
Ivar B. Jessen
//-----
fso = new oleautoclient("Scripting.FileSystemObject")
// Create a folder for testing
cPath = "C:\TempX117"
if not fso.FolderExists(cPath)
fso.CreateFolder(cPath)
endif
// Add a file to the folder
copy file "delFolder.prg" to cPath + "\delFolder.prg"
// Get the number of files in the folder
fold = fso.GetFolder(cPath)
? fold.files.count
// List the files in the folder
for i = 0 to fold.files.count - 1
? fold.files[0].Name, fold.files[0].Type
next i
*return // uncomment to see result of code
// Delete the file in the folder
fold.files[0].delete()
// Delete the folder with all its files
fso.DeleteFolder(cPath)
fold = null
fso = null
//-----
unknown
2008-11-25 19:30:11 UTC
Permalink
On Tue, 25 Nov 2008 17:45:56 +0100 "evilaro" <buzon[NO]@evilfoto.es>
Sender: "evilaro" <buzon[NO]@evilfoto.es>
wrote the following in:
Newsgroup: dbase.programming
Post by evilaro
I had one of the options but not all ;)
Is there a parameter to NOT delete a folder if
there is a file inside or another folder...
Thanks a lot.
Emilio,

Check the number of files and subfolders in the folder and only delete if the counts are zero.

See the code below my signatire for an example.


Ivar B. Jessen

//-----
fso = new oleautoclient("Scripting.FileSystemObject")

// Create a folder for testing
cPath = "C:\TempX117"
if not fso.FolderExists(cPath)
fso.CreateFolder(cPath)
endif

// Add a file to the folder
copy file program(1) to cPath + "\" + Program() + ".prg"

// Create a subfolder in the folder
cPathSub = cPath + "\TempSub"
if not fso.FolderExists(cPathSub)
fso.CreateFolder(cPathSub)
endif

*return

// Get the number of files and subfolders in the folder
fold = fso.GetFolder(cPath)
? "Number of files in the folder = ", ""+fold.files.count
? "Number of subfolders in the folder = ", ""+fold.SubFolders.count

*return

// List the files in the folder
?
for i = 0 to fold.files.count - 1
? fold.files[0].Name at 1, fold.files[0].Type at 25
next i

// List the subfolders in the folder
?
for i = 0 to fold.SubFolders.count - 1
? fold.SubFolders[0].Name at 1, fold.SubFolders[0].Type at 25
next i

*return // uncomment to see result of code

// Delete the file in the folder
*fold.files[0].delete()

// Delete the subfolder in the folder
*fold.SubFolders[0].delete()

// Delete the folder with all its files and subfolders if the folder is empty
if fold.files.count == 0 and fold.SubFolders.count == 0
fso.DeleteFolder(cPath)
endif

fold= null
fso = null
//-----
evilaro
2008-11-26 10:46:38 UTC
Permalink
Ivar:

Very very sharp....
Perfect..

Thank a lot.

Emilio

PS...

I believe that when you delete a file or a folder with this
system it does NOT got to the recycling bin ??
Post by unknown
// Delete the folder with all its files and subfolders if the folder is empty
if fold.files.count == 0 and fold.SubFolders.count == 0
fso.DeleteFolder(cPath)
endif
fold= null
fso = null
//-----
unknown
2008-11-26 12:16:45 UTC
Permalink
On Wed, 26 Nov 2008 11:46:38 +0100 "evilaro" <buzon[NO]@evilfoto.es>
Sender: "evilaro" <buzon[NO]@evilfoto.es>
wrote the following in:
Newsgroup: dbase.programming
Post by evilaro
Very very sharp....
Perfect..
Thank a lot.
Emilio
PS...
I believe that when you delete a file or a folder with this
system it does NOT got to the recycling bin ??
Emilio,

I have lost the documentation for the file system object; my recollection is that the file or folder
is deleted without warning and gone forever.

If you do not want to loose the file or folder, you can move it to another location as in the
following example,
Post by evilaro
// List the subfolders in the folder
?
for i = 0 to fold.SubFolders.count - 1
? fold.SubFolders[0].Name at 1, fold.SubFolders[0].Type at 25
next i
// Move file in the folder to the subfolder
fso.MoveFile(cPath + "\" + Program() + ".prg", cPathSub + "\" + Program() + ".prg")
Post by evilaro
*return // uncomment to see result of code
A similar command exists for moving folders. Check out inspect(fso).


Ivar B. Jessen
evilaro
2008-11-27 19:37:32 UTC
Permalink
Ivar:

Thanls a lot.....

Emilio
Post by unknown
// Move file in the folder to the subfolder
fso.MoveFile(cPath + "\" + Program() + ".prg", cPathSub + "\" + Program() + ".prg")
Post by unknown
*return // uncomment to see result of code
A similar command exists for moving folders. Check out inspect(fso).
Ivar B. Jessen
unknown
2008-11-27 21:16:10 UTC
Permalink
On Thu, 27 Nov 2008 20:37:32 +0100 "evilaro" <buzon[NO]@evilfoto.es>
Sender: "evilaro" <buzon[NO]@evilfoto.es>
wrote the following in:
Newsgroup: dbase.programming
Post by evilaro
Thanls a lot.....
Emilio,

You are welcome.


Ivar B. Jessen

evilaro
2008-11-25 16:44:24 UTC
Permalink
Ronnie:

I have put a small delay and it improves...
but I have to make more trial to be sure...

Thanks
Emilio
Post by Ronnie MacGregor
Most probably a timing issue.
You are probably trying to delete the directory before Windows has finished the
actual deletion of the files. Try introducing a delay.
Loading...