Announcement

Collapse
No announcement yet.

Move a folder to the recycle bin

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Move a folder to the recycle bin

    I can programmatically move files to the recycle bin.
    I would like to programmatically move folders to the recycle bin.
    The "SHFILEOPSTRUCT structure" seems to be for files only.

    Any help would be appreciated.
    Trento Castricone
    www.fileraptor.com
    [email protected]

  • #2
    My code works fine. (Yes I had to tweak the flags)
    Code:
    ' Test_deletefolder_rb
    ' 08.22.09
    ' ShFileOperation does not handle delete of folders to recycle bin?
    ' I don't believe that. I should have, it's true,
    ' Using FOF_WANTNUKEWARNING (when requested)  does come up
    ' what if... FOF_ALLOWUNDO is used?  Goes to recycle bin!
    
    #COMPILE EXE
    #DIM     ALL
    
    
    #INCLUDE "WIN32API.INC"
    
    
    FUNCTION PBMAIN () AS LONG
        
      LOCAL  szFolder AS ASCIIZ * %MAX_PATH
      LOCAL  SHFO  AS SHFILEOPSTRUCT
      LOCAL  iRet AS LONG
    
    
      RESET            szFolder
      szFolder      =  "D:\Testdata\DeleteMe"
      RESET            SHFO
      SHFO.hWnd     =  GetDesktopWindow()
      SHFO.wFunc    =  %FO_DELETE
      SHFO.pFrom    =  VARPTR(szFolder)
      SHFO.fFlags   =  %FOF_WANTNUKEWARNING OR %FOF_ALLOWUNDO
    
    ' SDK doc:
    ' " If pFrom is set to a file name, deleting the file with FO_DELETE will not move it to the Recycle Bin,
    ' " even if the FOF_ALLOWUNDO flag is set. You must use a full path.
    
      iRet = SHFileOperation (SHFO)
      MSGBOX  USING$ ("SHFO returns #   &", iRet, IIF$ (iRet, "Error", "Success"))
      
    END FUNCTION
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Move folder to recycle bin

      Michael

      Thanks - works!
      I don't think I would have ever accomplished this one on my own.

      (Requires no trailing backslash)
      Trento Castricone
      www.fileraptor.com
      [email protected]

      Comment


      • #4
        >I don't think I would have ever accomplished this one on my own.

        Oh, come on.

        All you would have had to do was try a couple of different flags and see what happened. Hell, that's how I did it. Took me maybe ten minutes, tops.

        Or, if your problem was really...
        >Requires no trailing backslash

        .. then....
        A. You didn't check the return code from ShFileOperation, did you? (code not shown)
        B. Windows NEVER wants a trailing backslash when supplying a folder name.

        MCM
        Last edited by Michael Mattias; 22 Aug 2009, 11:21 AM.
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          Michael
          FOF_WANTNUKEWARNINGVersion 5.0. Send a warning if a file is being permanently destroyed during a delete operation rather than recycled. This flag partially overrides FOF_NOCONFIRMATION.
          I guess I just didn't understand the above verbiage.

          Thanks again
          Trento Castricone
          www.fileraptor.com
          [email protected]

          Comment


          • #6
            >I guess I just didn't understand the above verbiage

            I didn't either. But at least I showed my code.....

            ...which includes in the comments a "hint" that FOF_WANTNUKEWARNING is NOT the important flag here!!!!

            MCM
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              This is the code I am using for "FILES".
              I had NO code for "DIRECTORIES"


              Code:
              FUNCTION RecycleBinSilent(BYVAL FileName AS STRING) AS LONG
                LOCAL shfo AS SHFILEOPSTRUCT
                LOCAL szSource AS ASCIIZ * %MAX_PATH
                LOCAL lResult AS LONG
                RESET        szSource
                szSource     = FileName
                RESET        SHFO
                SHFO.hWnd    =  GetDesktopWindow()
                shfo.wFunc   = %FO_DELETE
                shfo.pFrom   = VARPTR(szSource)
                shfo.fFlags  = %FOF_ALLOWUNDO OR %FOF_NOCONFIRMATION 'OR %FOF_SIMPLEPROGRESS
                lResult = SHFileOperation(shfo)
                FUNCTION = shfo.fAnyOperationsAborted
              END FUNCTION
              Trento Castricone
              www.fileraptor.com
              [email protected]

              Comment


              • #8
                Originally posted by Michael Mattias View Post
                B. Windows NEVER wants a trailing backslash when supplying a folder name.

                MCM

                That's like saying "There are no absolutes"...just in saying it makes an absolute. You also forgot about the root "Folder" which is a \. I too forgot about it a couple times and now always remember to handle its special case.
                sigpic
                Mobile Solutions
                Sys Analyst and Development

                Comment


                • #9
                  Trento,

                  Is Reset necessary in your code? Since both variables that it's applied to are local and not STATIC, why did you reset them?

                  Just wondering.

                  Also, I just noticed from MCM's response that the file name must be the complete pathname. I ran your code with something like "myfile.txt" but until I used exe.path$ + "myfile.txt", the deleted file did not show in the recycle bin. It took me a minute to realize the his/your codes were the same (flags different) and applied to both files/folders.

                  Also I see that when a folder is deleted, it need not be empty - useful and dangerous.

                  Thanks to both of your for posting the code. I had been wanting just that snippet!
                  Last edited by Gary Beene; 22 Aug 2009, 11:02 PM.

                  Comment


                  • #10
                    http://www.powerbasic.com/support/pb...ad.php?t=17250
                    There are no atheists in a fox hole or the morning of a math test.
                    If my flag offends you, I'll help you pack.

                    Comment


                    • #11
                      Mel,

                      Thanks for the link. I had looked a few weeks ago and didn't see your post. Now that you point it out - there it is in plain sight.

                      I've said before that finding the right keywords can be troublesome, but in this case I don't see how I'd have missed it (except that it showed up off the first page of search results) - there are only a few ways to type "recycle bin".

                      ... updated ... Wow, and now I see that it wasn't in PBWin. I've been guilty of thinking console forum stuff is not useful to PBWin apps, but now I know better. I'll need to go browse around that forum to see what I missed that I might find useful.

                      Thanks!
                      Last edited by Gary Beene; 22 Aug 2009, 11:12 PM.

                      Comment


                      • #12
                        Is Reset necessary in your code? Since both variables that it's applied to are local and not STATIC, why did you reset them
                        No, and force of habit.

                        As a rule I'm a control freak and don't like putting total faith in defaults.

                        I also use a lot of 'unnecessary' parentheses, eschewing the operator precedence rules which should result in the order I want. Of course this also is as mich for my own benefit, so I can look at code six months later and know what I did without the need to look up those operator precedence rules.
                        Last edited by Michael Mattias; 23 Aug 2009, 08:43 AM.
                        Michael Mattias
                        Tal Systems (retired)
                        Port Washington WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          Originally posted by Michael Mattias View Post
                          ...As a rule I'm a control freak...
                          As a rule MM, you shouldn't open yourself up like that.
                          There are no atheists in a fox hole or the morning of a math test.
                          If my flag offends you, I'll help you pack.

                          Comment


                          • #14
                            >you shouldn't open yourself up like that

                            Over the years I have acknowledged other personal history and attributes a lot worse than being a control freak.

                            I'll live.

                            (Curious? Go find 'em yourself using the search. That ought to keep you busy for a couple of hours).

                            MCM
                            Michael Mattias
                            Tal Systems (retired)
                            Port Washington WI USA
                            [email protected]
                            http://www.talsystems.com

                            Comment


                            • #15
                              Gary

                              My problem was the trailing back slash on the path to delete folders.

                              As far as the RESET instruction goes:
                              Michael had it in his code, I read the documentation and then used it.
                              It seems to work just fine with or without RESET.

                              Maybe Michael can answer - why.
                              Trento Castricone
                              www.fileraptor.com
                              [email protected]

                              Comment


                              • #16
                                >Maybe Michael can answer - why [he used RESET]

                                I gave one answer in post#12 but if you don't like that one, you can use this:

                                "To give you something to keep you awake at night, wondering."
                                Michael Mattias
                                Tal Systems (retired)
                                Port Washington WI USA
                                [email protected]
                                http://www.talsystems.com

                                Comment

                                Working...
                                X