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.