Solved

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

Posted on 2014-03-11
14
2,218 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
  • 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever had a hard drive that you can't boot into, but need to change the registry? Here is the solution! This article guides you through accessing and editing a registry of a non-primary drive. To read registry information on a non-prim…
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…
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…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

808 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