trash
This commit is contained in:
parent
2e78c2dabf
commit
c933f22b37
1 changed files with 37 additions and 16 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue