Using Robocopy to move files to Null

1

I'm trying to move files using robocopy to NULL in a powershell script, in effect deleting them. Normally I'd use get-childitem but I'm dealing with long file name paths and it fails on those, but robocopy won't. For some reason powershell is interpreting NULL as a folder it needs to create instead of copying it to the NULL device. Googling led me to think this is possible, but everywhere I saw people using NULL in the same way I'm trying to but were using it to find files matching a string, instead of moving the files. This is the part of the script that does the moving:

robocopy $directory NULL *.* /xf *.pst /e /mov

Anyone have any luck using robocopy to move files to NULL? Or is there a better way to do this?

user66131

Posted 2017-12-18T18:50:59.697

Reputation: 11

Where did you see people using NULL like that? I think the null device in PowerShell is $NULL. – Dour High Arch – 2017-12-19T14:45:42.400

It is NUL with one L. And no you cannot use it like /DEV/NULL in the Unix world. – Squashman – 2018-12-13T00:16:47.097

Answers

3

Check out /purge. From the robocopy help file:

/PURGE :: delete dest files/dirs that no longer exist in source.

If you create a dummy folder that is empty, say C:\empty, you can use:

robocopy C:\empty $directory *.* /xf *.pst /e /purge

root

Posted 2017-12-18T18:50:59.697

Reputation: 2 992

to save a few keystrokes, /MIR can be used instead of /e /purge – JamHandy – 2019-04-15T13:01:04.617