user2611836
user2611836

Reputation: 416

Batch file to move all subfolder files up then delete empty subfolders?

I have a folder that contains many subfolders, each with different files in them:

C:/folder/subfolder1/file1.jpg,
C:/folder/subfolder2/file2.jpg,
C:/folder/subfolder3/file3.jpg, 
etc.

How can I create a batch file that moves the files in the subfolders to the folder, then deletes the empty subfolders.

ie. After the bat file is run, the folder structure should be as follows:

C:/folder/file1.jpg
C:/folder/file2.jpg
C:/folder/file3.jpg 
etc.

Upvotes: 4

Views: 13565

Answers (3)

Travis Bryant
Travis Bryant

Reputation: 1

I used this as a batch file to move all files from subfolders in the current folder then delete the empty folders.

rem Copy all files from subfolders in the current folder to the current folder.
for /R %cd% %%G IN (*.*) DO move /Y "%%G"
rem Move all directories into a temp folder in the current folder.
for /D %%I in (*) do move /y %%I %cd%\temp
rem Remove the temp folder including empty folders.
rmdir /s/q %cd%\temp

Upvotes: 0

rostok
rostok

Reputation: 2137

try this

@echo off
for /f "tokens=*" %%f in ('dir /a:-D /s /b') do move "%%f" .
for /f "tokens=*" %%f in ('dir /a:D /s /b') do rd "%%f"

Upvotes: 9

unclemeat
unclemeat

Reputation: 5207

This will go through every folder in %root% (C:\folder) and copy the contents to %root% -

@echo off
set root=c:\folder
for /f %%a in ('dir /b /ad %root%') do (
    for /f %%b in ('dir /b %root%\%%a') do move "%root%\%%a\%%b" "%root%\%%b"
    rmdir "%root%\%%a"
)

Upvotes: 1

Related Questions