复制功能的ProgressBar Overlay或ProgressBar

时间:2015-03-15 05:56:02

标签: powershell-v2.0 powershell-remoting

Function GetTotalBytesOfCopyDestination{ 

    param($destinationPath);
    $colItems = (Get-ChildItem $destinationPath | Measure-Object -property length -sum)
    return $colItems.sum;
}

Function GetBytesOfFile{

    param($sourcePath);
    return (Get-Item $sourcePath).length; 
}

Function GetPosition{

    param([double]$currentOfBytesSended)
    param([double]$countsOfBytesWillSend)
    $position = ($currentOfBytesSended / $countsOfBytesWillSend) * 100;

    #range 0 - 100
    #(15800 bytes / 1975633689 bytes)*100
    #

    return $position;
}

Function Copy-File {
    #.Synopsis
    # Copies all files and folders in $source folder to $destination folder, but with .copy inserted before the extension if the file already exists
    param($source,$Destination2)

    # create destination if it's not there ...
    mkdir $Destination2 -force -erroraction SilentlyContinue

    [double]$currentOfBytesSended = 0;

    [double]$countsOfBytesWillSend = 0;
    [double]$countsOfBytesWillSend = GetTotalBytesOfCopyDestination($source);

    $progressbar6.Maximum = 100;
    $progressbar6.Step = 1;

    foreach($original in ls $source -recurse) {
      $result = $original.FullName.Replace($source,$Destination2)
      while(test-path $result -type leaf){ $result = [IO.Path]::ChangeExtension($result,"copy$([IO.Path]::GetExtension($result))") }

        [System.Windows.Forms.Application]::DoEvents()

        if($original.PSIsContainer) {
        mkdir $result -ErrorAction SilentlyContinue
         } else {
        copy $original.FullName -destination $result

        [System.Windows.Forms.Application]::DoEvents()

            $currentCopyingFileSizeInBytes = 0;
            $currentCopyingFileSizeInBytes = GetBytesOfFile($original.FullName);

            $currentOfBytesSended = [double]$currentOfBytesSended + [double]$currentCopyingFileSizeInBytes;
            #$currentOfBytesSended += $currentCopyingFileSizeInBytes;


            $progressbar6.Value=GetPosition([double]$currentOfBytesSended, [double]$countsOfBytesWillSend);

            [System.Windows.Forms.Application]::DoEvents()

            #$progressbar6.PerformStep();
            $progressbar6.Refresh();
      }
    }
    }

我想要的是复制文件功能,复制文件&从远程机器到本地机器的目录,同时移动进度条取决于要复制的总金额,哪些已经复制,它定义了进度条的位置,我收到此错误

ERROR: GetPosition : Cannot process argument transformation on parameter 'currentOfBytesSended'. Cannot convert the "System.Object[]" value of type "System.Object[]"
ERROR: to type "System.Double".
Assit App.pff (337): ERROR: At Line: 337 char: 35
ERROR: +                 $progressbar6.Value=GetPosition([double]$currentOfBytesSended, [double]$coun ...
ERROR: +                                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidData: (:) [GetPosition], ParameterBindingArgumentTransformationException
ERROR:     + FullyQualifiedErrorId : ParameterArgumentTransformationError,GetPosition
ERROR:
>> Script Ended

1 个答案:

答案 0 :(得分:0)

首先,欢迎来到StackOverflow。尝试将您的问题分解为单个问题,而不是将其中的许多问题分组。这个网站的格式更适合一个问题,一个答案,除非可能有指导说明。

排除文件夹

您可以使用Exclude命令上的Get-ChildItem参数来忽略文件夹。

想象一下,您有一个类似于以下的文件夹结构:

c:\gci | |\a | \a.txt |\b | \b.txt |\c | \c.txt

如果您只想获取c:\gci\ac:\gci\c的内容,可以使用以下命令排除c:\gci\b

Get-ChildItem -Path c:\gci -Exclude b* -Recurse;

请注意,这也将排除以" b"开头的其他项目。例如c:\gci\bcd.txt

进度条

您可以使用Write-Progress命令创建进度条。您必须编写自己的自定义逻辑,以确定报告进度的任务以及进度的百分比。您可以根据复制的字节数与要复制的总字节数,复制的文件数量与文件总数或其他某种指标进行此操作。

这个没有简单的答案。您必须自己执行计算,并在适当的时间致电Write-Progress