Link to home
Start Free TrialLog in
Avatar of dosyl
dosyl

asked on

Put file in the trassh can.

I took an example in a book, and try to work it but i have always the error SUB OR FUNCTION NOT DEFINED for w95Recycle. If i put the commondialog vb doesn't reconize FileOpen.

 Here is my code:

This Project call the modules.
Project Corbeil :

Private Sub Command1_Click()
      'utilise la corbeille de Windows et remplace
      'la commande Kill de VB
      ReDim Fichiers(1 To 1) As Variant
      'DlgTitle$ = "Sélection (multiple) de fichiers à supprimer"
      'Filter$ = "Tous Fichiers (*.*)|*.*"
      'SelectFich$ = FileOpen(DlgTitle$, Filter$, True)
      SelectFich$ = "C:\api\allo.csa"
      PathExtract = False
      Do While Len(SelectFich$) > 0
        'fichiers séparés par des caractères vides
        SepPos% = InStr(SelectFich$, " ")
        If SepPos% = 0 Then
            Counter% = Counter% + 1
            ReDim Preserve Fichiers(1 To Counter%)
            FichierSuivant$ = SelectFich$
            SelectFich$ = ""
            Fichiers(Counter%) = FichierSuivant$
          Else
            If PathExtract = False Then
                 Chemin$ = Mid$(SelectFich$, 1, SepPos% - 1)
                 SelectFich$ = Mid$(SelectFich$, SepPos% + 1)
                 'chemin au début de la chaîne
                 If Right$(Chemin$, 1) <> "\" Then Chemin$ = Chemin$ + "\"
                 PathExtract = True
               Else
                 'tous les autres morceaux sont des noms de fichier
                 FichierSuivant$ = Mid$(SelectFich$, 1, SepPos% - 1)
                 SelectFich$ = Mid$(SelectFich$, SepPos% + 1)
                 'redimensionnement du tableau, sans perte des données
                 Counter% = Counter% + 1
                 ReDim Preserve Fichiers(1 To Counter%)
                 Fichiers(Counter%) = FichierSuivant$
            End If
        End If
      Loop
      'à la Corbeille, on l'aime bien
      w95Recycle Me.hwnd, Fichiers()

End Sub

Project with modules :
Corbeil.bas :

Option Explicit

'fichiers à la corbeille
Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

Type SHFILEOPSTRUCT
  hwnd As Long
  wFunc As Long
  pFrom As String
  pTo As String
  fFlags As Integer
  fAnyOperationsAborted As Long
  hNameMappings As Long
  lpszProgressTitle As String
End Type

Corbeil.cls :

Public Sub w95Recycle(hwnd As Long, Fichiers() As Variant)
  'envoi de fichiers à la corbeille
  Dim SHFileOp As SHFILEOPSTRUCT
  Dim fList As String
  'tableau présent ?
  If LBound(Fichiers) <> 1 Then Exit Sub
  If UBound(Fichiers) = 0 Then Exit Sub
  For X% = LBound(Fichiers) To UBound(Fichiers)
    fList = fList & Fichiers(X%) & vbNullChar
  Next X%
  fList = fList & vbNullChar
  'remplissage de la structure
  With SHFileOp
    .hwnd = hwnd&
    .wFunc = &H3   'suppression
    .pFrom = fList 'liste des fichiers
    .fFlags = &H40 'annulation possible
    .lpszProgressTitle = "Supprimer..."
  End With
  R& = SHFileOperation(SHFileOp)
End Sub

If you have a better way to do this i can accept it.
Thank's a lot.
ASKER CERTIFIED SOLUTION
Avatar of MikeP090797
MikeP090797

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of spenner
spenner

If you want to send a file to the recycle bin, you can just move it to c:\recycled
He can't because the place of the recycled bin is different from computer to computer
Avatar of dosyl

ASKER

Dim fo As SHFILEOPSTRUCT

I have an error with this : Expected : End with statement.
Why?
Did you put the declarations at the top as well?
Avatar of dosyl

ASKER

'I wrted this in the class module:
Public Sub w95Recycle(hwnd As Long, Fichiers() As Variant)
  'envoi de fichiers à la corbeille
  Dim SHFileOp As SHFILEOPSTRUCT
  Dim fList As String
  'tableau présent ?
  If LBound(Fichiers) <> 1 Then Exit Sub
  If UBound(Fichiers) = 0 Then Exit Sub
  For X% = LBound(Fichiers) To UBound(Fichiers)
    fList = fList & Fichiers(X%) & vbNullChar
  Next X%
  fList = fList & vbNullChar
  'remplissage de la structure
  With SHFileOp
    .hwnd = hwnd&
    .wFunc = &H3   'suppression
    .pFrom = fList 'liste des fichiers
    .fFlags = &H40 'annulation possible
    .lpszProgressTitle = "Supprimer..."
  End With
  R& = SHFileOperation(SHFileOp)
End Sub


'PURPOSE:   Deletes files by sending them to recycled bin
'ARGUMENTS:
'   IN szFiles:         files to delete
'   IN bShowPrompt:     Specifies whether to ask the user to confirm the deletition
'   IN bShowProgress:   Specifies whether to display a progress information
Public Sub SendToRecycledBin(szFiles As String, Optional bShowPrompt As Boolean = True, Optional bShowProgress As Boolean = True)
       Dim fo as SHFILEOPSTRUCT      'it's write inred here
       fo.hwnd = 0&
       fo.wFunc = FO_DELETE
       fo.pFrom = szFiles
       fo.pTo = ""
       fo.fFlags = FOF_ALLOWUNDO Or IIf(bShowPrompt, 0, FOF_NOCONFIRMATION) Or IIf(bShowProgress, 0, FOF_SILENT)
       SHFileOperation fo
End Sub


Put the whole I gave you in a regular module (.bas)
Avatar of dosyl

ASKER

Ihave the same error : SendToRecycledBin not defined. Why? Why?
email me the project to michaelp@gezernet.co.il, i'll try to fix it
Avatar of dosyl

ASKER

Here is MikeP code  and it works :
*******************Corbeil.frm****************************

Private Sub Command1_Click()
      'utilise la corbeille de Windows et remplace
      'la commande Kill de VB
      ReDim Fichiers(1 To 1) As String
      DlgTitle$ = "Sélection (multiple) de fichiers à supprimer"
      Filter$ = "Tous Fichiers (*.*)|*.*"
      SelectFich$ = FileOpen(DlgTitle$, Filter$, True)
      PathExtract = False
      Do While Len(selectfich$) > 0
        'fichiers séparés par des caractères vides
        SepPos% = InStr(selectfich$, " ")
        If SepPos% = 0 Then
            Counter% = Counter% + 1
            ReDim Preserve Fichiers(1 To Counter%)
            FichierSuivant$ = selectfich$
            selectfich$ = ""
            Fichiers(Counter%) = FichierSuivant$
          Else
            If PathExtract = False Then
                 Chemin$ = Mid$(selectfich$, 1, SepPos% - 1)
                 selectfich$ = Mid$(selectfich$, SepPos% + 1)
                 'chemin au début de la chaîne
                 If Right$(Chemin$, 1) <> "\" Then Chemin$ = Chemin$ + "\"
                 PathExtract = True
               Else
                 'tous les autres morceaux sont des noms de fichier
                 FichierSuivant$ = Mid$(selectfich$, 1, SepPos% - 1)
                 selectfich$ = Mid$(selectfich$, SepPos% + 1)
                 'redimensionnement du tableau, sans perte des données
                 Counter% = Counter% + 1
                 ReDim Preserve Fichiers(1 To Counter%)
                 Fichiers(Counter%) = FichierSuivant$
            End If
        End If
      Loop
      'à la Corbeille, on l'aime bien
 
SendToRecycledBin Fichiers(Counter%)
End Sub

*****************Corbeil.bas*************************
Option Explicit

'fichiers à la corbeille
Public Type SHFILEOPSTRUCT
  hwnd As Long
  wFunc As Long
  pFrom As String
  pTo As String
  fFlags As Integer
  fAnyOperationsAborted As Long
  hNameMappings As Long
  lpszProgressTitle As String
End Type

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

Private Const FO_DELETE = &H3 'Send files to recycled bin
Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_SILENT = &H4
Private Const FOF_NOCONFIRMATION = &H10

'PURPOSE:   Deletes files by sending them to recycled bin
'ARGUMENTS:
'   IN szFiles:         files to delete
'   IN bShowPrompt:     Specifies whether to ask the user to confirm the deletition
'   IN bShowProgress:   Specifies whether to display a progress information
Public Sub SendToRecycledBin(szFiles As String, Optional bShowPrompt As Boolean = True, Optional bShowProgress As Boolean = True)
Dim fo As SHFILEOPSTRUCT
fo.hwnd = 0&
fo.wFunc = FO_DELETE
fo.pFrom = szFiles
fo.pTo = ""
fo.fFlags = FOF_ALLOWUNDO Or IIf(bShowPrompt, 0, FOF_NOCONFIRMATION) Or IIf(bShowProgress, 0, FOF_SILENT)
Dim x As Long
x = SHFileOperation(fo)
End Sub