Robocopy






param (
    [string]$SourcePath,
    [string]$DestinationPath,
    [string]$LogFile = "C:\robocopy_log1.txt"
)

# Function to log messages
function Write-Log {
    param (
        [string]$Message
    )
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    "$timestamp - $Message" | Out-File -Append -FilePath $LogFile
}

# Function to start copying with Robocopy
function Start-Copy {
    param (
        [string]$Source,
        [string]$Destination
    )
    
    Write-Log "Starting copy from $Source to $Destination"
    robocopy "$Source" "$Destination" /E /COPYALL /R:2 /W:2 /LOG+:$LogFile
    Write-Log "Completed copy from $Source to $Destination"
}

# Function to process folders recursively using child jobs
function Process-Folders {
    param (
        [string]$CurrentSource,
        [string]$CurrentDestination
    )
    
    Start-Copy -Source $CurrentSource -Destination $CurrentDestination
    
    $subFolders = Get-ChildItem -Path $CurrentSource -Directory
    foreach ($folder in $subFolders) {
        $newSource = Join-Path -Path $CurrentSource -ChildPath $folder.Name
        $newDestination = Join-Path -Path $CurrentDestination -ChildPath $folder.Name
        
        Write-Log "Creating job for subfolder: $newSource"
        Start-Job -ScriptBlock {
            param ($src, $dest, $logFile)
            function Write-Log {
                param ([string]$Message)
                $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
                "$timestamp - $Message" | Out-File -Append -FilePath $logFile
            }
            
            function Start-Copy {
                param ([string]$Source, [string]$Destination)
                Write-Log "Starting copy from $Source to $Destination"
                robocopy "$Source" "$Destination" /E /COPYALL /R:2 /W:2 /LOG+:$logFile
                Write-Log "Completed copy from $Source to $Destination"
            }
            
            function Process-Folders {
                param ([string]$CurrentSource, [string]$CurrentDestination, [string]$logFile)
                Start-Copy -Source $CurrentSource -Destination $CurrentDestination
                $subFolders = Get-ChildItem -Path $CurrentSource -Directory
                foreach ($folder in $subFolders) {
                    $newSource = Join-Path -Path $CurrentSource -ChildPath $folder.Name
                    $newDestination = Join-Path -Path $CurrentDestination -ChildPath $folder.Name
                    Write-Log "Processing subfolder: $newSource"
                    Process-Folders -CurrentSource $newSource -CurrentDestination $newDestination -logFile $logFile
                }
            }
            
            Process-Folders -CurrentSource $src -CurrentDestination $dest -logFile $logFile
        } -ArgumentList $newSource, $newDestination, $LogFile
    }
}

# Start processing
Write-Log "Starting folder processing from $SourcePath to $DestinationPath"
Process-Folders -CurrentSource $SourcePath -CurrentDestination $DestinationPath

# Wait for jobs to complete
Get-Job | Wait-Job | Receive-Job
Get-Job | Remove-Job
Write-Log "All jobs completed."

Post a Comment

0 Comments