• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

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.
0
dosyl
Asked:
dosyl
  • 5
  • 4
1 Solution
 
MikeP090797Commented:

You can use this one:


Type SHFILEOPSTRUCT     'Send files to recycled bin
        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
Public Const FO_DELETE = &H3      'Send files to recycled bin
Public Const FOF_ALLOWUNDO = &H40
Public Const FOF_SILENT = &H4
Public Const FOF_NOCONFIRMATION = &H10

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

'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)
       SHFileOperation fo
End Sub

0
 
spennerCommented:
If you want to send a file to the recycle bin, you can just move it to c:\recycled
0
 
MikeP090797Commented:
He can't because the place of the recycled bin is different from computer to computer
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dosylAuthor Commented:
Dim fo As SHFILEOPSTRUCT

I have an error with this : Expected : End with statement.
Why?
0
 
MikeP090797Commented:
Did you put the declarations at the top as well?
0
 
dosylAuthor Commented:
'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


0
 
MikeP090797Commented:
Put the whole I gave you in a regular module (.bas)
0
 
dosylAuthor Commented:
Ihave the same error : SendToRecycledBin not defined. Why? Why?
0
 
MikeP090797Commented:
email me the project to michaelp@gezernet.co.il, i'll try to fix it
0
 
dosylAuthor Commented:
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

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now