When you move a directory, its sub-directories are no longer available at the source location, so you need to first handle sub-directories before you move their parents:
rem /* Iterate through all directories in the tree, and change the default
rem top-to-bottom order to bottom-to-top order per each sub-branch: */
for /F "delims= eol=|" %%I in ('
dir /S /B /A:D-H-S "D:\source\*" ^| sort /R
') do (
move "%%~I" "D:\basedir\"
)
If you just want the sub- and sub-sub-directories to be moved but not deeper ones, use this code instead:
rem // Iterate through immediate child directories:
for /D %%J in ("D:\source\*") do (
rem // Iterate through grandchild directories:
for /D %%I in ("%%~J\*") do (
rem // Move current grandchild directory:
move "%%~I" "D:\basedir\"
)
rem // Move current child directory:
move "%%~J" "D:\basedir\"
)