How can I move files from 2-level folders to 1-level folder

6,079

Solution 1

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.

Solution 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

Share:
6,079

Related videos on Youtube

Nicholas Lie
Author by

Nicholas Lie

Updated on September 18, 2022

Comments

  • Nicholas Lie
    Nicholas Lie over 1 year

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

    • surfasb
      surfasb over 12 years
      What Operating System?????
    • Nicholas Lie
      Nicholas Lie over 12 years
      windows 7 32-bit
  • Nicholas Lie
    Nicholas Lie over 12 years
    i copy pasted your code and it gives the following error: Missing expression after unary operator '!'. At line:5 char:39
  • surfasb
    surfasb over 12 years
    @NicholasLie: Edit: I didn't format the code as code so some of the characters were unescaped. There should be an underscore after the $.
  • Nicholas Lie
    Nicholas Lie over 12 years
    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) >,<