-2

I have hundreds over folders with structure like this:

  • PARENT\FolderA\Subfolder01\files1.iso
  • PARENT\FolderB\Subfolder02\files2.iso
  • PARENT\FolderC\Subfolder03\files3.iso

I want to move all the files1.iso, files2.iso, files3.iso up one level respectively. Should look like this.

  • PARENT\FolderA\files1.iso
  • PARENT\FolderB\files2.iso
  • PARENT\FolderC\files3.iso

And what would be even better is something that work to delete the Subfolder01, Subfolder02, Subfolder03 which are not wanted.

And if possible, as well batch rename those files1.iso, files2.iso, files3.iso to the name of FolderA.iso, FolderB.iso, FolderC.iso respectively.

I really have no idea how to work this out. Anybody can help?

genzi0401
  • 3
  • 1
  • 4
  • 2
    Possible duplicate of [move all files from "old" folders out into the parent as long as there are no files existing in the parent folder yet](https://stackoverflow.com/questions/51542843/move-all-files-from-old-folders-out-into-the-parent-as-long-as-there-are-no-fi) – Jeff Zeitlin Jul 30 '18 at 12:13
  • I can't really figure out. All my folders are having different name except the PARENT folder. – genzi0401 Jul 30 '18 at 12:26
  • Another related question: https://stackoverflow.com/questions/48158375/move-folders-to-parent-level-windows?s=11|34.9712 – Jeff Zeitlin Jul 30 '18 at 12:29
  • @JeffZeitlin The only answer to that 2nd question is wrong, just the comment is helpful. –  Jul 30 '18 at 12:37

1 Answers1

1
cd PARENT
for /D %%i in (*) do (
  for /D %%j in (%%i\*) do (
    move "%%j\*" "%%i\%%i.iso" 2>&1>nul && rmdir "%%j" 2>&1>nul
  )
)

An explanation:

cd PARENT

Just make sure you're in the root directory to work from so the rest works

for /D %%i in (*) do (

This is a for loop, for every directory in the working directory it sets %%i to the directory name (e.g. FolderA), then does the following:

  for /D %%j in (%%i\*) do (

This is a nested for loop, for every directory in %%i (on first loop, FolderA) it sets %%j to the directory name (on first loop, FolderA\Subfolder01), then does the following:

    move "%%j\*.iso" "%%i\%%i.iso" 2>&1>nul && rmdir "%%j" 2>&1>nul

Move everything whose name ends with .iso in %%j (FolderA\Subfolder01) to %%i (FolderA), and rename it to %%i.iso (FolderA.iso). If that works, remove the %%j directory. Redirect all output to nul (i.e. produce no output).

  )
)

Close off the loops.

AbyxDev
  • 1,363
  • 16
  • 30
  • I just tried this. It worked partially. It only worked for the first folder. The 2nd and remaining folders do not work. Then I redo it again from scratch, it does not work anymore. – genzi0401 Jul 30 '18 at 12:40
  • Your move command will include any extension, not just .iso. I'd also execute the rmdir only on success of the move `&&` and redirect output to 2&>1>nul so non empty directories which aren't removed won't irritate users. –  Jul 30 '18 at 12:42
  • 2
    You should definitely include doublequotes around paths when you cannot know whether or not they contain characters such as spaces. _For example: `"%%i\*"`, `"%%j\*"`, `"%%i\%%i.iso"` and `"%%j"`_. It is recommended, when using the `CD` command from an unknown current directory to use the `/D` option with the full path, _without it, if the intended directory is on a different drive, the change will not occur as intended_. – Compo Jul 30 '18 at 13:05
  • @Compo Yea, thanks so much. You save the day. Problem solved. – genzi0401 Jul 30 '18 at 13:09
  • @LotPings May I know how to enhance the code to what you mentioned? Thanks and appreciate. – genzi0401 Jul 30 '18 at 13:15
  • 1
    @genzi0401 Change the inner two lines to `move "%%j\*.iso" "%%i\%%i.iso" 2>&1>nul && rmdir "%%j" 2>&1>nul` Sorry other comment had the redirection wrong. –  Jul 30 '18 at 13:47
  • @genzi0401 I have edited the answer with changes from all of the comments, you can see the answer now. – AbyxDev Jul 30 '18 at 14:36
  • @KenHilton still some mistake, the 3rd line, `("%%i\*")` and 4th line, `"%%j\*.iso"` and ya, thanks so much. credit to you. – genzi0401 Jul 30 '18 at 14:44
  • @genzi0401 for the 3rd line: `(%%i\*)` just means every folder in the `%%i` directory, it should be fine. for the 4th line: I was under the impression that you only wanted `.iso` files to be moved? or is all kinds of files ok? – AbyxDev Jul 30 '18 at 14:51
  • @KenHilton Thanks for the explanation. Suppose I want to move only the iso files but there are other files in the same folder too. Ermm.. by the way, really thanks for your help. You have made me save lots of time. – genzi0401 Jul 31 '18 at 04:05
  • @genzi0401 If you only want to move the iso files AND there are other files, that means you can't remove the subfolders; but the script should work for that purpose too, as the `rmdir` will silently fail for each non-empty subfolder. You're completely welcome - glad to have helped. – AbyxDev Jul 31 '18 at 05:34