October 28, 2020, 05:59:58 AM

Author Topic: [bmx] Delete to Recycle Bin by JoshK [ 1+ years ago ]  (Read 1369 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Delete to Recycle Bin by JoshK [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Delete to Recycle Bin
Author : JoshK
Posted : 1+ years ago

Description : Set the optional permanent argument to True to perform a conventional file deletion.  Windows only, at the moment.

Code :
Code: BlitzMax
  1. SuperStrict
  2.  
  3. ?win32
  4. Import pub.win32
  5.  
  6. Type SHFILEOPSTRUCT
  7.         Field hwnd:Int
  8.         Field wFunc:Int
  9.         Field pFrom:Int
  10.         Field pTo:Byte Ptr
  11.         Field fFlags:Int
  12.         Field fAnyOperationsAborted:Int
  13.         Field hNameMappings:Int
  14.         Field lpszProgressTitle:Int
  15. EndType
  16.  
  17. Extern "win32"
  18.         Function SHFileOperation:Int(lpFileOp:Byte Ptr)
  19. EndExtern
  20.  
  21. Const DE_SAMEFILE     :Int = $71
  22. Const DE_MANYSRC1DEST :Int = $72
  23. Const DE_OPCANCELLED  :Int = $75
  24. Const DE_DESTSUBTREE  :Int = $76
  25. Const DE_INVALIDFILES :Int = $7C
  26. Const DE_DESTSAMETREE :Int = $7D
  27. Const DE_DIFFDIR       :Int = $73
  28. Const DE_FILEDESTISFLD :Int = $80
  29.  
  30. Const FO_DELETE:Int = $3
  31. Const FOF_ALLOWUNDO:Int = $40
  32. Const FOF_NOCONFIRMATION:Int = $10
  33. Const FOF_SILENT:Int = $4
  34. ?
  35.  
  36. Function DeleteDir:Int(filename:String,recursive:Int=False,permanent:Int=False)
  37.         Local dir:String[]
  38.         Local file:String
  39.        
  40.         If permanent Return BRL.FileSystem.DeleteDir(filename,recursive)
  41.         If FileType(filename)<>2 Return False
  42.         If Not recursive
  43.                 dir=LoadDir(filename)
  44.                 If dir.length Return False
  45.         EndIf
  46.         Return DeleteFile(filename)
  47. EndFunction
  48.  
  49. Function DeleteFile:Int(filename:String,permanent:Int=False)
  50. ?macos
  51.         Return BRL.FileSystem.DeleteFile(filename)
  52. ?
  53. ?linux
  54.         Return BRL.FileSystem.DeleteFile(filename)
  55. ?
  56. ?win32
  57.         'CallWindowProc function
  58.         'http://msdn.microsoft.com/en-us/library/windows/Desktop/ms633571%28v=vs.85%29.aspx
  59.        
  60.         'SHFileOperation function
  61.         'http://msdn.microsoft.com/en-us/library/windows/Desktop/bb762164%28v=vs.85%29.aspx
  62.        
  63.         'SHFILEOPSTRUCT structure
  64.         'http://msdn.microsoft.com/en-us/library/windows/desktop/bb759795%28v=vs.85%29.aspx
  65.        
  66.         Local lpFileOp:SHFILEOPSTRUCT=New SHFILEOPSTRUCT
  67.         Local sfrom:Short[]
  68.         Local sto:Short[]
  69.         Local n:Int
  70.         Local str_addr:Int
  71.         Local asm:Byte[5]
  72.         Local result:Int
  73.         Local doublenulltermstring:Byte[]
  74.        
  75.         If permanent Return BRL.FileSystem.DeleteFile(filename)
  76.        
  77.         filename=RealPath(filename).Replace("","/")
  78.        
  79.         'We have to get some funny unicode string
  80.         asm[0]=$8b
  81.         asm[1]=$44
  82.         asm[2]=$24
  83.         asm[3]=$10
  84.         asm[4]=$c3
  85.        
  86.         doublenulltermstring=doublenulltermstring[..filename.length+2]
  87.         For n=0 To filename.length-1
  88.                 doublenulltermstring[n] = filename[n]
  89.         Next
  90.         doublenulltermstring[doublenulltermstring.length-2]=0
  91.         doublenulltermstring[doublenulltermstring.length-1]=0
  92.         str_addr = CallWindowProcA(asm,0,0,0,Int(Varptr doublenulltermstring[0]))
  93.        
  94.         lpFileOp.wFunc=FO_DELETE
  95.         lpFileOp.pFrom=str_addr
  96.         lpFileOp.fFlags=FOF_ALLOWUNDO|FOF_SILENT|FOF_NOCONFIRMATION
  97.        
  98.         result=SHFileOperation(lpFileOp)
  99.         If result=0 Return True Else Return False
  100. ?
  101. EndFunction
  102.  
  103. Local path:String = "test.txt"
  104.  
  105. If DeleteFile(path)
  106.         Print "sent "+path+" to Bin"
  107. Else
  108.         Print "problem sending file "+path+" to bin"
  109. EndIf


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal