Want to move all subfolders to one level up

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ivar75
Posts: 5
Joined: 11 Apr 2012 05:31

Want to move all subfolders to one level up

#1 Post by ivar75 » 11 Apr 2012 05:44

Hi,

I need a small batch file to do some work

MY root directory is H:/wavz&Rex/XYZ
I am having 2000 folders like this
FOLDER 1
A
FOLDER 2
B
FOLDER 3
C
FOLDER 4
D

I wish to move all A,B,C,D etc., folders to root directory XYZ. Kindly help, I am very new to ms-dos even I dont know how to make a batch file. Kindly help me and save my time.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Want to move all subfolders to one level up

#2 Post by Fawers » 11 Apr 2012 06:55

Untested:

Code: Select all

@echo off
setlocal enabledelayedexpansion
cd /d H:/wavz&Rex/XYZ
for /f "delims=" %%A in ('dir /b /ad') do (
  set fldrname=%%~nA
  if not exist "..\!fldrname!" md "..\!fldrname!"
  xcopy /s /e /i /h /-y "%%A\*.*" "..\!fldrname!"&&^
    set /p "q=!fldrname! was copied successfully. Remove "%%A"? [Y/N] "
    if /i [!q!] == [Y] (
      rd /s /q "%%A"&&^
      echo "%%~nA" removed successfully.||^
        echo "%%~nA" was not removed successfully.
      )
  set q=
  )
endlocal



Edit: added the option to accept user input; user chooses whether original folder will be removed or not.

Edit²: added the /e switch to xcopy command.
Last edited by Fawers on 11 Apr 2012 10:07, edited 2 times in total.

ivar75
Posts: 5
Joined: 11 Apr 2012 05:31

Re: Want to move all subfolders to one level up

#3 Post by ivar75 » 11 Apr 2012 09:33

Hi Fawers,

Thanks for spending ur precious time on this. I checked this batch file. Its working but I found a small problem .

MY root directory is H:/wavz&Rex/XYZ
I am having 2000 folders like this
FOLDER 1
A
FOLDER 2
B
FOLDER 3
C
FOLDER 4
D

When I tried this batch file All my Floder 1, Folder 2, Folder 3,Folder 4 or moving to XYZ without their subdirectories . But I need All Subdirectories to be moved to XYZ without Folder 1,2,3,4. Your help will be appreciated.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Want to move all subfolders to one level up

#4 Post by Fawers » 11 Apr 2012 09:54

ivar75 wrote:When I tried this batch file All my Floder 1, Folder 2, Folder 3,Folder 4 or moving to XYZ without their subdirectories . But I need All Subdirectories to be moved to XYZ without Folder 1,2,3,4. Your help will be appreciated.


Are the subdirectories empty? The xcopy command is set to move subdirectories as well, but only if they are not empty.

Anyways, add the /e switch to the xcopy command.

Code: Select all

xcopy /s /i /h /-y "%%A" "..\!fldrname!"&&^

should look like

Code: Select all

xcopy /s /e /i /h /-y "%%A" "..\!fldrname!"&&^


Tell me the results when you're done.

Edit: I added it manually to the code on my first post. You might want to test the whole code again.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Want to move all subfolders to one level up

#5 Post by foxidrive » 11 Apr 2012 13:03

This should work if there are no duplicate folder names.

Run it in the folder that contains "folder1" folder2" etc and it will move folders "folder1\a" and "folder2\b" along with all subdirectories to "H:/wavz&Rex/XYZ"

Please test it on a set of sample files and folders first. It worked here in a simple test but you need to sure that it works properly.


Code: Select all

@echo off
for /f "delims=" %%a in ('dir /ad /b') do (
for /f "delims=" %%b in ('dir "%%a" /ad /b') do (
move "%%a\%%b" "H:/wavz&Rex/XYZ"
)
)
pause

ivar75
Posts: 5
Joined: 11 Apr 2012 05:31

Re: Want to move all subfolders to one level up

#6 Post by ivar75 » 12 Apr 2012 01:25

@fewers

Yes folder A,B, are empty. When I put some files in A,B folders . they are working but I need only subdirectories to come out, Tolal Folder 1,2,(inclu A,B) are going up. My intention is just to seperate all A,B folders out.

Thanks for your time

ivar75
Posts: 5
Joined: 11 Apr 2012 05:31

Re: Want to move all subfolders to one level up

#7 Post by ivar75 » 12 Apr 2012 01:32

@foxdirve

Its working !! Exactly what I am looking for !! Thank you very much !! Love the forum..........today I used it and It saved me 48 hours of work :)....Thank you once again

Post Reply