0

I need a batch file to move the files from this:

F:\123456\V000\Chaptered\File1
F:\123456\V000\Chaptered\File2
F:\123457\V000\Chaptered\File1
F:\123457\V000\Chaptered\File2

to this

F:\123456\File1
F:\123456\File2
F:\123457\File1
F:\123457\File2

while deleting the folders V000 and Chaptered but not deleting any files.

I've tried to make a batch file in several different ways, but can't get it to work.

@echo off & setlocal enabledelayedexpansion
for /d /r %~"dp0" %%a in (*) do (
    if /i "%%~nxa"=="FolderX" (
       set "folderpath=%%a"
       move /y "!folderpath!\*" "!folderpath!\.."
       rmdir "!folderpath!"
   )
)

I'd hope to achieve the file structure above, but have not been able to get anything to change. (I'm trying to be careful about what I delete as the files are important.)

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Firstly.. `%~"dp0"` should be `"%~dp0"` then `rd /S /Q` generally deletes folders including files and subfolders in quiet mode... What I don't know however is what your folder should be? you do a match of `FolderX` but your input and expected results does not show this. Instead it seems you are actually looking for a directory called `Chaptered` and then looking to copy 2 folders back.. `"!folderpath!\..\.."` – Gerhard May 07 '19 at 13:33
  • @GerhardBarnard I'm trying to get a lot of different folders structured this way fixed all at once. The files are in the folder "Chaptered" which is in the folder V000 which is in the folders 123456, 123457, etc. I want the files to be in each of the folders 123456, 123457, etc. I'm putting all of these folders in a larger folder to run the batch file on all of them at once. – Rolf Quackenbush May 07 '19 at 19:10
  • Ok. I was going to answer but @Magoo already gave a similar version to.my answer below. Ao have a look at what he posted. Keep in mind what I mentioned about `folderx` vs `chaptered` – Gerhard May 07 '19 at 20:05

2 Answers2

1
@echo off
setlocal
for /d /r "%~dp0" %%a in (*) do (
    if /i "%%~nxa"=="FolderX" (
       ECHO move /y "%%a\*" "%%a\..\.."
       ECHO rmdir "%%a"
   )
)

(Untested)

Theory - %%a will be assigned the name of each subdirectory in turn. Only leaves named FolderX will be selected for processing; the contents of that directory will be moved to its grandparent.

The required commands should be reported by the echos - a safety device to ensure no damage is done during testing until debugging has been completed.

Problem is that if the target directory contains subdirectories then those subdirectories will also be deleted.

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

Well, if your target files are always in the same directory hierarchy level, I suggest to not search files in the whole directory tree but only in that level, like this:

@echo off
rem // Enumerate the variating destination directories and check sub-directory tree for existence:
for /D %%I in ("F:\*") do if exist "%%~I\V000\Chaptered\" (
    rem // Move found files to the destination directories:
    move /Y "%%~I\V000\Chaptered\*.*" "%%~I\"
    rem // Remove the sub-directory tree:
    rmdir "%%~I\V000\Chaptered" "%%~I\V000"
)
aschipfl
  • 33,626
  • 12
  • 54
  • 99