B4J Question Delete to Windows Recycle Bin

Sifu

Active Member
Hi,

File.Delete(FilePath,FileName) deletes a file directly from hard-disk, similar as holding Shift and then delete.
I searched but can't find nothing on how to delete to Windows Recycle bin. (For sure I'm not the only one asking this right?)

So I asked GPTchat and it came up with 2 scripts where I'm playing with, but both do not work and are giving errors.

How to correctly delete a File to Windows Recycle bin instead of having it lost immediately forever?

If you want to play with the 2 solutions AI came up with by using shell32, please do so, cause I don't know where the error comes from or how to solve it correctly.
Any other or better solution is welcomed.

Thanks!
 

Attachments

  • filedeletetest.zip
    3.3 KB · Views: 112
  • 2filedeletetest.zip
    3.4 KB · Views: 134

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
#AdditionalJar: jna-platform-5.8.0.jar
#AdditionalJar: jna-5.8.0.jar
Sub Process_Globals
   
End Sub

Sub AppStart (Args() As String)
    MoveToRecycleBin(Array("C:\Users\H\Downloads\Error.txt"))
End Sub

'File - one or more files to move to the recycle bin.
Private Sub MoveToRecycleBin (Files() As Object)
    Dim FileUtils As JavaObject
    FileUtils = FileUtils.InitializeStatic("com.sun.jna.platform.FileUtils").RunMethod("getInstance", Null)
    If FileUtils.RunMethod("hasTrash", Null) Then
        Dim JavaFiles(Files.Length) As Object
        For i = 0 To Files.Length - 1
            Dim jo As JavaObject
            jo.InitializeNewInstance("java.io.File", Array(Files(i)))
            JavaFiles(i) = jo
        Next
        Dim ArrayOfFiles As JavaObject
        ArrayOfFiles.InitializeArray("java.io.File", JavaFiles)
        FileUtils.RunMethod("moveToTrash", Array(ArrayOfFiles))
        Log("files moved to trash")
    Else
        Log("no trash")
    End If
End Sub

Download and copy to additional libs folder:
- https://repo1.maven.org/maven2/net/java/dev/jna/jna-platform/5.8.0/jna-platform-5.8.0.jar
- https://repo1.maven.org/maven2/net/java/dev/jna/jna/5.8.0/jna-5.8.0.jar
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
As an alternative AWT has a 'moveToTrash' function in Desktop, in case you do not want to add JNA jars to your build.
B4X:
    Dim jo As JavaObject
    jo.InitializeNewInstance("java.io.File", Array(File.DirApp & "/dummy.txt"))
    Log("moving " & $"${jo.RunMethod("getCanonicalPath",Null)}"$ &" to recycle bin")
    Dim desktop As JavaObject
    desktop.InitializeStatic("java.awt.Desktop").RunMethodJO("getDesktop",Null).RunMethod("moveToTrash",Array(jo))

But I like Erels version as it recycles more than one file at a time.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
The code I posted in post #3 is just the bare bones and contains no error checking. (It could be run on a system with no Recycle bin or in a headless state)

B4X:
    Dim jo, desktop, action As JavaObject
    jo.InitializeNewInstance("java.io.File", Array(File.DirApp & "/dummy.txt"))
    
    action.InitializeStatic("java.awt.Desktop.Action")

    desktop = desktop.InitializeStatic("java.awt.Desktop").RunMethodJO("getDesktop",Null)

    If desktop.RunMethod("isSupported",Array(action.GetField("MOVE_TO_TRASH"))) Then
        Log("moving " & $"${jo.RunMethod("getCanonicalPath",Null)}"$ &" to recycle bin")
        desktop.RunMethod("moveToTrash",Array(jo))
    Else
        Log("deleting " & $"${jo.RunMethod("getCanonicalPath",Null)}"$ &"(No recycle bin)")
        File.Delete(File.DirApp,"/dummy.txt")
    End If
 
Upvote 0

Sifu

Active Member
Thanks!
I have my application made now so the user can choose if they want to delete forever or first to Recycle bin.
These scripts are welcomed by others probably too I assume, as I could not find actually any solution yet for this.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…