Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2718
  • Last Modified:

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.
0
DebbieFost
Asked:
DebbieFost
  • 8
  • 5
1 Solution
 
oBdACommented:
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
 
DebbieFostAuthor Commented:
The batch text returns the following error:

 "\MyFiles\*.*"> do < was unexpected at this time
0
 
oBdACommented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
DebbieFostAuthor Commented:
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
 
oBdACommented:
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
 
serialbandCommented:
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
 
DebbieFostAuthor Commented:
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
 
oBdACommented:
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
 
oBdACommented:
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
 
oBdACommented:
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
 
DebbieFostAuthor Commented:
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
 
oBdACommented:
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
 
DebbieFostAuthor Commented:
Just in case it is possible - there would not be duplicate files....only duplicate folder names. Don't know if that changes anything
0
 
oBdACommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now