Search code examples
windowsbatch-filexcopy

How to copy to a folder on a Sunday but delete on every other day?


How can I move/copy a file from D:\WindowsImageBackup\ to D:\Included\ if it's a Sunday or delete D:\Included\ if it's any other day?

The scenario is: An automatic backup to a remote server runs once a day. I want a certain file to be backed up only once a week, on a Sunday (it's the Windows image backup). I have excluded the folder where the file lives (D:\WindowsImageBackup).


Solution

  • You can do it pretty easily:

    setlocal enableextensions enabledelayedexpansion
    set dow=%date:~0,3%
    if "%dow%" equ "Sun" xcopy /y "D:\WindowsImageBackup\*.* "D:\Included\" & goto eof
    echo del "D:\Included\*.*" /y
    :eof
    

    Note that this works only if your system language is English, as it does a literal comparison of the day part of Date against the value Sun, and of course isn't localized.

    Also, see my comment to your question about this perhaps being better for a scheduled task (as errors would stop it and write to the Event Log).