Windows – Robocopy: How to move the content of a directory but KEEP the directory

robocopywindows

Simple task:

Move all the content of the directory c:\Users\files\Desktop\test 2 to c:\Users\files\Desktop\test 1

C:\admin\temp>robocopy "c:\Users\files\Desktop\test 2" "c:\Users\files\Desktop\test" *.* /S /MOVE

Sounds reasonable, but now, the directory c:\Users\files\Desktop\test 2 gets removed too. Is there a switch to just move the CONTENT without the removing directory itself?

Best Answer

I have been trying to do the same from a network share.

SET MoveDirSource=\\Server\Folder
SET MoveDirDestination=Z:\Folder

Here are my findings and solution from tests on a Windows 7 machine.

Suggestion:

ROBOCOPY "%MoveDirSource%" "%MoveDirDestination%" /MOVE /E

Problem: Moves source folder.

Suggestion:

ROBOCOPY "%MoveDirSource%" "%MoveDirDestination%" /MOVE /E /XD "%MoveDirSource%"

Problem: Still moves source folder.

Suggestion:

ROBOCOPY "%MoveDirSource%" "%MoveDirDestination%" /MOV /E

Problem: Leaves empty folder structure behind in source folder.

Working solution:

MKDIR "%MoveDirDestination%"
FOR    %%i IN ("%MoveDirSource%\*") DO           MOVE /Y "%%i" "%MoveDirDestination%\%%~nxi"
FOR /D %%i IN ("%MoveDirSource%\*") DO ROBOCOPY /MOVE /E "%%i" "%MoveDirDestination%\%%~nxi"

First two lines move top level files, the third moves folders. Note the double %% is for usage in a batch file, pasting into a command line needs these changed to a single percentage mark.

~nxi in the destination is a FOR SUBSTITUTION VARIABLE ( See FOR /? ) where ~nxi represents the name and extension of the item currently being looped through.

Robocopy defaults to RETRY A MILLION times and WAIT 30 SECONDS between each try, so you may wish to add /R:1 /W:1 to the robocopy arguments.