Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Recycle File
#1
Code: (Select All)
Screen _NewImage(800, 600, 32)

Open "MyFile.txt" For Output As #1
Print #1, "Test:" + Date$ + " " + Time$
Close
Print "File created called MyFile.txt"

Recycle "MyFile.txt"
Print "MyFile.txt should now be recycled and in the Recycle Bin, instead of just erased permanently."
Print "Check your recycle bin to see if it exists there and contains a simple message of 'Test'."
End

Sub Recycle (file$)
If _FileExists(file$) Then
$If WIN Then
ps$ = "powershell -NoLogo -NoProfile -Command "
ps$ = ps$ + Chr$(34) + "Add-Type -AssemblyName Microsoft.VisualBasic; "
ps$ = ps$ + "[Microsoft.VisualBasic.FileIO.FileSystem]:eleteFile('"
ps$ = ps$ + file$
ps$ = ps$ + "','OnlyErrorDialogs','SendToRecycleBin')" + Chr$(34)
Shell _Hide ps$
$ElseIf MAC Then
Shell _Hide "mv " + Chr$(34) + file$ + Chr$(34) + " ~/.Trash/"
If _FileExists(file$) Then 'a back up second method as per here --> https://www.macterminal.cc/answers/mv-command-examples
message$ = "File failed to move to trash folder. Possible reasons for this are:" + Chr$(10)
message$ = message$ + Chr$(10)
message$ = message$ + "1. Missing Full Disk Access" + Chr$(10)
message$ = message$ + "By default, the Terminal does not have permission to modify 'protected' folders like ~/.Trash/, leading to 'Operation not permitted' errors. "
message$ = message$ + Chr$(10)
message$ = message$ + "Fix: Go to System Settings > Privacy & Security > Full Disk Access and toggle the switch for Terminal to ON." + Chr$(10)
message$ = message$ + Chr$(10)
message$ = message$ + Chr$(10)
message$ = message$ + "2. Incorrect Trash Path for External Drives" + Chr$(10)
message$ = message$ + "Each drive on a Mac has its own hidden trash folder. If the file you are moving is on an external drive, ~/.Trash/ (which is on your internal startup disk) might not be the correct destination for a simple 'move'." + Chr$(10)
message$ = message$ + Chr$(10)
message$ = message$ + "Fix: For files on external volumes, the path is usually /Volumes/[DriveName]/.Trashes/[UserUID]/." + Chr$(10)
message$ = message$ + Chr$(10)
message$ = message$ + Chr$(10)
message$ = message$ + "3. Folder Ownership or Corruption" + Chr$(10)
message$ = message$ + "Sometimes the .Trash directory itself has incorrect permissions or has become corrupted." + Chr$(10)
message$ = message$ + Chr$(10)
message$ = message$ + "Verify: Run ls -ld ~/.Trash to see the permissions. It should be owned by your username with rwx permissions." + Chr$(10)
message$ = message$ + "Fix: If it's missing or broken, you can recreate it by running mkdir ~/.Trash (though you may need to delete a corrupted one first with sudo rm -rf ~/.Trash)." + Chr$(10)
_MessageBox "File failed to move to trash", message$, "info"
End If
$Else
'Assume Linux-like environment with gio available
SHELL _HIDE "gio trash " + CHR$(34) + file$ + CHR$(34)
$End If
End If
End Sub


A simple routine which should send a file to the recycle bin, rather than just destroy it utterly like it does with KILL or a shell to DEL or ERASE.  Short, simple, and easy enough to toss into any program which needs to make use of it, without a lot of dependencies and such attached to working with it.


Edit: Updated to hopefully work on Mac and Linux.  At least according to others who use Mac and Linux, *they* claim this works.  I'm a windows guy myself, so I'm just trusting them.  I can't actually test it on my own to be certain, so try it out and if it fails, fuss at them! 

:D
Reply
#2
Update:  Had to disable similies in the code above.  It was scattering Big Grin in the middle of the code box of text!  LOL!!  If anyone grabbed it and tried it and it failed to work, try it again now that the letters are letters and not big smiling faces in the middle of the code.
Reply
#3
Updated with a Mac and Linux version which... should... hopefully work.  I don't have an actual mac or linux machine for testing, but it *seems* like this should work for them.  If not...   

I'm more than welcome to suggestions from people who actually have the proper machines to code and test things on to give their feedback and opinions on this stuff!  It's purely hypothetical as far as I'm concerned.  It *should* work... I think... But without testing on an actual machine...  

Who knows?!
Reply
#4
I gave it a try on my Mac. It create the file and put it in the QB folder, but it didn't delete it.
Reply
#5
A quick search brought me this. The Terminal command to move a file to the Recycle Bin.

> trash my file.txt.   (where '>' is the command prompt)

And open a Terminal window with the SHELL command in QB64
Reply
#6
I'm not a MAC guru but I consulted one I know on another board.
Lets try this !


Code: (Select All)
Screen _NewImage(800, 600, 32)

Open "MyFile.txt" For Output As #1
Print #1, "Test:" + Date$ + " " + Time$
Close
Print "File created called MyFile.txt"

Recycle "MyFile.txt"
Print "MyFile.txt should now be recycled and in the Recycle Bin, instead of just erased permanently."
Print "Check your recycle bin to see if it exists there and contains a simple message of 'Test'."
End

Sub Recycle (file$)
    If _FileExists(file$) Then
        $If WIN Then
            ps$ = "powershell -NoLogo -NoProfile -Command "
            ps$ = ps$ + Chr$(34) + "Add-Type -AssemblyName Microsoft.VisualBasic; "
            ps$ = ps$ + "[Microsoft.VisualBasic.FileIO.FileSystem]:Big GrineleteFile('"
            ps$ = ps$ + file$
            ps$ = ps$ + "','OnlyErrorDialogs','SendToRecycleBin')" + Chr$(34)
            Shell _Hide ps$
        $ElseIf MAC Then
            ' Build an osascript command that tells Finder to delete the POSIX file (move to Trash)
            ' Use single quotes around the whole -e argument in the shell, and escape inner double quotes for AppleScript.
            cmd$ = "osascript -e 'tell application " + Chr$(34) + "Finder" + Chr$(34)
            cmd$ = cmd$ + " to move POSIX file "
            cmd$ = cmd$ + Chr$(34) + file$ + Chr$(34) + " to trash'"
            Shell _Hide cmd$
        $Else
            'Assume Linux-like environment with gio available
            Shell _Hide "gio trash " + Chr$(34) + file$ + Chr$(34)
        $End If
    End If
End Sub
Reply
#7
(02-28-2026, 12:36 AM)NakedApe Wrote: I gave it a try on my Mac. It create the file and put it in the QB folder, but it didn't delete it.

Try my variant !
Reply
#8
I was worried about Mac.  It has rules about what needs to be quoted and double quoted, and without a machine to test it on, I can't get the syntax perfectly right with it.

From what I understand, trash doesn't come in Mac natively and has to be installed independently to work on all OSes.  

This is what it *should* look like to work:

Code: (Select All)
function del() {
if [[ $# ]]; then
  a=()
  for f in "$@"; do
   a+=("$(realpath "$f")")
  done
  f=$(printf "\",POSIX file \"%s" "${a[@]}")\"
  osascript -ss -e"tell app \"Finder\" to delete {${f:2}}" 1>/dev/null
fi
}


Code: (Select All)

Screen _NewImage(800, 600, 32)

Open "MyFile.txt" For Output As #1
Print #1, "Test:" + Date$ + " " + Time$
Close
Print "File created called MyFile.txt"

Recycle "MyFile.txt"
Print "MyFile.txt should now be recycled and in the Recycle Bin, instead of just erased permanently."
Print "Check your recycle bin to see if it exists there and contains a simple message of 'Test'."
End

Sub Recycle (file$)
If _FileExists(file$) Then
$If WIN Then
ps$ = "powershell -NoLogo -NoProfile -Command "
ps$ = ps$ + Chr$(34) + "Add-Type -AssemblyName Microsoft.VisualBasic; "
ps$ = ps$ + "[Microsoft.VisualBasic.FileIO.FileSystem]:Big GrineleteFile('"
ps$ = ps$ + file$
ps$ = ps$ + "','OnlyErrorDialogs','SendToRecycleBin')" + Chr$(34)
Shell _Hide ps$
$ElseIf MAC Then
' Build an osascript command that tells Finder to delete the POSIX file (move to Trash)
' Use single quotes around the whole -e argument in the shell, and escape inner double quotes for AppleScript.
cmd$ = "osascript -e 'tell application \" + Chr$(34) + "Finder\" + Chr$(34)
cmd$ = cmd$ + " to delete POSIX file "
cmd$ = cmd$ + Chr$(34) + file$ + Chr$(34) + "'"
Shell cmd$
$Else
'Assume Linux-like environment with gio available
Shell _Hide "gio trash " + Chr$(34) + file$ + Chr$(34)
$End If
End If
End Sub

Give the above a shot a see how it does for you. With the _HIDE removed, you might see some message that'll give a clue as to what we're missing. The original didn't have the inner slashes in the output, so that may have been the problem.

I have a feeling it's something simple, with just one bit of formatting not formatted perfectly that's gunking it up. It should be close to what it needs, but I'm working blind trying to sort out the glitch. LOL!
Reply
#9
Sorry, ahenry that didn't do it either.

SHELL "trash myfile.txt" works for me when the file is in the current working directory - QB folder here.
Reply
#10
@NakedApe One more test with the code in the post above yours, if you don't mind.  

Maybe it also needs a full path, or a ./MyFile.txt for it?  I'm spitballing and looking to see what sticks.  LOL!
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)