Something like this might serve your purpose:
SET "src_root=D:\root\for\source\directories" SET "tgt_path=\\NETWORK\SHARE\target\path" DIR "%src_root%" /B /AD /OD /TC > "%TEMP%\dirlist.tmp" < "%TEMP%\dirlist.tmp" SET /P last_dir= FOR /D %%I IN ("%tgt_path%\*") DO RD "%%I" /S /Q DEL "%tgt_path%\*" /Q XCOPY "%src_root%\%last_dir%\*" "%tgt_path%" /S /E
It is assumed that the src_root variable contains the path to the folder in which your daily folders are created, and tgt_path is the destination path for the last contents of the folder to copy.
The DIR command is configured to return the contents of the root folder as follows:
there is no additional information on the output, only names ( /B );
no files, just folders ( /AD );
sort ( /Oโฆ ) the output in descending order ( โฆ-โฆ ) the date of the folders ( โฆD );
date is the date the folder was created ( /TC ).
The output is redirected to a temporary file, whose first line is then read into the variable ( SET /P command). This piece of information, together with the root path and the target path, is ultimately used first with deleting and then copying the files.
Deletion is performed in two stages: first the folders ( RD command in the FOR /D cycle), then the files ( DEL ). At this stage, I would like to note that this script does not involve intervention at any stage of your part, and I realized that this is your intention. Therefore, it does not expect confirmation of the deletion of files and folders on the target path, so when the script is run, the old contents will be deleted silently (which is a consequence of the /Q switch used with both RD and DEL ).
Copying is performed using XCOPY , as this allows us to preserve the structure of the original folder (switch /S ), including empty subdirectories ( /E ), if any.
Can you get additional information about each command mentioned here by calling any of them with /? on the command line.
source share