PureBasic Forum
https://www.purebasic.fr/english/

Send To Trash
https://www.purebasic.fr/english/viewtopic.php?f=15&t=72508
Page 1 of 1

Author:  StarBootics [ Thu Mar 21, 2019 9:08 pm ]
Post subject:  Send To Trash

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:
gio trash 'filename'

So I'm trying to send a file to the trash can using RunProgram() but it didn't work.
Code:
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; 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

Author:  Sicro [ Thu Mar 21, 2019 10:30 pm ]
Post subject:  Re: Send To Trash

Thanks 8)

This works:
Code:
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.

Author:  wombats [ Thu Mar 21, 2019 10:52 pm ]
Post subject:  Re: Send To Trash

There is code by Shardik here for macOS.

Author:  StarBootics [ Fri Mar 22, 2019 1:59 am ]
Post subject:  Re: Send To Trash

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

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/