0

My goal is to move some files into recycle bin using a cmd file. I've done that with Cmdutils/recycle and it works great when files are stored on my computer (windows 10). Unfortunately those files (and also the batch file) are on a network folder (on win server 2016). I've tried mapping that folder as a network drive on my computer and running that cmd, but those files are completely deleted. Is there a way for moving them into recycle bin also if they are on a network drive?

I've read here how to map an unused folder into that network drive and once done that, cmdutils/recycle correctly moves file into my local recycle bin. The problem is that different users will need to run that cmd, so if there's a way via cmd (or powershell) without doing that trick I'd be happier.

Any idea?

Thanks! Luigi.

4
  • Did you see this tool ? Commented Oct 29, 2020 at 18:19
  • Thanks. I actually need this deletion to be inside a cmd/powershell, cause I have some logic there for detecting which file to delete...
    – solquest
    Commented Oct 29, 2020 at 18:41
  • Thanks. You mean moving them on another folder instead on in recycle bin? I'd rather prefer deleting these files instead of moving to another folder, cause if users find out that they need them, they can easily be restored -while if I move them to another folder, it's not so easy to restore them in the original folder (will be like thousands of files and hundreds of subfolders)
    – solquest
    Commented Oct 29, 2020 at 20:34
  • Hmmm thanks but that sounds too complex for me...
    – solquest
    Commented Oct 31, 2020 at 13:09

1 Answer 1

1

OK.. ok.. I have thought this through thoroughly and looked up the Win32 API and implementation that make the recycle bin happen.

SHFileOperation (the actual function that moves things to the recycle bin) does not support network paths (mapped or not) when the FOF_ALLOWUNDO flag is set (recycle). The trick you found is a workaround that Microsoft might very well fix any day now. It leaves the API broken as you can see if you read all of the posts on the workaround page you provided.

Lets start with logic. Look at the root of any local drive you have. You will find a folder called $Recycle.bin. When you delete a file, the windows explorer uses this folder to move your file there instead of a real delete. It does not move files from D: to C:\Recycle.Bin.. etc.. the mechanism is made to work against a single volume. Even though there is some complexity in renaming the original file and keeping track of the date/time of deletion, it is as simple as that.

When you are looking at a network share, you will NOT find a recycle bin folder and windows (rightfully so) will not make one for you. Can you imagine if every admin had to keep track of all of the crap that each user deleted on their share? Microsoft could have very well made this work.. but they chose not to.

You might get this to work but there are two basic problems here.

Problem 1 is that it Microsoft never intended to have recycle bin access from cmd.exe. Even the utilities that allow this call into the shell API (aka explorer).

Problem 2 is that this mechanism will NEVER work correctly from a network share even if you can demonstrate cases where it does "work". Until you see a $Recycle.bin folder on your mapped network drive.. something outside of the tested code path is happening.

I hope that someone else can give you a workaround but unless you "roll your own", I think it will be shoddy at best.

Good Luck! I mean it!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .