Home > other >  Batch program to copy file when there is a change in source folder
Batch program to copy file when there is a change in source folder

Time:06-26

I need a batch program that will copy files from source folder to destination folder whenever there is a change(new files added or files modified) in source folder and restart a specific service.

CodePudding user response:

Refer to the help of xcopy /? using the switch /D

/D : Copy files changed on or after the specified date. If no date is given, copy only files whose source date/time is newer than the destination time.

Syntax :

XCOPY source [destination] [options]

Remark :

So, in this batch file below, if you have any spaces in your directory names, you need to enclose them in double quotes.


@echo off
Set SourceDir="C\My Source\stuff"
Set TargetDir="D:\My Backup\stuff"
xcopy %SourceDir% %TargetDir% /i /d /y /e

/D : copy only files whose source date/time is newer than the destination time.

/E : To copy everything, including new directories, you should add the /e switch

/Y : Suppress prompt to confirm overwriting a file.

/I : If in doubt always assume the destination is a folder e.g. when the destination does not exist.

CodePudding user response:

Similarly to @Hackoo's answer, you can also use RoboCopy.

See here: https://www.ubackup.com/windows-10/robocopy-windows-10-6988.html

and also here for the official Microsoft docs : https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy

if you prefer to have it all in your terminal, Win R, cmd, Enter, and then robocopy /?

Hope this helps you,

Silloky

  • Related