jeffshumphreys / filmcab

A film collector's management system

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Better asynch non-locking copy massive files

jeffshumphreys opened this issue · comments

Start-BitsTransfer -Source https://server01/servertestdir/. -Destination c:\clienttestdir\

Run it as job? Then show status in GUI? Probably max 3 jobs, same as in Windows Explorer.

$scriptblock = {
    $BasePath = $args[0]
    $TargetPath = $args[1]
    $files = Get-ChildItem -File -Recurse -Path "$($BasePath)\$($Filename)" -ErrorAction SilentlyContinue

    foreach ($file in $files)
    {
        $subdirectorypath = split-path $file.FullName.Replace($BasePath, "").Trim("\")
        $targetdirectorypath = "$($TargetPath)\$($subdirectorypath)"
        if ((Test-Path $targetdirectorypath) -eq $false)
        {
            Write-Host "Creating directory: $targetdirectorypath"
            md $targetdirectorypath -Force
        }

        Write-Host "Copying file to: $($targetdirectorypath.TrimEnd('\'))\$($File.Name)"
        Move-Item $File.FullName "$($targetdirectorypath.TrimEnd('\'))\$($File.Name)" -Force
    }
}

$arguments = @("C:\From","C:\To")
start-job -scriptblock $scriptblock -ArgumentList $arguments

Found this online (no credits given).

Testing.

$scriptblock = {
    $SourcePath = $args[0]
    $TargetPath = $args[1]
    #$SourceFilesAndDirectories = Get-ChildItem -File -Recurse -Path "$($SourcePath)\$($Filename)" -ErrorAction SilentlyContinue
    Write-Host "`$SourcePath = $SourcePath"
    Write-Host "`$TargetPath = $TargetPath"
    $SourceFilesAndDirectories = Get-ChildItem -Recurse -LiteralPath "$SourcePath" -ErrorAction SilentlyContinue

    foreach ($fileOrDirectory in $SourceFilesAndDirectories)
    {
        $MeaningfulPartOfSourcePath = ($fileOrDirectory.FullName.Substring($SourcePath.Length).Trim("\"))
        Write-Host "`$MeaningfulPartOfSourcePath = $MeaningfulPartOfSourcePath"
        $NewlyConstructedTargetPath = "$($TargetPath)\$($MeaningfulPartOfSourcePath)"
        if ((Test-Path -LiteralPath $fileOrDirectory.FullName -PathType Container))
        {
            # For cases where directories are empty, we still want to move them over.
            Write-Host "Creating directory: $NewlyConstructedTargetPath"
            New-Item -Path $NewlyConstructedTargetPath -ItemType Directory -Force|Out-Null
        } else {
            Write-Host "Copying file to: $NewlyConstructedTargetPath"
            # We want to use Move-Item because of space concerns when moving to the same spindle.  Huge moves of many files and directories could run out of space with Copy-Item
            Move-Item -LiteralPath $fileOrDirectory.FullName -Destination $NewlyConstructedTargetPath -Force|Out-Null
        }
    }
    Write-Host "`$SourcePath = $SourcePath"

    # This deletes the source
    Remove-Item -LiteralPath $SourcePath -Force -Recurse -ErrorAction SilentlyContinue|Out-Null
}

Much better. Moves all directories as well.

Great progress. However, issues with migrating drives vs. just moving and replacing last folder ("from", "to"). So setting up flags.