confOS/.config/fish/functions/trash.fish

40 lines
1.2 KiB
Fish
Raw Permalink Normal View History

2024-01-01 18:07:07 +01:00
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]
2024-11-04 16:57:37 +01:00
echo -e "Usage: $current_name path/to/element"
2024-01-01 18:07:07 +01:00
return 0
end
2024-11-04 16:57:37 +01:00
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)
2024-11-04 17:06:22 +01:00
# Move element to temporary windows directory
2024-11-04 16:57:37 +01:00
cmd.exe /c "MKDIR $temp_dir 2>NUL" &> /dev/null
2024-11-04 17:06:22 +01:00
powershell.exe -Command "Copy-Item \"$path\" -Destination \"$temp_dir\""
2024-11-04 16:57:37 +01:00
2024-11-04 17:06:22 +01:00
# Switch between trashing a file or a directory
2024-11-04 16:57:37 +01:00
set switch DeleteFile
if test -d $element
set switch DeleteDirectory
end
2024-11-04 17:06:22 +01:00
# Move element to the Windows trash
2024-11-04 16:57:37 +01:00
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";
2024-01-01 18:07:07 +01:00
end