Here is a Windows batch script for this.
This first script will move the files only within the directory structure similar to the one shown in the OP question.
Edit the value of the script variable "topfolder" to contain the "base folder" for the directory structure.
Edit the value of the script variable "subfolderprefix" to contain the common prefix for the "first level" folders to be searched within "topfolder". In the OP question, this "prefix" was "folder" but it can be changed to any string as necessary. Using "folder" as the prefix for example, the script will search folders like this:
top_folder
folder1
folder2
folder3
folder99
folderABC
but, other folders in the "top_folder" will be skipped, like these:
top_folder
somefolder
someotherfolder
The "second level" subfolders that will be searched for files, have a common prefix of the parent foldername followed by "_" (folder1\folder1_xxx
). Again, any other folders will be skipped:
top_folder
folder1
folder1_2
(Files here will be moved to "folder1")
folder1_99
(Files here will be moved to "folder1")
folder1_ABC
(Files here will be moved to "folder1")
folder2
folder2_2
(Files here will be moved to "folder2")
somesubfolder
(Files here will be skipped)
folder3
folder3_1
(Files here will be moved to "folder3")
folder3_2
(Files here will be moved to "folder3")
folder99
folder99_XYZ
(Files here will be moved to "folder99")
folderABC
folderABC_123
(Files here will be moved to "folderABC")
somefolder
(Folders here will be skipped)
someotherfolder
(Folders here will be skipped)
Here is the batch script:
@echo off
set "topfolder=C:\Temp\SU373589\top_folder"
set "subfolderprefix=folder"
set "startdir=%CD%"
cd /d "%topfolder%"
set ctoptest=0
set cfilecount=0
for /d %%f in ("%subfolderprefix%*") do call :worklevel1 "%%~f"
if %ctoptest% EQU 0 echo There are no matching subfolders in "%topfolder%".
if %cfilecount% EQU 0 echo There were no files moved.
if %cfilecount% EQU 1 echo %cfilecount% file was moved.
if %cfilecount% GEQ 2 echo %cfilecount% files were moved.
cd /d "%startdir%"
goto :EOF
:worklevel1
set "subfolder=%~1"
set /a ctoptest+=1
echo Processing subfolder "%subfolder%" ...
set "subsubfolderprefix=%subfolder%\%subfolder%_"
set cwork1test=0
for /d %%g in ("%subsubfolderprefix%*") do call :worklevel2 "%%~g"
if %cwork1test% EQU 0 echo There are no matching subfolders in "%subfolder%"
echo.
set "subfolder="
set "subsubfolderprefix="
set cwork1test=
goto :EOF
:worklevel2
set "subsubfolder=%~1"
set /a cwork1test+=1
echo Processing subsubfolder "%subsubfolder%" ...
set cwork2test=0
for %%h in ("%subsubfolder%\*") do call :workmove "%%~h"
if %cwork2test% EQU 0 echo There are no files to move in "%subsubfolder%"
set "subsubfolder="
set cwork2test=
goto :EOF
:workmove
set "targetfile=%~1"
set /a cwork2test+=1
set /a cfilecount+=1
echo Moving file "%targetfile%" to "%subfolder%" (%cwork2test%)...
move "%targetfile%" "%subfolder%" >nul 2>&1
set "targetfile="
goto :EOF
This second batch script will work in a similar way, looking in ALL "first level" subfolders, and moving all files from all "second level" subfolders to the "first level" subfolders. It will not look to match any "prefix" to any of the foldernames.
Edit the value of the script variable "topfolder" to contain the base folder for the directory structure.
@echo off
set "topfolder=C:\Temp\SE373589\top_folder"
set "startdir=%CD%"
cd /d "%topfolder%"
set ctoptest=0
set cfilecount=0
for /d %%f in ("*") do call :worklevel1 "%%~f"
if %ctoptest% EQU 0 echo There are no matching subfolders in "%topfolder%".
if %cfilecount% EQU 0 echo There were no files moved.
if %cfilecount% EQU 1 echo %cfilecount% file was moved.
if %cfilecount% GEQ 2 echo %cfilecount% files were moved.
cd /d "%startdir%"
goto :EOF
:worklevel1
set "subfolder=%~1"
set /a ctoptest+=1
echo Processing subfolder "%subfolder%" ...
set cwork1test=0
for /d %%g in ("%subfolder%\*") do call :worklevel2 "%%~g"
if %cwork1test% EQU 0 echo There are no matching subfolders in "%subfolder%"
echo.
set "subfolder="
set cwork1test=
goto :EOF
:worklevel2
set "subsubfolder=%~1"
set /a cwork1test+=1
echo Processing subsubfolder "%subsubfolder%" ...
set cwork2test=0
for %%h in ("%subsubfolder%\*") do call :workmove "%%~h"
if %cwork2test% EQU 0 echo There are no files to move in "%subsubfolder%"
set "subsubfolder="
set cwork2test=
goto :EOF
:workmove
set "targetfile=%~1"
set /a cwork2test+=1
set /a cfilecount+=1
echo Moving file "%targetfile%" to "%subfolder%" (%cwork2test%)...
move "%targetfile%" "%subfolder%" >nul 2>&1
set "targetfile="
goto :EOF
A simpler version of the batch script without all the checking for empty folders, etc...:
@echo off
set "topfolder=C:\Temp\SE373589\top_folder"
set "startdir=%CD%"
cd /d "%topfolder%"
for /d %%f in (*) do (
echo Processing folder: "%%~f"
for /d %%g in ("%%f\*") do (
echo Processing sub-folder: "%%~g"
for %%h in ("%%~g\*") do (
echo Moving file: "%%~h" to "%%~f"
move "%%~h" "%%~f" >nul 2>&1
)
)
echo.
)
cd /d "%startdir%"
And finally, a "bare minimum" script without messaging and assuming you are already in the proper "base folder":
@echo off
for /d %%f in (*) do (
for /d %%g in ("%%f\*") do (
for %%h in ("%%~g\*") do move "%%~h" "%%~f" >nul 2>&1
)
)
And, just in case you were expecting it, a "one liner" you can run from the command prompt:
@for /d %f in (*) do @(for /d %g in ("%f\*") do @(for %h in ("%~g\*") do @move "%~h" "%~f" >nul 2>&1))