Remove subfolders from a folder (loop)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rodman
Posts: 6
Joined: 23 Mar 2012 04:37
Location: Paris

Remove subfolders from a folder (loop)

#1 Post by Rodman » 23 Mar 2012 04:46

Hello,

I have a collection of folders which contain subfolders and files.

I want for each folder that contains subfolders to remove them but keep the files in this folder.

For exemple, from this :

Root folder
_folder 1
__subfolder 1
___test.txt
__subfolder 2
___test.jpg
_folder 2
__subfolder 1
___subsubfolder 1
____test.mov

I want this :

Root folder
_folder 1
__test.txt
_test.jpg
_folder 2
__test.mov


Thank you.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Remove subfolders from a folder (loop)

#2 Post by abc0502 » 23 Mar 2012 06:15

Hi,
u can try this but it will move all files to the root folder not to the first subfolder

Code: Select all

@echo off 
cls
set root=%userprofile%\desktop\rootfolder
For /R %root% %%a IN (*.*) DO move "%%a" "%root%"

But There is a problem if there is a file with the same name i couldn't fix it
and you can use this:

Code: Select all

@echo off
cls
set root=%userprofile%\desktop\rootfolder
MD %root%\jpg
set jpg=%root%\jpg
MD %root%\txt
set txt=%root%\txt

For /R %root% %%a IN (*.jpg) DO move "%%a" "%jpg%"
For /R %root% %%b IN (*.txt) DO move "%%a" "%txt%"

This will make folders named "jpg" and "txt" and copy text files to txt folder and jpg files to jpg folder
you can add folders usinf MD and use the same For command to move files to it

Thats all what i can do i hope that help u :)

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Remove subfolders from a folder (loop)

#3 Post by abc0502 » 23 Mar 2012 06:45

I found this topic see this it could be what u looking for:
http://www.dostips.com/forum/viewtopic.php?f=3&t=2193

be carfule it will overwrite files automaticaly if files name the same

Rodman
Posts: 6
Joined: 23 Mar 2012 04:37
Location: Paris

Re: Remove subfolders from a folder (loop)

#4 Post by Rodman » 23 Mar 2012 07:26

Thanks Sir !!! :D

Your link seems to be exactly what I need.
I'm gonna try it and reply you after to tell you if it works or if I have another problem.

Rodman
Posts: 6
Joined: 23 Mar 2012 04:37
Location: Paris

Re: Remove subfolders from a folder (loop)

#5 Post by Rodman » 23 Mar 2012 07:53

It's perfectly working ! I'm so glad ! You made my day.

Post Reply