Moving files to archive using Robocopy

Renessance 1 Reputation point
2021-11-30T09:17:55.997+00:00

Hi All!

I'm working on a batch file with a robocopy command in it to periodically run via task scheduler and move folders that haven't been modified for 120 days from one network location to another network location. But I'm not sure if I am understanding the command and attributes completely. It seems the script searches for files within subfolders as well. Could you help me out and let me know if I'm on the right way? And if I'm using too much attributes or forgetting something?

  • I only want to move folders (and the folders and files within them) that have not been modified for 120 days to the archive destination. (Based on the main folders modified date)
  • I only want to move folders that have a name that starts with "A2021" or "B2021" and all of their contents.
  • The folder structure and files within these folders need to be copied as well, even empty ones.
  • The script only needs to search for files and folders within the source directory. Not in subdirectories.
  • I only want the complete folders to be moved. Not per subfile or subfolder that matches the criteria and is within these folders.
  • Everything at the destination can be overwritten, modified timestamp can be updated.
  • I don't want the script to look for files or folders that match "A2021" as part of the name, within project folders that are in the source dir. This should only be done on the main dir.

This is what I have right now:

@Echo ON
SETLOCAL
SET SourceDir=\XXX.XXX.XXX.XXX\Projects
SET TargetDir=\XXX.XXX.XXX.XXX\Archive\Projects-2021
SET LogFile=\XXX.XXX.XXX.XXX\Archive_Logs\Logfile-2021-job.txt
ROBOCOPY "%SourceDir%" "%TargetDir%" A2021* B2021* C2021* /E /MOVE /R:5 /LOG+:%LogFile% /TS /FP /ETA /L /Z /MT:32 /minlad:120
GOTO EOF

Thanks a lot for your help!

Windows for business Windows Server User experience Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2021-11-30T18:08:56.217+00:00

    I think you'll have better luck with Powershell. Give this script a try. Remove the WhatIf's to actually do the move.

    $src = 'c:\temp\foo1'
    $dst = 'c:\temp\foo2'
    $targetDate = (Get-Date).AddDays(-120)
    "{0} - Archive script executing." -f (get-date)
    "Looking for folders older than {0}" -f $targetDate
    $folders = Get-ChildItem  $src -Directory  -include "A2021",  "B2021", "foo*" | where-object {$_.LastWriteTime -lt $targetDate}
    foreach ($folder in $folders) {
        "Processing {0} - {1}" -f $folder.Name, $folder.LastWriteTime
        $newFolder = "$dst\$($folder.name)"
        if (Test-path $newFolder) {
            "Destination folder already exists!"
            "Deleting {0}" -f $newFolder
            Remove-Item $newFolder -recurse -force -WhatIf
        } 
        Move-Item $folder.FullName -Destination $dst -WhatIf 
    }
    "Complete."
    

    To run it in task scheduler, create a .bat file like this using the name of the PS script.

    powerhell.exe C:\Scripts\Archive.ps1 1>> C:\Logs\Archive.%date:~10,4%-%date:~4,2%-%date:~7,2%.log 2>&1 
    

    Set the task to run cmd.exe with the name of the bat file as an argument. What that will do is to capture stdout and stderr from Powershell so that if anything goes wrong, an entry will be in the log.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.