Page 1 of 1

Send To Trash

Posted: Thu Mar 21, 2019 9:08 pm
by StarBootics
Hello everyone,

According to https://developer.gnome.org/gio/stable/gio.html I know that the following command used in the terminal work just fine :

Code: Select all

gio trash 'filename'
So I'm trying to send a file to the trash can using RunProgram() but it didn't work.

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : SendToTrash - Module
; File Name : SendToTrash - Module.pb
; File version: 0.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 11-08-2018
; Last Update : 21-03-2019
; PureBasic code : V5.70 LTS
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule SendToTrash
  
  Declare DoIt(FileName.s)
  
EndDeclareModule

Module SendToTrash
  
  Procedure DoIt(FileName.s)
    
    CompilerSelect #PB_Compiler_OS
        
      CompilerCase #PB_OS_Windows 
        
        Protected lpFileOp.SHFILEOPSTRUCT
        
        If FileSize(FileName) <> -1
          
          If Right(FileName, 1) = "\"
            FileName = Left(FileName, Len(FileName) - 1)
          EndIf
          
          Memory = AllocateMemory((Len(FileName)+2)*SizeOf(Character), 0)
          
          If Memory
            
            lpFileOp\hwnd = 0
            lpFileOp\pTo = 0
            lpFileOp\wFunc = #FO_DELETE
            lpFileOp\pFrom = Memory
            lpFileOp\fFlags = #FOF_ALLOWUNDO | #FOF_NOCONFIRMATION
            
            PokeS(Memory, FileName)
            SHFileOperation_(@lpFileOp)
            FreeMemory(Memory)
            
          EndIf
          
        EndIf
        
      CompilerCase #PB_OS_Linux
        If FileSize(FileName) <> -1
          RunProgram("gio", "trash " + #DQUOTE$ + FileName + #DQUOTE$, "")
        EndIf
        
      CompilerCase #PB_OS_MacOS
        
    CompilerEndSelect
    
  EndProcedure
  
EndModule

CompilerIf #PB_Compiler_IsMainFile 
  
  If CreateFile(0, GetCurrentDirectory() + "dummy.txt")
    
    WriteString(0, "dummy text in a file to be deleted")
    CloseFile(0)
    
    SendToTrash::DoIt(GetCurrentDirectory() + "dummy.txt")
    
  EndIf
  
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Does anyone have a clue about how to make it to work ?

Thanks beforehand.
StarBootics

Re: Send To Trash

Posted: Thu Mar 21, 2019 10:30 pm
by Sicro
Thanks 8)

This works:

Code: Select all

RunProgram("gio", "trash " + #DQUOTE$ + FileName + #DQUOTE$, "")
By the way, this code also works with directories, not only with files.

Now, only MacOS is missing.

But why you using a module for only one procedure? I find it superfluous.

Re: Send To Trash

Posted: Thu Mar 21, 2019 10:52 pm
by wombats
There is code by Shardik here for macOS.

Re: Send To Trash

Posted: Fri Mar 22, 2019 1:59 am
by StarBootics
Sicro wrote:But why you using a module for only one procedure? I find it superfluous.
Because I'm a Module Addict, all my programs are made of modules so it's more convenient to have module even if the Module has a tiny instruction.

Thanks for your snippet.

Best regards
StarBootics