Home > database >  Recursive move folders in windows, .bat archive
Recursive move folders in windows, .bat archive

Time:01-28

We have a folder structure like this

dir 1
  - dir A
  - dir B
dir 2
  - dir C
dir 3
dir 4

Need to move to other directory, all dirs and subdirs, to same level, like this

d:\basedir
    dir 1
    dir A
    dir B
    dir 2
    dir C
    dir 3
...

I¡ve tried this script but, doesn't works

 for %%x in (%*) do (
    move %%x d:\basedir\
)

CodePudding user response:

The difficulty with the standard methods to solve this type of problems is that they works top-down, that is, they process first the top-most level folder. To correctly solve this problem the folders must be processed bottom-up: process first the last folders in the tree.

You may use the recursive process of a directory tree described at this answer

@echo off
cd D:\data\folder
call :treeProcess
goto :eof

:treeProcess
for /D %%d in (*) do (
   cd "%%d"
   call :treeProcess
   cd ..
   move "%%d" D:\basedir
)
exit /b

CodePudding user response:

FOR /D should do what you need.

Consider this example:

SET "SOURCE_DIR=n:\path\to\data"
FOR /D %%d IN (%SOURCE_DIR%\*.*) DO (
    @ECHO %%~fd
)

It spits out the absolute path to all the directories underneath SOURCE_DIR.

  •  Tags:  
  • Related