3

I have a bunch of files stored in a file directory as such

root_folder
  -- folder1
     --folder1_2
       --bunch of files here
  -- folder2
     --folder2_2
       --bunch of files here
  -- folder3
     --folder3_2
       --bunch of files here

As you can see, my files in a 2-level folder from the root folder. How can I easily move my bunch of files as such the directory becomes like this:

root_folder
  -- folder1
     --bunch of files here
  -- folder2
     --bunch of files here
  -- folder3
     --bunch of files here

Is there any freeware program I can use? Or maybe can I use command prompt to accomplish this? Thanks a lot for the help :)

2
  • What Operating System?????
    – surfasb
    Jan 2, 2012 at 1:16
  • windows 7 32-bit
    – nic
    Jan 2, 2012 at 13:35

4 Answers 4

3

I also provided a Powershell Example:

$source = "c:\sourceFolder"   
$dirs = dir $source | Where-Object {
$_.PSIsContainer }  

foreach ($folder in $dirs){  
    dir $folder -recurse | Where-Object { ! $_.PSIsContainer } | Move -Destination $folder -WhatIf  
     dir $folder -recurse | Where-Object { $_.PSIsContainer } | rd -recurse -Whatif  
}

You can copy and paste that into Powershell ISE. The bold whatif switches basically outputs a dry run.

You can check the output to make sure it is moving the files to where you exactly want them. Remove the -whatif switches to make script live.

2
  • i copy pasted your code and it gives the following error: Missing expression after unary operator '!'. At line:5 char:39
    – nic
    Jan 2, 2012 at 13:41
  • @NicholasLie: Edit: I didn't format the code as code so some of the characters were unescaped. There should be an underscore after the $.
    – surfasb
    Jan 2, 2012 at 19:35
2

Powershell solution. Use this in root_folder (make sure that there aren't any loose files directly under root_folder):

gci -R | ?{!$_.PSIsContainer} | %{mv $_.fullname $_.directory.parent}

The above puts any file it finds into the same directory that the file's parent directory is in.

Then, to delete any now-empty folders:

gci -R | ?{(gci $_) -eq $NULL} | rm

0

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))
-1

In Explorer (assuming Windows here) open folder1_2, select all files, right click & choose Move to Folder ..., select Folder1 in the dialog; then delete folder1_2 if that was your intention.

Repeat the above for all subfolders you want to clear out.

No need for any external program.

1
  • 1
    It would be ok if there's only 10 or so folders..If I have a 100+ folders it would cumbersome for me to do so (which in my case 200+ folders) >,<
    – nic
    Jan 3, 2012 at 4:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .