Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Recycle Bin functions

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

  • Recycle Bin functions

    This demo shows 5 different Recycle Bin routines. If you have 2 icons at hand (one of a full recycle bin, the other of an empty one), you may include a resource file for cosmetics.
    Otherwise leave the metastatement #RESOURCE out, as well as the lines that load an icon.
    I want to thank Tom, Lance and Jules for their appreciated help.
    Code:
    ' code for Recycle.rc - you need bin-icons here
    ' Note: you need 2 icons: an empty bin and a full bin
    ' ICON1   ICON   EmptyBin.ico
    ' ICON2   ICON   FullBin.ico
    ' ---------------------------------------------------
     
    #COMPILE EXE
    #INCLUDE  "WIN32API.INC"
    #RESOURCE "Recycle.pbr"                    ' leave this out when you don't compile
                                               ' Recycle.rc
     
    %ID_FILESLIST  = %WM_USER + 1
    %ID_FILESLABEL = %WM_USER + 2
    %ID_ERASEBTN   = %WM_USER + 3
    %ID_METHOD1    = %WM_USER + 4
    %ID_METHOD2    = %WM_USER + 5
    %ID_METHOD3    = %WM_USER + 6
    %ID_METHOD4    = %WM_USER + 7
    %ID_METHOD5    = %WM_USER + 8
    %LBL_ICON      = %WM_USER + 9
     
    DECLARE FUNCTION GetPersonalTempPath() AS LONG
    DECLARE SUB CreateUselessFiles()
    DECLARE FUNCTION FindFiles(msk AS STRING) AS LONG
    DECLARE SUB MainDialog()
    DECLARE CALLBACK FUNCTION DlgCallBack()
    DECLARE CALLBACK FUNCTION CancelBtn()
    DECLARE CALLBACK FUNCTION EraseBtn()
    DECLARE FUNCTION RecycleBin(FileSelection AS STRING, CtrlID AS LONG) AS LONG
     
    GLOBAL SrcPath AS STRING, FileNameArray() AS STRING
     
    SUB CreateUselessFiles
      LOCAL count AS LONG, f AS STRING
    
      FOR count = 1 TO 20
        f = "zylema" + format$(count) + ".egb"
        OPEN SrcPath + "\" + f FOR OUTPUT AS 1
        PRINT #1, "Egbert Zijlema was here"
        CLOSE
      NEXT
    END SUB
     
    FUNCTION GetPersonalTempPath() AS LONG
      
      FUNCTION = %TRUE
      SrcPath = ENVIRON$("TEMP")
      IF SrcPath = "" THEN FUNCTION = %FALSE
    END FUNCTION
     
    FUNCTION FindFiles(msk AS STRING) AS LONG
      LOCAL fd AS WIN32_FIND_DATA
      LOCAL fh AS LONG, count AS LONG, yesno AS LONG
      LOCAL szMask AS ASCIIZ * %MAX_PATH
    
      FUNCTION = %FALSE
      szMask = msk
      fh = FindFirstFile(szMask, fd)
      IF fh = %INVALID_HANDLE_VALUE THEN EXIT FUNCTION
      DO
        IF fd.dwFileAttributes = %FILE_ATTRIBUTE_ARCHIVE THEN
          INCR count
          REDIM PRESERVE FileNameArray(1 : count)
          FileNameArray(count) = UCASE$(fd.cFileName)
        END IF
        yesno = FindNextFile(fh, fd)
      LOOP UNTIL yesno = 0
      FindClose fh
      FUNCTION = count
    END FUNCTION
     
    SUB MainDialog()
      LOCAL hDlg AS LONG, BtnStyle AS LONG
    
      BtnStyle =  %BS_LEFT OR %WS_TABSTOP
      DIALOG NEW 0, " Recycle Bin functions", , , 296, 220, %WS_CLIPCHILDREN OR _
                                                        %WS_MINIMIZEBOX OR _
                                                        %WS_SYSMENU TO hDlg
      CONTROL ADD LABEL, hDlg, %ID_FILESLABEL, SrcPath, 10, 10, 120, 10
      CONTROL ADD LISTBOX, hDlg, %ID_FILESLIST, FileNameArray(), 10, 22, 120, 180, _
                                                        %WS_TABSTOP OR %LBS_STANDARD OR _
                                                        %WS_VSCROLL OR %LBS_EXTENDEDSEL OR _
                                                        %LBS_NOTIFY OR %LBS_HASSTRINGS _
                                                        CALL EraseBtn
      CONTROL ADD BUTTON, hDlg, %ID_METHOD1, "Method &1        (Recycle Bin with confirmation)", _
                                                         140, 22, 140, 16, BtnStyle CALL EraseBtn
      CONTROL ADD BUTTON, hDlg, %ID_METHOD2, "Method &2        (Recycle Bin)", _
                                                         140, 40, 140, 16, BtnStyle CALL EraseBtn
      CONTROL ADD BUTTON, hDlg, %ID_METHOD3, "Method &3        (Delete with confirmation)", _
                                                         140, 58, 140, 16, BtnStyle CALL EraseBtn
      CONTROL ADD BUTTON, hDlg, %ID_METHOD4, "Method &4        (Delete)", _
                                                         140, 76, 140, 16, BtnStyle CALL EraseBtn
      CONTROL ADD BUTTON, hDlg, %ID_METHOD5, "Method &5        (Select multiple files)", _
                                                         140, 94, 140, 16, BtnStyle CALL EraseBtn
      CONTROL ADD BUTTON, hDlg, %IDCANCEL, "&Exit", 240, 176, 40, 16, %WS_TABSTOP CALL CancelBtn
      CONTROL ADD IMAGE,  hDlg, %LBL_ICON, "ICON1", 192, 176, 0, 0, %SS_ICON
      DIALOG SHOW MODAL hDlg, CALL DlgCallBack
    END SUB
     
    CALLBACK FUNCTION DlgCallBack()
      LOCAL hInst AS LONG
    
      SELECT CASE CBMSG
        CASE %WM_INITDIALOG
          CONTROL SET FOCUS CBHNDL, %ID_FILESLIST
          hInst = GetModuleHandle("")
          SendMessage CBHNDL, %WM_SETICON, %ICON_BIG, LoadIcon(hInst, "ICON1")
      END SELECT
    END FUNCTION
     
    CALLBACK FUNCTION CancelBtn()
      DIALOG END CBHNDL, 0
    END FUNCTION
     
    CALLBACK FUNCTION EraseBtn()
      LOCAL recycl AS LONG, items() AS LONG, count AS LONG
      LOCAL szElement AS ASCIIZ * %MAX_PATH, selection AS STRING
      STATIC result AS LONG
    
      SELECT CASE CBCTL
        CASE %ID_FILESLIST
          IF CBCTLMSG = %LBN_SELCHANGE THEN
            CONTROL SEND CBHNDL, %ID_FILESLIST, %LB_GETSELCOUNT, 0, 0 TO result
          END IF
        CASE %ID_METHOD1 TO %ID_METHOD5
          IF result = 0 THEN EXIT FUNCTION
          DIM items(1 : result)
          SendMessage GetDlgItem(CBHNDL, %ID_FILESLIST), %LB_GETSELITEMS, result, _
                                                          BYVAL VARPTR(items(1))
          FOR count = 1 TO result
            SendMessage GetDlgItem(CBHNDL, %ID_FILESLIST), %LB_GETTEXT, items(count), _
                                                          BYVAL VARPTR(szElement)
            selection = selection + SrcPath + "\" + szElement + CHR$(0)
          NEXT
          selection = selection + CHR$(0)
          recycl = RecycleBin(selection, CBCTL)
          IF ISFALSE(recycl) THEN
            SELECT CASE CBCTL
              CASE %ID_METHOD1, %ID_METHOD2, %ID_METHOD5
                CONTROL SET IMAGE CBHNDL, %LBL_ICON, "ICON2"
            END SELECT
            LISTBOX RESET CBHNDL, %ID_FILESLIST
            ERASE FileNameArray
            result = FindFiles(SrcPath + "\*.egb")
            IF result = 0 THEN
              MSGBOX "There are no more files in directory " + SrcPath + $CRLF + _
                     "Exit and rerun application", 64, "File(s) not found!"
              EXIT FUNCTION
            END IF
            dummy& = SendDlgItemMessage(CBHNDL, %ID_FILESLIST, %WM_SETREDRAW, %FALSE, 0)
            FOR count = 1 TO result
              LISTBOX ADD CBHNDL, %ID_FILESLIST, FileNameArray(count)
            NEXT
            dummy& = SendDlgItemMessage(CBHNDL, %ID_FILESLIST, %WM_SETREDRAW, %TRUE, 0)
          END IF
          result = 0
      END SELECT
    END FUNCTION
     
    FUNCTION RecycleBin(FilNam AS STRING, CtrlID AS LONG) AS LONG
      LOCAL shfo AS SHFILEOPSTRUCT                 ' predefined structure
      LOCAL caption AS STRING, DestDir AS STRING
    
      shfo.pFrom  = STRPTR(FilNam)                 ' pointer to file
      shfo.wFunc  = %FO_DELETE                     ' function: delete file
    
      SELECT CASE CtrlID
        CASE %ID_METHOD1
          shfo.fFlags = %FOF_ALLOWUNDO             ' to recycle bin
        CASE %ID_METHOD2
          shfo.fFlags = %FOF_ALLOWUNDO OR _
                        %FOF_NOCONFIRMATION        ' to recycle bin without confirmation dialog
        CASE %ID_METHOD3
          ' fFlags not set
          EXIT SELECT                              ' delete with confirmation dialog
        CASE %ID_METHOD4
          shfo.fFlags = %FOF_NOCONFIRMATION        ' delete without confirm
        CASE %ID_METHOD5
          shfo.fFlags = %FOF_ALLOWUNDO OR _
                        %FOF_SIMPLEPROGRESS
          caption = "Yes, I surely want my own message here"
          shfo.lpszProgressTitle = STRPTR(caption)
      END SELECT
      dummy& = SHFileOperation(shfo)               ' call funtion
      FUNCTION = shfo.fAnyOperationsAborted        ' return value, either 0 or non-zero
    END FUNCTION
     
    FUNCTION PBMain() AS LONG
    
      IF ISFALSE(GetPersonalTempPath) THEN
        MSGBOX "Your personal temp path cannot be found", 16, "Contact your system administrator"
        EXIT FUNCTION
      END IF
    
      IF ISFALSE(FindFiles(SrcPath + "\*.egb")) THEN
        CreateUselessFiles                         ' create ........
        dummy& = FindFiles(SrcPath + "\*.egb")     ' ..... and find them
      END IF
      MainDialog
    END FUNCTION
    ------------------
    mailto:[email protected][email protected]</A>
    www.basicguru.com/zijlema/

    [This message has been edited by Egbert Zijlema (edited February 08, 2001).]

    Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
    http://zijlema.basicguru.eu
    *** Opinions expressed here are not necessarily untrue ***
Working...
X