This commit is contained in:
Mylloon 2024-11-04 16:57:37 +01:00
parent 2e78c2dabf
commit c933f22b37
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -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." function trash --description "Move file or directory to the windows recycle bin."
# Get function name # Get function name
set current_name $(status current-function) set current_name $(status current-function)
# Check if no arguments were provided # Check if no arguments were provided
if test -z $argv[1] if test -z $argv[1]
echo -e "Usage: $current_name path/to/file" echo -e "Usage: $current_name path/to/element"
return 0 return 0
end end
# Move file to trash set element $argv[1]
set file (wslpath -w $argv[1])
powershell.exe -Command "& {" \ # Check if element exists
" Add-Type -AssemblyName 'Microsoft.VisualBasic';" \ if not test -e $element
" Get-ChildItem -Path '$file' | ForEach-Object {" \ echo "File or directory doesn't exists" 1>&2
" if (\$_ -is [System.IO.DirectoryInfo]) {" \ return 1
" [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteDirectory(\$_.FullName, 'OnlyErrorDialogs', 'SendToRecycleBin')" \ end
" } else {" \
" [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile(\$_.FullName, 'OnlyErrorDialogs', 'SendToRecycleBin')" \ 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 end