Send To Trash

Linux specific forum
User avatar
StarBootics
Enthusiast
Enthusiast
Posts: 703
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Send To Trash

Post 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
Last edited by StarBootics on Thu Mar 21, 2019 11:52 pm, edited 1 time in total.
The Stone Age did not end due to a shortage of stones !
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 452
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Send To Trash

Post 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.
Last edited by Sicro on Thu Mar 21, 2019 10:52 pm, edited 1 time in total.
Image
Why OpenSource should have a license :: DarkColor-Scheme for PureBasic
PureBasic-CodeArchiv-Rebirth: Git-Repository / Download – Any help is welcome!
Manjaro Xfce x64 (Main system) :: Windows 7 x64 (VirtualBox) :: Newest PureBasic version
wombats
Enthusiast
Enthusiast
Posts: 601
Joined: Thu Dec 29, 2011 5:03 pm

Re: Send To Trash

Post by wombats »

There is code by Shardik here for macOS.
User avatar
StarBootics
Enthusiast
Enthusiast
Posts: 703
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Send To Trash

Post 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
The Stone Age did not end due to a shortage of stones !
Post Reply