0

I have a root directory C:\ROOT and this directory contains a number of subfolders, with names like these:

C:\ROOT\Folder0001
C:\ROOT\Folder0002
C:\ROOT\Folder0003
...
C:\ROOT\Foldernnnn

Each subfolder contains a certain number of files. All the files and subfolders must be processed.

I am preparing a Windows Shell script to backup the whole tree under C:\ROOT to D:\ROOT and this must be done according to the following "full overwrite, not merge" scheme:

  • If the C:\ROOT\Folderxxxx subfolder does not exist yet under D:\ROOT, it must be created and all the files inside the origin subfolder must be copied to the destination subfolder.

  • If the C:\ROOT\Folderxxxx subfolder already exists under D:\ROOT, all the files in the destination subfolder must be deleted first and then the origin subfolder should be copied to the destination one as in the previous case. Namely, I need a total subdirectory overwrite instead of a merge.

Now, thanks to your help, I found how to delete all the files in the destination subfolder:

if NOT ERRORLEVEL 1 del "%%i\*.*"

and to go through the root directory:

@echo off
CD /d "C:\ROOT"
CLS
FOR /R "C:\ROOT\" %%i IN (a*.*) DO
(
 xcopy /s /v /y "C:\ROOT" "D:\ROOT\"
 echo %%i
)

Where I am stuck at is how to discriminate between the create new subfolder and overwrite an existing subfolder.

Looking at info available on this site, I found how to check if a folder exists or not

if not exist ...

so I think that I should use something like this:

for /r /d %%i in (*)do chDir /d "%%~dpnxi" && =;(
 xcopy ".\%%~nxi.*" | find /i ".*"
 if exist ".\\%%~nxi.*" 2>nul del ".\*.*"
);=

but this is on a file basis, not a subfolder one and I have no clue as to put the pieces together.

Thanks in advance if anyone lends again a hand.

1
  • 1
    Are you trying to reinvent robocopy ?
    – harrymc
    Commented Jul 24, 2023 at 17:38

0

You must log in to answer this question.

Browse other questions tagged .