Solved

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

Posted on 2014-03-11
14
2,105 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 83

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 83

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
 

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 83

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 27

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 83

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 83

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 83

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 83

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 83

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The password reset disk is often mentioned as the best solution to deal with the lost Windows password problem. In Windows 2008, 7, Vista and XP, a password reset disk can be easily created. But besides Windows 7/Vista/XP, Windows Server 2008 and ot…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now