Command Prompt to delete to recycle bin


  1. Posts : 132
    10 Pro
       #1

    Command Prompt to delete to recycle bin


    I've recently made some bat scripts to do some stuff with zip files, extract and sort/rename etc blah blah blah not important. As part of the cleanup I have it delete the zip file once its extracted however something went wrong the other day and it turns out "del file.zip" just deletes it completely. Is there a way to send to recycle bin via command prompt so i can retrieve the file if something goes wrong??
      My Computer


  2. Posts : 31,660
    10 Home x64 (22H2) (10 Pro on 2nd pc)
       #2

    Not in a command prompt, though it should be possible in a PowerShell script like this one:
    Technet forum | How can I delete "to recycle bin" in powershell instead of remove item ?

    There are third-party utilities that you could use, CmdUtils includes a recycle.exe that will do the job. Use recycle -f to skip the confirmation dialog. It's been around for years, but still works in W10.
    MaDdoG product: CmdUtils

    If you want to stick to the standard Microsoft commands, as an alternative to deleting the files you could move them to another folder you've already created for that purpose, for example:

    MOVE /Y *.* C:\My_Bin
      My Computers


  3. Posts : 132
    10 Pro
    Thread Starter
       #3

    Cheers, I think I'll give recycle a go!
      My Computer


  4. Posts : 16,949
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #4

    mkr,

    I have just posted in the TechNet thread that @Bree gave you a link to.

    I have posted suitable cmd lines to add to your batch files in order to call a common PS1 file to handle the deletion stage. The deleted files go to the Recycle bin as you wanted and they can be 'restored' [undeleted] from there in the normal manner if required.
    - Note that I have only written this for file deletions rather than folder deletions.
    - You will see that my PS1 file lives in a folder called C:\Tools, as do all my own batch, vbs & PS1 scripts. I created that folder then modified its permissions so that only Administrators can change its contents [as a guard against mistakes & malware].

    If you are going to use my suggested code for file deletions from your batch files and have any queries then you can just post back here instead of in TechNet.

    Denis
      My Computer


  5. Posts : 16,949
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #5

    mkr,

    I tidied up the code a bit.

    Where, in your batch file, you currently have a delete command you should replace it with a call to the PS1 file followed by a list of the condemned files, using their full paths. [I have left two sample file paths in there but you need to substitute your own.]

    Code:
    powershell.exe -ExecutionPolicy ByPass -File "C:\Tools\DeleteToRecycleBin.ps1"  "D:\Documents\IS\Scripting\PowerShell\Notes\delete\DeleteToRecycleBin - TechNet - Copy.pdf" "D:\Documents\IS\Scripting\PowerShell\Notes\delete\DeleteToRecycleBin - TechNet - Copy (2).pdf"

    The PS1 file is C:\Tools\DeleteToRecycleBin.ps1. You can just create it as a text file, enter the code given, save it then rename it with the extension ps1.

    Code:
     ForEach ($PassedArg in $args) {
        $paths = Get-ChildItem -Path "$PassedArg"
        $shell = New-Object-ComObject 'Shell.Application'
            ForEach ($path in $paths)
            {
            $shell.NameSpace(0).ParseName($path.FullName).InvokeVerb('delete')
            }
    }
    
    ##### Derived from code posted by TechNet forum user Albert Ling  ##### 
    

    As I mentioned before, I created the folder C:\Tools for all my own scripts.

    Denis
      My Computer


  6. Posts : 132
    10 Pro
    Thread Starter
       #6

    Thanks, I'd much prefer that than installing extra stuff. Presumably I can wildcard files in that command?

    powershell.exe -ExecutionPolicy ByPass -File "C:\Tools\DeleteToRecycleBin.ps1" "D:\Documents\IS\Scripting\PowerShell\Notes\delete\*.pdf


    Edit: Just tried it and got this message

    C:\mkr\tools>powershell.exe -ExecutionPolicy ByPass -File "C:\mkr\tools\recycle.ps1" "C:\Users\Michael\Videos\*.rar"
    New-Object-ComObject : The term 'New-Object-ComObject' is not recognized as the name of a cmdlet, function, script
    file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
    and try again.
    At C:\mkr\tools\recycle.ps1:3 char:14
    + $shell = New-Object-ComObject 'Shell.Application'
    + ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (New-Object-ComObject:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


    You cannot call a method on a null-valued expression.
    At C:\mkr\tools\recycle.ps1:6 char:9
    + $shell.NameSpace(0).ParseName($path.FullName).InvokeVerb('del ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull


      My Computer


  7. Posts : 16,949
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #7

    In addition to the limitation you have just discovered, it cannot be used to delete folders.

    The tool can only be used to delete files for which full paths-names are specified.

    Denis
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 00:33.
Find Us




Windows 10 Forums