I suggest one avoid using undocumented objects like the xp_copy_files
extended stored procedure. These are intended to be used internally by SQL Server tools and utilities. Undocumented objects are unsupported and may be changed or removed in subsequent versions and updates, breaking code that uses them.
Albeit it may be more convenient in some cases to manage files using T-SQL, a more robust solution is to leverage a scripting language like Powershell scheduled with SQL Server Agent. This will also mitigate concerns of using xp_cmdshell
, which many organizations prohibit for security reasons.
Below is a basic example for your task:
try {
$copyCreationTime = (Get-date).AddDays(-1)
$Files = Get-ChildItem "C:\SQLServer2022LocalBackups\*.bak" -File
foreach ($File in $Files)
{
if ($File.CreationTime -lt $copyCreationTime)
{
Copy-Item $File -Destination "S:\SharedTargetFolderLocation\"
Write-Host "Copying $File"
}
}
}
catch {
throw
}