It is currently Tue Oct 22, 2019 5:03 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Send To Trash
PostPosted: Thu Mar 21, 2019 9:08 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Jul 07, 2013 11:35 am
Posts: 409
Location: Canada
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

_________________
The Stone Age did not end due to a shortage of stones !


Last edited by StarBootics on Thu Mar 21, 2019 11:52 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Send To Trash
PostPosted: Thu Mar 21, 2019 10:30 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jun 25, 2014 5:25 pm
Posts: 369
Location: Germany
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.

_________________
Image
Why OpenSource should have a license
PureBasic-CodeArchiv-Rebirth: Git-Repository / Download -- Any help is welcome!
Manjaro Xfce x64 (Main system) :: WindowsXP/Xubuntu x86 (VirtualBox) :: PureBasic (Linux: x86/x64, Windows: x86) :: All are up to date


Last edited by Sicro on Thu Mar 21, 2019 10:52 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Send To Trash
PostPosted: Thu Mar 21, 2019 10:52 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu Dec 29, 2011 5:03 pm
Posts: 466
There is code by Shardik here for macOS.


Top
 Profile  
Reply with quote  
 Post subject: Re: Send To Trash
PostPosted: Fri Mar 22, 2019 1:59 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Jul 07, 2013 11:35 am
Posts: 409
Location: Canada
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 !


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye