From c933f22b37060a16539498c9be9c1835d9bd6993 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 4 Nov 2024 16:57:37 +0100 Subject: [PATCH] trash --- .config/fish/functions/trash.fish | 53 +++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/.config/fish/functions/trash.fish b/.config/fish/functions/trash.fish index f2c0058..39b60f1 100644 --- a/.config/fish/functions/trash.fish +++ b/.config/fish/functions/trash.fish @@ -1,26 +1,47 @@ -# Do not use this script, this is slow and it's not moving files to the -# windows recycle bin since it's impossible from WSL - function trash --description "Move file or directory to the windows recycle bin." # Get function name set current_name $(status current-function) # Check if no arguments were provided if test -z $argv[1] - echo -e "Usage: $current_name path/to/file" + echo -e "Usage: $current_name path/to/element" return 0 end - # Move file to trash - set file (wslpath -w $argv[1]) - powershell.exe -Command "& {" \ - " Add-Type -AssemblyName 'Microsoft.VisualBasic';" \ - " Get-ChildItem -Path '$file' | ForEach-Object {" \ - " if (\$_ -is [System.IO.DirectoryInfo]) {" \ - " [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteDirectory(\$_.FullName, 'OnlyErrorDialogs', 'SendToRecycleBin')" \ - " } else {" \ - " [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile(\$_.FullName, 'OnlyErrorDialogs', 'SendToRecycleBin')" \ - " }" \ - " }" \ - "}" + set element $argv[1] + + # Check if element exists + if not test -e $element + echo "File or directory doesn't exists" 1>&2 + return 1 + end + + set temp_dir (powershell.exe -Command "Write-Host \$env:TEMP") + set path (wslpath -w $element) + + # Move element to TEMP windows directory + cmd.exe /c "MKDIR $temp_dir 2>NUL" &> /dev/null + powershell.exe -Command "Copy-Item $path -Destination $temp_dir" + + set switch DeleteFile + if test -d $element + set switch DeleteDirectory + end + + + # Move path to Windows trash + powershell.exe -Command "" \ + "Add-Type -AssemblyName Microsoft.VisualBasic;" \ + "[Microsoft.VisualBasic.FileIO.FileSystem]::$switch('$temp_dir\\$element', 'OnlyErrorDialogs', 'SendToRecycleBin')" + + # Delete the WSL path + rm -r "$element"; + + # Empty trash (move to another function!) + # powershell.exe -Command "" \ + # "\$bin = (New-Object -ComObject Shell.Application).NameSpace(10);" \ + # "\$bin.items() | ForEach {" \ + # " Write-Host 'Deleting:' \$_.Name;" \ + # " Remove-Item \$_.Path -Recurse -Force;" \ + # "}" end