I have a function (.bat) to search and rename (or move) any folder with a specific name folder (call "data"), with all its contents intact, on a specific path (path:\ Is the specific route like c:\ or h:)

I need to simplify it and make it work.

call:test "TEST1"
call:test "TEST2"

:: funcion test
@echo off
 set test=%1
 CD /D path:\
 if exist "%test%" (goto make) else (goto end)
 MOVE /Y "%test%" data
 FOR /F %%x IN ("%test%") DO REN "%%x" data
 FOR /F "tokens=*" %%G IN ('DIR /B /AD /S "%test%"') DO MOVE /Y "%%G" data
 echo OK

The TEST1 and TEST2 folders contain many files and subfolders. The problem is that i use three commands to do this work and not 100% works.

 MOVE /Y "%test%" data
 FOR /F %%x IN ("%test%") DO REN "%%x" data
 FOR /F "tokens=*" %%G IN ('DIR /B /AD /S "%test%"') DO MOVE /Y "%%G"

I need to rename the folders TEST1 and TEST2, wherever they are within the path (may be in the root or in subfolders. Both should be renamed (merged) as "data").

Example: The TEST1 and TEST2 folders have content (files and subfolders). The ".bat" search into the path to find TEST1 and when it finds renamed TEST1 with "data". Keep looking and find TEST2 and rename as "data" too without asking.

But, in the case of both folders (TEST1 and TEST2) are within the same location, both should be merged (one overrides the other). The important thing is to do it without asking.

But "MOVE /Y" does not overwrite folders (only files). I think the solution could be that instead of overwriting, rename the file or folder duplicate to avoid this failure command

Note: Previously i used Robocopy with "move" option...

SET MoveDirSource=path:\"%test%"
SET MoveDirDestination=path:\data
MKDIR "%MoveDirDestination%"
FOR %%i IN ("%MoveDirSource%\*") DO MOVE /Y "%%i" "%MoveDirDestination%\%%~nxi"
FOR /D %%i IN ("%MoveDirSource%\*") DO ROBOCOPY /MOVE /E /W:5 "%%i" "%MoveDirDestination%\%%~nxi"

But it is not viable, because robocopy, first, copy from source to destination and then removed at source. And if TEST1 or TEST2 folders have 2 or 6 GB of information, this process can take hours.

Thanks a lot

  • thanks @DavidPostill That's why I have published. To help me correct that. Remember that the TEST1 and TEST2 folder can be repeated within subfolders – BrianC Oct 7 '16 at 21:26

I would use

ROBOCOPY "%Test%" Data /MOVE /E

Robocopy is installed by default in windows and does all what you want in one pass.

| improve this answer | |
  • thanks but robocopy is not viable. Read why in "Note:". Thanks anyway – BrianC Oct 7 '16 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.