Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to remove an existing directory programatically?
Message
 
To
15/10/2002 22:22:52
Henry Ravichander
RC Management Systems Inc.
Saskatchewan, Canada
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00711569
Message ID:
00722928
Views:
40
In order to use RMDIR (aka RD), the folder being removed must be empty. You could use the following code I wrote (based on code from someone here on UT) to delete folder contents (and sub-folders, too):
PROCEDURE Folder_Delete_Contents
**********************************************************************
* this procedure will delete a folder's contents
**********************************************************************
    #INCLUDE FOXPRO.h
    #DEFINE MB_TITLE "Folder_Delete_Contents"

* SHFileOperation constants -----------------
#DEFINE FO_COPY 				2
#DEFINE FO_DELETE 				3
#DEFINE FO_MOVE 				1
#DEFINE FO_RENAME 				4

#DEFINE FOF_ALLOWUNDO 			0x40
#DEFINE FOF_CONFIRMMOUSE 		0x2

* on *.*, do only files
#DEFINE FOF_FILESONLY 			0x80

* when copying multiple files to multiple destinations
#DEFINE FOF_MULTIDESTFILES 		0x1

* don't prompt the user
#DEFINE FOF_NOCONFIRMATION 		0x10

* don't confirm making any needed directories
#DEFINE FOF_NOCONFIRMMKDIR 		0x200

#DEFINE FOF_RENAMEONCOLLISION 	0x8

* don't create progress/report
#DEFINE FOF_SILENT 				0x4

* don't show names of files
#DEFINE FOF_SIMPLEPROGRESS 		0x100

* If FOF_RENAMEONCOLLISION is specified, the hNameMappings member will be filled in if any files were renamed. 
#DEFINE FOF_WANTMAPPINGHANDLE 	0x20
* -------------------------------------------

    LPARAMETER tcSource, tlSubfolders
    LOCAL lcSource, lcTarget, loHeap, lnHeap, lnOptions, lcFileOpStruct, lnResult
    IF VARTYPE(tcSource) # "C"
        tcSource = SPACE(0)
    ENDIF
    IF VARTYPE(tlSubfolders) # "L"
        tlSubfolders = .F.
    ENDIF
    IF EMPTY(tcSource)
        =MESSAGEBOX("Source folder is required!",MB_OK+MB_ICONSTOP,MB_TITLE)
        RETURN
    ENDIF
    IF !DIRECTORY(tcSource)
        =MESSAGEBOX("Source folder (" + tcSource + ") was not found!",MB_OK+MB_ICONSTOP,MB_TITLE)
        RETURN
    ENDIF
    lcSource = tcSource
    lcSource = ADDBS(lcSource) + "*.*"
    lcSource = lcSource + CHR(0) + CHR(0)
    lcTarget = CHR(0)
    *
    DECLARE INTEGER SHFileOperation IN SHELL32.DLL STRING @ LPSHFILEOPSTRUCT
    DECLARE INTEGER GetActiveWindow IN WIN32API
    IF "CLSHEAP" $ UPPER(SET("PROCEDURE"))

        *** this is available on UT's download ***
        SET PROCEDURE TO CLSHEAP ADDITIVE

    ENDIF
    loHeap = CREATEOBJ('Heap')
    lnHeap = loHeap.AllocBlob(lcSource)
    lnOptions = FOF_NOCONFIRMATION + FOF_ALLOWUNDO
    IF !tlSubfolders
        lnOptions = lnOptions + FOF_FILESONLY
    ENDIF
    lcFileOpStruct = NumToDWORD(GetActiveWindow()) + ;
        NumToDWORD(FO_DELETE) + ;
        NumToDWORD(lnHeap) + ;
        NumToDWORD(0) + ;
        NumToDWORD(lnOptions) + ;
        CHR(0) + ;
        NumToDWORD(0) + ;
        NumToDWORD(0)
    lnResult = SHFileOperation(lcFileOpStruct)
    IF lnResult # 0
        =MESSAGEBOX("There was a problem deleting folder contents.  Result code (" ;
            + TRANSFORM(lnResult) + ").", MB_OK+MB_ICONEXCLAMATION,MB_TITLE)
    ENDIF
    loHeap = .NULL.
    #UnDefine MB_TITLE
ENDPROC
I call the above with code like this:
Do Folder_Delete_Contents With lcFolderName, .T.
RMDIR (lcFolderName)
Hope this helps.

James Jernigan
Nashville, TN USA

>Hi all:
>
>I am attempting to first, delete an existing directory and all subfolders within that directory and then create a new directory with subdirectories where needed.
>
>I have tried the RMDIR and RD commands - but get a message that says "File Access is Denied". Clicking HELP tells me that I am trying to write to a protected file (whihc is not true).
>
>Any suggestions? Thank you in advance.
=======================
J Robert Jernigan
Calabasas, CA USA
jrjernigan@hotmail.com

"To err is human ... I should know, I get to show my humanity everyday!" jrj

"I just love the internet - it takes this great big planet and makes it smaller. Now, why can't we all just get along?" jrj
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform