GLBasic forum

Main forum => GLBasic - en => Topic started by: ampos on 2011-Mar-03

Title: How do I delete a directory?
Post by: ampos on 2011-Mar-03
I am using KILLFILE to kill all files inside a directory, but I can not delete the directory...

I am trying SHELLCMD "rd directory" with no luck...

any idea?
Title: Re: How do I delete a directory?
Post by: MrTAToad on 2011-Mar-03
Sure you are in the correct position to remove the directory ?
Title: Re: How do I delete a directory?
Post by: ampos on 2011-Mar-03
Quote from: MrTAToad on 2011-Mar-03
Sure you are in the correct position to remove the directory ?

Yes, I am on my chair, and I sit the most straight I can...

Or do I need to sit on  :shit: to "remove" a directory?
Title: Re: How do I delete a directory?
Post by: MrTAToad on 2011-Mar-03
You should be in the preceding directory :)
Title: Re: How do I delete a directory?
Post by: Moebius on 2011-Mar-03
He meant are you using absolute paths (e.g. "C:\Program Files\...") or relative paths ("Media\...")?

Also, if you get the command working, you can use "rd /S /Q" to remove all files and subfolders without having to use KILLFILE.
Title: Re: How do I delete a directory?
Post by: ampos on 2011-Mar-03
hehe, I was joking, opf course

I have been trying do delete an empty sir I created in c:, but again, no luck

Code (glbasic) Select
OK=SHELLCMD("cmd rd c:\t",0,0,z)
Title: Re: How do I delete a directory?
Post by: Marmor on 2011-Mar-03
most times is usefull to kill all file in the directory first and the kill the directory itself
Title: Re: How do I delete a directory?
Post by: MrTAToad on 2011-Mar-03
Quotehehe, I was joking, opf course
I know :)

Sure there are no hidden files in there ?
Title: Re: How do I delete a directory?
Post by: ampos on 2011-Mar-03
I just created this "t" dir in c: and I am trying to delete it. It is really empty. Try my example.
Title: Re: How do I delete a directory?
Post by: Moebius on 2011-Mar-03
Try something like this for your command:
Quote"CMD /C rd C:\t /S /Q"

Just stick the "CMD /C" at the front of whatever you're using.  It seems that you need to use the command prompt to execute the 'RD' command...
Title: Re: How do I delete a directory?
Post by: MrTAToad on 2011-Mar-03
Its possible that CMD isn't being found - if you can, pause the output window and see what it says.

Failing that, use something like :

Code (glbasic) Select

INLINE
}

extern "C" int rmdir(const char *path);

namespace __GLBASIC__ {
ENDINLINE

FUNCTION KILLDIR%:dirName$
INLINE
    return (DGNat) rmdir(dirName.c_Str());
ENDINLINE
ENDFUNCTION


I haven't tested it, but it's the general idea...
Title: Re: How do I delete a directory?
Post by: Slydog on 2011-Mar-03
You could use the system API.
The following is from my library, written in Visual Basic 6, but could be converted to GLBasic like other API calls.

Code (glbasic) Select
' Constants
Private Const FO_DELETE = &H3
Private Const FOF_RECYCLEBIN = &H40
Private Const FOF_SILENT = &H4
Private Const FOF_NOCONFIRMATION = &H10

Private Type SHFILEOPSTRUCT
    hWnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAborted As Boolean
    hNameMaps As Long
    sProgress As String
End Type

Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

' Deletes ALL files and folders (including sub files/folders) matching 'path'
Public Sub File_DeleteAll(path As String, Optional use_recycle_bin As Boolean = False)
    // Ensure file exists
    ...
    Dim file_op As SHFILEOPSTRUCT

    file_op.wFunc = FO_DELETE              'We want to delete the file
    file_op.pFrom = path                   'Which file?
    file_op.fFlags = FOF_NOCONFIRMATION    'NO confirmation
    If use_recycle_bin Then file_op.fFlags = file_op.fFlags + FOF_RECYCLEBIN     'Move to recycle bin?
    SHFileOperation file_op                'Call API
End Function

Title: Re: How do I delete a directory?
Post by: ampos on 2011-Mar-03
Quote from: Serpent on 2011-Mar-03
Try something like this for your command:
Quote"CMD /C rd C:\t /S /Q"

Just stick the "CMD /C" at the front of whatever you're using.  It seems that you need to use the command prompt to execute the 'RD' command...

With a second backslash it did work. Enough for me. Thank you all anyway.

Code (glbasic) Select
ok=SHELLCMD("CMD /C rd C:\\t /S /Q",0,0,z)
Title: Re: How do I delete a directory?
Post by: Kitty Hello on 2011-Mar-03
not x-platform, mind.

Why do you need that? I mean - why did you create it in the first place? :P

removing a directory is quite stupid on most platoforms, because you iterate through all subdirs and files and delete these first. Pretty complicated.
Title: Re: How do I delete a directory?
Post by: ampos on 2011-Mar-03
I am making an editor for internal use.

The editor rename files and launch automatically external setbox program, as setbox need directories. So I create a dir, place the file, create the shoebox, delete the file inside the dir and "try to delete the dir"

BTW, loadsprite works with jpeg files, but not if they are inside a shoebox.

Still I have not try jpg on iphone, just in win current version, and it works fine. But as they have to be "protected" in someway, I use a shoebox for each file, so I discover that jpg files inside a shoebox does not work. So I switched back to png, but png files are huge... I would love to use jpg...
Title: Re: How do I delete a directory?
Post by: Kitty Hello on 2011-Mar-04
jpg only "works" on windows it seems. t least on iPhone it crashes dramatically. :(
Title: Re: How do I delete a directory?
Post by: S.O.P.M. on 2014-Sep-11
To dig up an very old thread isn't very welcome but I see a good reason to do it nevertheless. I'd the problem not to be able to delete a folder. Now it works but I really ask myself how about proper path definitions. When can I use "\" and when "/" and is this dependend on the operating system my app is running on? GLBasic returns a path with "/" but on Windows, path levels are seperated by "\". And is it important to have a seperator at the end of a path declaration?

It would be helpful to have basic knowledge about this!
Title: Re: How do I delete a directory?
Post by: MrTAToad on 2014-Sep-11
Windows allows / and \

As other systems just use / you might as well stick to using just /

After all, why give yourself extra trouble :)
Title: Re: How do I delete a directory?
Post by: S.O.P.M. on 2014-Sep-11
Ouh okay! Nice to know, thanks. Have decided to use the slash because it has to work on Android also.
Title: Re: How do I delete a directory?
Post by: Kitty Hello on 2014-Sep-11
GLBasic always uses forward slash!

Gesendet von meinem GT-N7100 mit Tapatalk

Title: Re: How do I delete a directory?
Post by: S.O.P.M. on 2014-Sep-14
I've found out that I have to use the back slash on Android, otherwise no proper file operation will happen. At least in my case. It's ok for me because this way it works on Windows also and I don't have to use different, platform dependend, program behavior.
Title: Re: How do I delete a directory?
Post by: MrPlow on 2014-Sep-14
OK=SHELLCMD("cmd rmdir c:\t >nul",0,0,z)
Title: Re: How do I delete a directory?
Post by: Kitty Hello on 2015-Oct-13
GLBasic native solution:

Code (glbasic) Select


DelTree("C:/Users/gf/Downloads/demo/test", TRUE)
KEYWAIT

FUNCTION DelTree%: folder$, bReallyKill%=FALSE
LOCAL enter$[]
LOCAL files$[]
LOCAL dirs$[]

LOCAL cd$ = GETCURRENTDIR$()
IF NOT SETCURRENTDIR(folder$) THEN RETURN FALSE

DIMPUSH enter$[], folder$

WHILE LEN(enter$[])
LOCAL fld$ = enter$[-1]
DIMDEL enter$[], -1
SETCURRENTDIR(fld$)
LOCAL tmp$[]
LOCAL ntot% = GETFILELIST("*.*", tmp$[])

LOCAL ntotdir% = INTEGER(ntot% / 0x10000)
FOR ndir% = 1 TO ntotdir%
IF tmp$[0]<>"." AND tmp$[0]<>".."
LOCAL full$ = fld$ + "/" + tmp$[0]
DIMPUSH enter$[], full$
DIMPUSH dirs$[], full$
ENDIF
DIMDEL tmp$[], 0 // pop top
NEXT

FOR nfile% = 1 TO MOD(ntot%, 0x10000)
DIMPUSH files$[], fld$ + "/" + tmp$[nfile%-1]
NEXT
WEND

SETCURRENTDIR(cd$) // otherwise the system hold a handle to the directory to remove!

// reverse dirs, delete from long to short pathes
LOCAL revdir$[]
WHILE LEN(dirs$[])
DIMPUSH revdir$[], dirs$[-1]
DIMDEL dirs$[], -1
WEND

FOREACH f$ IN files$[]
IF bReallyKill
KILLFILE f$
ENDIF
NEXT

FOREACH f$ IN revdir$[]
IF bReallyKill
KILLFILE f$
ENDIF
NEXT

IF bReallyKill
KILLFILE folder$
ENDIF

ENDFUNCTION


It's working now.