[Implemented] DeleteDirectory() or RemoveDirectory()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

[Implemented] DeleteDirectory() or RemoveDirectory()

Post by BackupUser »

Restored from previous forum. Originally posted by Ralf.

Hello would be great if there would be a statement or funktion
which delete a directory and it subdirectories.
cOUld be called DeleteDirectory() or RemoveDirectory() or
something like that.
I wrote a Routine to ge rid of all the files in the Directory
und it's subdirectories. But the Directories are still there.
Only the files are deleted.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.
I wrote a Routine to ge rid of all the files in the Directory
und it's subdirectories. But the Directories are still there.
Only the files are deleted.
In Windows, you can use this command to remove an empty directory:

Code: Select all

dir$="c:\DirToRemove\"
RemoveDirectory_(dir$)

PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Ok, I will add it.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Ralf.

Thanks a lot.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Ralf.

PB.
I tried your :
dir$="C:\DirToRemove"
RomoveDirectory_(dir$)
i doesn't work.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

(1) dir$="C:\DirToRemove"
(2) RomoveDirectory_(dir$)

Works for me... did you have a trailing slash for (1) above?
(Although I don't think it actually makes a difference...).

And for (2), I assume that Romove was just a typo?

It won't work as a DLL, so test it as an EXE. (Note to Fred: If you add
a "delete dir" command, please ensure it works for EXEs and DLLs).


PB - Registered PureBasic Coder

Edited by - PB on 29 January 2002 14:16:46
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.
(Note to Fred: If you add a "delete dir" command, please ensure it works for EXEs and DLLs).
All commands work with DLL. Why this one shouldn't ?

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> All commands work with DLL. Why this one shouldn't ?

Don't know, but if I compile+run the code I posted above, while still in the
editor and not as a compiled DLL, it doesn't work. (When the compiler option
is set to Shared DLL, that is). Maybe it works when actually compiled to a
real DLL file, but I haven't tested that... so I apologize if it does work
when done that way.

Don't know why Ralf is having trouble with it though...


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Stan.

Don't know why Ralf is having trouble with it though...

PB - Registered PureBasic Coder
As a newbie I may be completely off, but I remeber having the same problem in
C with strings like "\thedirectory" or "\newdirectory" as \t and \n are special
sequences, solved it in C with "\\thedirectory" or "\\newdirectory"

Hope this helps.



Learning and Love are what life is all about ...
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Ralf.

This is the Procedure:

OpenConsole()

Procedure DelDirectory(DelDirectory$, Start)
If ExamineDirectory(Start, DelDirectory$, "*.*")
Repeat
Type = NextDirectoryEntry()
If Type = 2
If DirectoryEntryName() "." And DirectoryEntryName() ".."
c$ = DelDirectory$+DirectoryEntryName()+"\"
DelDirectory(c$, Start+1)
UseDirectory(Start)
EndIf
Else
If Type = 1
DeleteFile(DelDirectory$+DirectoryEntryName())
EndIf
EndIf
Until Type=0
EndIf
EndProcedure

DelDirectory("C:\DOS99\",Start)
dir$="C:\DOS99"
RemoveDirectory_(dir$)

So what's the problem ? Doesn't work.
Maybe i wait until Fred has implemented the funktion.



Edited by - ralf on 30 January 2002 12:54:03
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> dir$="C:\DOS99"
> RemoveDirectory_(dir$)

The above code works for me with both Win 95 and Win 2000...

Are you 100% certain that the directory is empty? Probably a stupid question,
I guess, but still... are there no hidden files or anything?

PB - Registered PureBasic Coder

Edited by - PB on 30 January 2002 13:18:15
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Ralf.

I checked the directory c:\dos-99.
It's empty except there are empty subdirectories.
No files, no hidden ones.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> It's empty except there are empty subdirectories.

Ah, that's why! The directory to be removed has to be empty, as I said
at the start. It cannot contain anything else inside it, not even other
empty directories.


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Ralf.

aha . that 's the reason.
This RemoveDirectory_(dir$), where is it from ?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> aha . that 's the reason.

Yep. So modify your procedure a bit to remove the emptied directories as soon
as all the files have been deleted in each one, and it'll work just fine.

> This RemoveDirectory_(dir$), where is it from ?

It's a Win32 API command which is built-in to all Windows versions.


PB - Registered PureBasic Coder

Edited by - PB on 30 January 2002 14:15:14
Post Reply