I have something that makes a new folder with files in every night. Does anyone know how to create a script that will copy the content of the newest/latest folder to a network share and overwrite any of the files and folders in the network share? Been scratching my head for awhile.
Thanks in advance.
Something like this might serve your purpose:
The
src_rootvariable is supposed to contain the path to the folder where your daily folders are created, andtgt_pathis the target path for the latest folder’s contents to copy.The
DIRcommand is set up to return the contents of the root folder in the following manner:no extra information in the output, only names (
/B);no files, just folders (
/AD);sort (
/O…) the output in the descending (…-…) order of folder date (…D);the date is that of the folder’s creation (
/TC).The output is redirected to a temporary file, whose first line is then read into a variable (the
SET /Pcommand). That piece of information, together with the root path and the target path, is ultimately used first with deleting, then with copying files.The deleting is done in two steps: first the folders (the
RDcommand in theFOR /Dloop), then the files (DEL). I’d like to note at this point that this script doesn’t assume intervention at any stage on your side, which I understood was your intention. Consequently, it doesn’t expect a confirmation to delete the files and folders at the target path, so, when you run the script, the old contents will be deleted silently (which is the effect of the/Qswitch used with bothRDandDEL).The copying is done with
XCOPY, as it allows us to retain the structure of the source folder (the/Sswitch), including empty subdirectories (/E), if any.You can get more information on every command mentioned here by invoking any of them with the
/?switch at the command prompt.