confOS/.config/fish/functions/trash.fish
2024-11-04 16:57:37 +01:00

47 lines
1.4 KiB
Fish

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/element"
return 0
end
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