Solved

I need to batch move sub-folders to the top level

Posted on 2014-03-11
14
2,269 Views
Last Modified: 2014-03-12
I have a Drive with 100s of folders at the top/root level that have sub-folders within them, and I need the first level of those sub-folders to be moved to the top-level.

I don't want to spend hours "dragging and dropping" sub-folders folders to the top level, so was after a batch script that would do this for me:

i.e.:

C:\myFiles\ABC\12345\6789\<lots of files in here>

would become

C:\myFiles\12345\6789\<lots of files in here>

So a script that would take all 2ndlevel folders in C:\myfiles and promote them to the top level.
0
Comment
Question by:DebbieFost
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
14 Comments
 
LVL 84

Expert Comment

by:oBdA
ID: 39921191
You don't even need a script to do that; an open, elevated command prompt will do the trick:
for /d %a in ("C:\MyFiles\*.*") do @(echo Processing '%~nxa' ... & move "%a" C:\)

Open in new window

If you get an "Access denied", you either really don't have access, or a file or folder (for example in Explorer) is open. In this case, the folder can't be moved.
As batch, you basically just need to double up on the percent signs:
@echo off
for /d %a in ("C:\MyFiles\*.*") do (
	echo Processing '%~nxa' ...
	move "%a" C:\
)

Open in new window

0
 

Author Comment

by:DebbieFost
ID: 39921364
The batch text returns the following error:

 "\MyFiles\*.*"> do < was unexpected at this time
0
 
LVL 84

Expert Comment

by:oBdA
ID: 39921393
My bad, sorry; if I say "double up on the percent signs", I should then follow my advice.
@echo off
for /d %%a in ("C:\MyFiles\*.*") do (
	echo Processing '%%~nxa' ...
	move "%%a" C:\
)

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:DebbieFost
ID: 39921460
That moved all of my folders to the root of my C Drive

It did not take the first level of subfolders out from underneath the top level folders

i.e.

It just moved C:\myFiles\ABC\12345\6789\<lots of files in here> to C:\ABC\12345\6789\<lots of files in here>
0
 
LVL 84

Expert Comment

by:oBdA
ID: 39921498
Sorry, misunderstood "promote them to the top level".
So you currently have a bunch of folders like C:\ABC\12345 that basically need to go in C:\MyFiles\12345?
Are these folders really on C:, or is that just for demonstration? In other words: can the folders that need to be moved back be easily identified, or do you need an exclusion list (like "C:\Windows", "C:\Program Files", ...)?
0
 
LVL 29

Expert Comment

by:serialband
ID: 39921501
You should have done this.
@echo off
for /d %%a in ("C:\MyFiles\ABC\*.*") do (
	echo Processing '%%~nxa' ...
	move "%%a" C:\myFiles
)

Open in new window



But now, you should do this to set it right, assuming I understand what it is you wish to move.
@echo off
for /d %%a in ("C:\ABC\*.*") do (
	echo Processing '%%~nxa' ...
	move "%%a" C:\myFiles
)

Open in new window



You could always select one folder, then press Control+A to select ALL folders and dragged and dropped once.  "Moves" are relatively quick operations since they really just toggle the directory structure and not actually move the file data.
0
 

Author Comment

by:DebbieFost
ID: 39921638
I appreciate your efforts, but I tried your updated code and  all this is doing is MOVING the folders with the topfolder name intact. I know how to move folders, but that is not what I am trying to accomplish.

Perhaps another way of putting it - I need to DELETE the top level folder, without deleting the underlying subfolders, or as you had put it -  I need to PROMOTE the subfolder immediately underneath to become the top level and remove the original top level folder.
0
 
LVL 84

Expert Comment

by:oBdA
ID: 39921646
I have to ask again: what's your current situation? Do the folders need to be moved back, or did you just try it on some test folders?
0
 
LVL 84

Expert Comment

by:oBdA
ID: 39921656
And just in case: are the subfolders that need to be promoted guaranteed to be unique under the ABC folders, so that they can be moved into the same new top folder, or is there a risk that folder 12345 may appear both under ABC and DEF?
0
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 39921706
Assuming you've only been operating on test folders, for the initial situation, you need the script below. It's currently in test mode and will only display the "move" commands it would normally run, so that you can test if it does what you want. Remove the uppercase ECHO in line 7 to move the folders for real. In addition, you can remove the ECHO in front of "rd" in line 9 as well to delete the old root folders (will only delete the folder if it's actually empty).
If duplicate subfolders exist, you'll get an "Access denied" on the second folder, and the second folder will not be moved (and its parent not deleted).
@echo off
setlocal enabledelayedexpansion
set SourceFolder=C:\Temp\MyFiles
for /d %%a in ("%SourceFolder%\*.*") do (
	echo Processing '%%~nxa' ...
	for /d %%s in ("%%a\*.*") do (
		ECHO move "%%s" "%SourceFolder%"
	)
	ECHO rd "%%a"
)

Open in new window

0
 

Author Comment

by:DebbieFost
ID: 39921913
Awesome job....that works brilliantly!!!

Is there anyway to force a folder to merge during this process? I will have a lot of second level folders that will have the same names, and it would be nice to have them merge together.

If not, a log file created by this job might work - I could use it to script a merge copy.
0
 
LVL 84

Expert Comment

by:oBdA
ID: 39922008
Well, the question with a merge would be what to do with duplicate files.
Here's a version that creates a log file; again in test mode (line 15 and 25), you can set the log file in line 4, the delimiter for the log file in line 5, and if you want to clean out the log file before each run, remove the REM in line 6.
@echo off
setlocal enabledelayedexpansion
set SourceFolder=C:\Temp\MyFiles
set LogFile=C:\Temp\MoveFolders.log
set Delim=;
REM if exist "%LogFile%" del "%LogFile%"
for /d %%a in ("%SourceFolder%\*.*") do (
	echo Processing '%%~nxa'
	for /d %%s in ("%%a\*.*") do (
		echo 	- '%%~nxs' ...
		if exist "%SourceFolder%\%%~nxs" (
			echo 	... duplicate.
			>>"%LogFile%" echo "%%s"%Delim%"DUPLICATE"
		) else (
			ECHO move "%%s" "%SourceFolder%" >NUL
			if errorlevel 1 (
				echo 	... error.
				>>"%LogFile%" echo "%%s"%Delim%"ERROR"
			) else (
				echo 	... OK.
				>>"%LogFile%" echo "%%s"%Delim%"OK"
			)
		)
	)
	ECHO rd "%%a"
)

Open in new window

0
 

Author Comment

by:DebbieFost
ID: 39923600
Just in case it is possible - there would not be duplicate files....only duplicate folder names. Don't know if that changes anything
0
 
LVL 84

Expert Comment

by:oBdA
ID: 39923863
The safest solution would then be to "robocopy /mov" a duplicate subfolder into the primary one, otherwise you'd have to walk down the duplicate directory tree and move single files if the the target folder exists already, or whole subfolders if the target folder doesn't exists yet; that could be somewhat error prone.
Disadvantage with robocopy /mov is that it doesn't move the files, it copies a file and then deletes it in the source if the copy was successful, so this won't be as fast as a move.
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many admins will agree: WSUS is is a nice invention but using it on the client side when updating a newly installed computer is still time consuming as you have to do several reboots and furthermore, the procedure of installing updates, rebooting an…
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question