I have file structure like this:
folder +---a | \---b | \---foo bar +---c | \---d | \---foo bar +---. \---.
I want to move foo bar
to it's own parent folder, using a batch-file.
I tried this in command-line but it said:
The syntax of the command is incorrect.
cd "path to folder"
for /r %x in (foo bar) do move "%x" ".."
Edit
I've changed my command to this:
for /r %x in ("foo bar") do move "%x" "%~px.."
But now it gives an error, because there is space between foo and bar here: ("foo bar")
, what should I do? use foo?bar
instead of "foo bar"
maybe?
Edit 2
my final command:
for /d /r %x in ("?oo bar") do if "%~nx"=="foo bar" move "%x" "%~px.."
I didn't use (*)
because it drastically increases the command execution time if you have hundreds of folders since it has to check every folder for it's name.
Thanks everyone for their help
I have included the below examples, to not use recursion, and use RoboCopy
for the moving action. RoboCopy
has the benefit of being able to merge directories, and overwrite/or otherwise, (depending upon the options you choose), should there be any collisions.
If you're only moving directories with a specific name, e.g. foo bar
then the following command-line may help:
(PushD "folder"&&(For /D %G In (*)Do @For /D %H In ("%G\*") Do @RoboCopy "%H\foo bar" "%G\foo bar" /E /Move)&PopD)>NUL 2>&1
If you're moving all directories, regardless of their names, from three levels below folder
up one level then perhaps this will suffice.
(PushD "folder"&&(For /D %G In (*)Do @For /D %H In ("%G\*") Do @For /D %I In ("%H\*")Do @RoboCopy "%I" "%G\%~nxI" /E /Move)&PopD)>NUL 2>&1
Please note that no checks have been included to determine whether the second level directories, which had contained your targets are now empty. If they are, and you wish to remove them, you'll need to expand the command lines, or add new ones as needed.