10

It is a simple task, in some instances but I have thousands of files inside hundreds of folders that I want to move up.

The directory structure goes like this:

C:\Photos\subject\randomnumbers\images\file.ext

And I want to end with:

C:\Photos\subject\randomnumbers\file.ext

I've tried running for /r %i in ("*\images\*.*") do move *\*.* but it doesn't work as expected.

I can't think of another way to do this other than manually doing it. While doing it manually will work, I'd benefit greatly with an automated operation I can kick off as needed.

It is possible, and can anyone help me with this?

2
  • 1
    If you have thousands of image files and you want to manage them, maybe you should invest some time in learning exiftool... You'll achieve what you want and likely more. Just play safe and test on a copy before applying to a large number of files.
    – techraf
    Commented Aug 19, 2016 at 1:56
  • @techraf Wow, exiftool is REALLY powerful, judging from a quick glimpse at its documentation! Will read more, thanks!
    – HASJ
    Commented Aug 19, 2016 at 2:45

5 Answers 5

4

This should work for you:

for /r %i in ("images\*.*") do move %i %~pi..
  • %~pi extracts the path from %i (ending with \)
  • .. placed after the above path points to the parent directory

Of course it doesn't handle the situation where you have files with conflicting names in subdirectories.

1
  • Wonderful! Working exactly as expected! Can't believe I was forgetting the %i in my original command. I also would never think of using ..! Thank you and thanks to everyone that replied! Hope these answers help other people with similar OR different questions!
    – HASJ
    Commented Aug 19, 2016 at 2:41
4

This is not very sophisticated, and I am sure people will come up with better solutions, but here is a quick one:

  1. Do a simple (Explorer) search for *, starting in the folder where you want to move the stuff to. It might take half a minute or so to complete.
  2. Sort the result by path
  3. Select all the files (which is what you want to move) that are not already in the target folder (those would be your existing 'thousands of directories'. They will be all together after the sort, so 'click' and 'shift+click' does it.
  4. Drag them into the folder structure tree on the left and drop them into the target folder.
  5. Get a coffee or do some other work while it moves. Windows can typically move 40 - 7000 files per second, depending on your hardware.
  6. Select all the - now empty- directories and delete them.

Note that you can also do step 1 with filters if you want only certain file types, for example *.jpg,*.png.

2

Batch move a folder's content up one level

Here's a batch solution where the MoveFromDir is the directory you start with for the contents within it you will move, and the MoveToDir is the directory explicit path those contents will be moved up one level from the MoveFromDir, so you'd have to set these variables accordingly.

Move all folders and files in MoveFromDir and beneath to MoveToDir

Please note that running only the second FOR loop of:

  • FOR /F "TOKENS=*" %%A IN ('DIR /S /B "%MoveFromDir%\*.jpg"')

You can specify just the file extensions you want to move to the MoveToDir (e.g. jpg)

@ECHO ON

SET MoveToDir=C:\Photos\subject\randomnumbers
SET MoveFromDir=C:\Photos\subject\randomnumbers\images

:: Move the folders from the move directory to the move to directory
FOR /D %%A IN ("%MoveFromDir%\*") DO MOVE /Y "%%~A" "%MoveToDir%"

:: Move any remaining files (or folders) from the move directory to the move to directory
FOR /F "TOKENS=*" %%A IN ('DIR /S /B "%MoveFromDir%\*.*"') DO MOVE /Y "%%~A" "%MoveToDir%\"
GOTO EOF

Further Resources

0

I found another way to do this...it takes more time and HDD space but it allows you to do this without knowing the lowest level directory name. So instead of having to know the lowest level folder is named "images", it can literally be anything.

For example, I had a bunch of folders, say they are named 1, 2, 3, 4, 5, etc. and inside all of those folders were more folders which contained files. I wanted to move all the files in any given root folder (1, 2, 3, 4, 5, etc.) up to that top level folder (1, 2, 3, 4, 5, etc.). I found that using WinRAR I could zip all those top level folders up (1, 2, 3, 4, 5 etc.) using "store" for compression to minimize processing time. Then I simply unzipped them using the right click button menu again "extract each archive to separate folder." Before doing the unzip though I had to "save settings" in WinRAR with the setting Extract-->Advanced Menu-->File Paths: "Do not extract paths" and every zip file was unzipped to it's own folder with all files inside (no matter how may folders/subfolders inside) in the top level.

I successfully did this with 2350 folders at once and it work great. Now I use it every week or so.

0

PowerShell:

$source = 'C:\Photos\subject\randomnumbers\images'

Get-ChildItem $source -file | move-item -Destination {$_.Directory.Parent.Fullname}

Use wildcards to process all randonmnumbers:

$source = 'C:\Photos\subject\*\images'

Get-ChildItem $source -file | move-item -Destination {$_.Directory.Parent.Fullname}

You must log in to answer this question.

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