通过Power Shell并行复制多个文件而无需使用任何第三方软件?

时间:2018-09-02 14:33:52

标签: powershell

问题陈述: 我试图将100个文件(每个文件的大小都超过1 GB)从源复制到目标目录,我通过Power-shell脚本将其自动化。执行脚本时,复制操作将按顺序复制文件。我们可以通过任何方式并行复制它们,以减少一些时间,因为复制所有文件需要花费大量时间,并且使用任何第三方软件都受到限制。

    $DATAFileDir="D:\TEST_FOLDER\DATAFILESFX\*"
    $LOGFileDir="D:\TEST_FOLDER\LOGFILESFX\*"
    $DestDataDir="D:\TEST_FOLDER\Data\"
    $DestLogDir="D:\TEST_FOLDER\Log\"

    #Copying the Primary file
    Copy-Item -Path $DATAFileDir -Destination $DestDataDir -Recurse -Force -Verbose
    #Copying the Audit File
    Copy-Item -Path $LOGFileDir -Destination $DestLogDir -Recurse -Force -Verbose

有什么建议吗?

5 个答案:

答案 0 :(得分:0)

您可以为要复制的每个文件启动作业个性化处理。

$Source = Get-ChildItem -Path C:\SourceFolder -Recurse | Select -ExpandProperty FullName
$Destination = 'C:\DestinationFolder'
foreach ($Item in @($Source)){
    #starting job for every item in source list
    Start-Job -ScriptBlock {
        param($Item,$Destination) #passing parameters for copy-item 
            #doing copy-item
            Copy-Item -Path $Item -Destination $Destination -Recurse  -Force
    } -ArgumentList $Item,$Destination #passing parameters for copy-item 
}

答案 1 :(得分:0)

使用powershell workflow,您应该可以轻松实现这一目标。节流限制将限制将并行复制多少个文件。删除它可以并行复制所有文件(可能不建议100个文件复制。)

workflow copyfiles {

    param($files)

    foreach -parallel -throttlelimit 3 ($file in $files) {

        Copy-Item -Path $file -Destination 'C:\destination\' -Force -verbose
    }
}

$files = Get-ChildItem -Path C:\source -Recurse -File

copyfiles $files.FullName

答案 2 :(得分:0)

此powershell脚本直接使用.NET Framework类,即使对于许多文件,其执行速度也应更快。使用throttlelimit来控制所需的并行度。

param([String]$argSourceRootDir,[String]$argTargetRootDir)

workflow copyfiles {

    param($sourceRootDir, $targetRootDir)

    $sourcePaths = [System.IO.Directory]::GetFiles($sourceRootDir, "*.*", "AllDirectories")

    foreach -parallel -throttlelimit 8 ($sourcePath in $sourcePaths) {

        $targetPath = $sourcePath.Replace($sourceRootDir, $targetRootDir)
        $targetDir = $targetPath.Substring(0, $targetPath.Length - [System.IO.Path]::GetFileName($targetPath).Length - 1)
        if(-not (Test-Path $targetDir))
        {
            $x = [System.IO.Directory]::CreateDirectory($targetDir)
            $z = [Console]::WriteLine("new directory: $targetDir")
        }
        $z = [Console]::WriteLine("copy file: $sourcePath => $targetPath")
        $x = [System.IO.File]::Copy($sourcePath, $targetPath, "true")
    }
}

copyfiles $argSourceRootDir $argTargetRootDir

只需将此代码另存为ParallelCopy.ps1并像这样运行它即可:

. ParallelCopy.ps1 "C:\Temp\SourceDir" "C:\Temp\TargetDir"

答案 3 :(得分:0)

或者您可以使用start-threadjob。如果您有ps5,则可以从图库中获取threadjob。 https://powershellgallery.com/packages/ThreadJob/2.0.0 ZoneId indiaStandardTime = ZoneId.of("Asia/Kolkata"); ZonedDateTime timeInIndia = LocalDateTime.parse(utcString, formatter) .atOffset(ZoneOffset.UTC) .atZoneSameInstant(indiaStandardTime); System.out.println(timeInIndia); 在ps 7 https://devblogs.microsoft.com/powershell/powershell-foreach-object-parallel-feature/

起始位传输? https://docs.microsoft.com/en-us/powershell/module/bitstransfer/start-bitstransfer?view=win10-ps

foreach-object -parallel

答案 4 :(得分:0)

如果所有100个文件都已发布到单个redshift表中, Redshift能够使用单个复制命令并行加载多个文件。 查看redshift文档:https://docs.aws.amazon.com/redshift/latest/dg/t_splitting-data-files.html