Archiving a folder which are x days old using batch file scripting RRS feed

  • Question

  • Hi,
    I have created a batch which will copy the contents(sub folders and files) of an existing folder with a specific name (folder name : "BackupFrom")from c:\ drive and create a new folder with a prefix "SampleRetentionBackup" followed by the date time stamp in "yyyy_mm_dd" format like "SampleRetentionBackup_2018_11_28". After, copying i am looking for the same folder SampleRetentionBackup_2018_11_28" which are 1 days old and moving that folder(s) alone to a new folder in d:\Archive where the Archive folder is created on the fly and move the folders which starts with "SampleRetentionBackup". So far it is working fine. But the only issue is instead of moving the folder which are x days old("SampleRetentionBackup_2018_11_28"), it is moving the recent folder ("SampleRetentionBackup_2018_12_07")to the archive folder in d:\drive. I need some help.

    Please find the batch file scripting below:

    @echo off
    cd c:\ 
    set source=SampleRetentionBackup_%date:~10,4%_%date:~4,2%_%date:~7,2%
    mkdir %source%
    robocopy C:\BackupFrom %source% *.* /S
    set destination=SampleRetentionBackup_%date:~10,4%_%date:~4,2%_%date:~7,2%
    mkdir %destination%
    if not exist d:\Archive\%destination% mkdir d:\Archive\%destination%
    cd d:\Archive\%destination%
    robocopy %source%  /s /e /minage:2 /move d:\Archive\%destination%

    • Moved by Bill_Stewart Friday, March 15, 2019 2:59 PM This is not "fix/debug/rewrite my script for me" forum
    Friday, December 7, 2018 6:44 AM

All replies

  • Your RoboCopy commands are wrong.

    You should be using PowerShell.  "Batch" is obsolete.

    $folder = 'SampleRetentionBackup_{0:yyyy_MM_dd}' -f [datetime]::Today.AddDays(-1)
    $source = Join-Path C:\BackupFrom $folder
    Write-Host "Source $source"
    Move-Item $source D:\Archive -Force

    Adjust "AddDays" as needed to select the correct folder


    • Edited by jrv Friday, December 7, 2018 10:37 AM
    Friday, December 7, 2018 10:28 AM