Sammy Miri
Sammy Miri

Reputation: 123

How to move files from subdirectories to their parent directory?

I have files in folders organized like this

c:\111\1\file.jpg
c:\222\2\file.jpg
c:\333\3\file.jpg

I am trying to move the files to their respective parent folder. So they will be placed like this.

c:\111\file.jpg
c:\222\file.jpg
c:\333\file.jpg

I tried

for %F in (c:\*\*\*.*) do move /Y %F c:\*\*.*

But that didn't work.

Upvotes: 2

Views: 1760

Answers (5)

bjoster
bjoster

Reputation: 1900

You can quickly iterate through the subfolders, one path at a time:

@echo off
for /f %%i in ('dir /a:d /b') do (
    cd "%%i"
    for /f %%j in ('dir /a:d /b') do (
        move "%%j" ..\
    )
    cd ..
)

Upvotes: 1

Ravi Koradia
Ravi Koradia

Reputation: 939

It is little bit late, But it will help those who need to move files in their respective parent folder from two level depth folder structure:

for /f %%i in ('dir /a:d /b') do (
    cd "%%i"
    for /f %%j in ('dir /a:d /b') do (
        move "%%j"\* .
        cd "%%j"
        for /f %%k in ('dir /a:d /b') do (
            move "%%k"\* ..\
        )
        cd ..
    )
    cd ..
)

Thanks, Ravi

Upvotes: 0

kkk
kkk

Reputation: 848

Just use:

FOR /F %i IN ('dir /s /b *.jpg') DO move %i %~pi..\

or this ... in case you'r inside a BATCH file:

FOR /F %%i IN ('dir /s /b *.jpg') DO move %%i %%~pi..\

Upvotes: 0

Dennis van Gils
Dennis van Gils

Reputation: 3452

Try this:

@echo off
FOR /D /R %%D in ("*") DO (
FOR %%f IN ("%%D\*.*") DO move "%%D\%%~nxf" "%%D\.."
)
pause

WARNING, this will move all documents in the tree below the folder this batch-file is in 1 directory up

Upvotes: 0

cwahls
cwahls

Reputation: 753

Just use ...

Put this batch file in the directory of the files to be acted on.

for %%a in (*) do (move "%%a" ..)

Make sure to use quotations on first parameter of move. The batch file will move, too

Upvotes: 0

Related Questions