Link to home
Start Free TrialLog in
Avatar of DebbieFost
DebbieFostFlag for United States of America

asked on

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

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.
Avatar of oBdA
oBdA

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

Avatar of DebbieFost

ASKER

The batch text returns the following error:

 "\MyFiles\*.*"> do < was unexpected at this time
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

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>
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", ...)?
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.
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.
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?
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?
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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

Just in case it is possible - there would not be duplicate files....only duplicate folder names. Don't know if that changes anything
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.