Solved

Put file in the trassh can.

Posted on 1999-01-29
10
192 Views
Last Modified: 2010-05-03
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
Comment
Question by:dosyl
  • 5
  • 4
10 Comments
 
LVL 8

Accepted Solution

by:
MikeP090797 earned 100 total points
Comment Utility

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
 
LVL 2

Expert Comment

by:spenner
Comment Utility
If you want to send a file to the recycle bin, you can just move it to c:\recycled
0
 
LVL 8

Expert Comment

by:MikeP090797
Comment Utility
He can't because the place of the recycled bin is different from computer to computer
0
 
LVL 1

Author Comment

by:dosyl
Comment Utility
Dim fo As SHFILEOPSTRUCT

I have an error with this : Expected : End with statement.
Why?
0
 
LVL 8

Expert Comment

by:MikeP090797
Comment Utility
Did you put the declarations at the top as well?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Author Comment

by:dosyl
Comment Utility
'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
 
LVL 8

Expert Comment

by:MikeP090797
Comment Utility
Put the whole I gave you in a regular module (.bas)
0
 
LVL 1

Author Comment

by:dosyl
Comment Utility
Ihave the same error : SendToRecycledBin not defined. Why? Why?
0
 
LVL 8

Expert Comment

by:MikeP090797
Comment Utility
email me the project to michaelp@gezernet.co.il, i'll try to fix it
0
 
LVL 1

Author Comment

by:dosyl
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now