2

Task:

Folder1>Subfolder1>Stuff1
Folder1>Subfolder2>Stuff2
...

Move Subfolder contents to Parent folder (Stuff1/2 are subdirectories that have other files or subdirectories inside)

Folder1>Subfolder1
Folder1>Subfolder2
Folder1>Stuff1
Folder1>Stuff2

I tried:

for /d %f in (.\*) do (
pushd %f
move .\%f\* ..
popd
)

But that didn't work.

2 Answers 2

2

You can't do a move * .. for directories. The only way for move to move a directory is to specify the directory name itself (no wildcards allowed). So you need a second loop (within the first) to loop through the directories:

@echo off
for /d %%d in ("*") do (
  for /d %%e in ("%%d\*") do (
    move "%%e" .
  )
)

No need for pushd because you can move it from the parent to the parent.

note: this is used for a batch file so for the %d the % is doubled to %%d and %%e. If you run it directly from the prompt you only need one %.

Edit:
If the first-level subfolders (Subfolder1 and Subfolder2) also contain files (besides folders Stuff1 and Stuff2) you can add another move to the outer for.

@echo off
for /d %%d in ("*") do (
  for /d %%e in ("%%d\*") do (
    move "%%e" .
  )
  move "%%d\*" .
)
5
  • Thanks, worked like a charm. Didn't realize you couldn't use wildcards with moving directories.
    – key2starz
    Nov 9, 2013 at 21:08
  • The inner for-loop shoudn't have a /d option if files have to be moved. With the /d option, only folders are iterated. Sep 2, 2014 at 14:42
  • @OlivierJacot-Descombes With the /d option, only folders are iterated: Which was exactly what the question was. To move directories within sub-directories to the main level. Nothing about the files in the first level of directories. Otherwise you could just add one extra move as last command of the outer for (just before the last )).
    – Rik
    Sep 2, 2014 at 15:03
  • The question says: "Move Subfolder contents to Parent folder [...] that have other files or subdirectories inside". Sep 2, 2014 at 16:08
  • 1
    @OlivierJacot-Descombes He showed Subfolder1/2 contains Stuff1/2 which contains subfolders and files. He doesn't show Subfolder1/2 contained files. I guess the answer was good enough in his situation but i added a note to also move extra files. Removing /d in the second for doesn't work though because you miss the subdirectories (Stuff1/2). You need to add another move to the outer for.
    – Rik
    Sep 2, 2014 at 20:33
0

Try and use xcopy in the command prompt. You can read more about xcopy here: Xcopy Xcopy have the ability to copy folder,sublfolders and files all at once. You can write a small batch file to automate the process, if you want to use it more than once.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .