Friday, October 27, 2017

windows - rename (or move) folders, subfolders and files on specific path

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
pause
goto:eof
:test
set test=%1
CD /D path:\
if exist "%test%" (goto make) else (goto end)
:make
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
:end
echo OK
goto:eof

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

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...