Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add progress to Copy-Item #18735

Merged
merged 15 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
show both copied and total files
  • Loading branch information
SteveL-MSFT committed Jan 25, 2023
commit f4c8975683eccbdd5f1bddd563d588feb2d82553
32 changes: 20 additions & 12 deletions src/System.Management.Automation/namespaces/FileSystemProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3559,8 +3559,7 @@ private static bool DirectoryInfoHasChildItems(DirectoryInfo directory)
{
if (progressPreference == ActionPreference.Continue)
{
// we use a thread to gather the total files so that copying can start right away
Thread t = new Thread(() =>
Task t = new Task(() =>
{
GetTotalFiles(path, recurse);
});
Expand Down Expand Up @@ -3596,22 +3595,26 @@ private void GetTotalFiles(string path, bool recurse)
var dir = new DirectoryInfo(path);
var enumOptions = new EnumerationOptions();
enumOptions.IgnoreInaccessible = true;
if (recurse)
{
enumOptions.RecurseSubdirectories = true;
}
enumOptions.AttributesToSkip = 0;
enumOptions.RecurseSubdirectories = recurse;

foreach (var file in dir.EnumerateFiles("*", enumOptions))
{
_totalFiles++;
_totalBytes += file.Length;
if (!SessionStateUtilities.MatchesAnyWildcardPattern(file.Name, _excludeMatcher, defaultValue: false))
{
_totalFiles++;
_totalBytes += file.Length;
}
}
}
else
{
var file = new FileInfo(path);
_totalFiles++;
_totalBytes += file.Length;
if (!SessionStateUtilities.MatchesAnyWildcardPattern(file.Name, _excludeMatcher, defaultValue: false))
{
_totalFiles++;
_totalBytes += file.Length;
}
}
}
catch
Expand Down Expand Up @@ -3936,10 +3939,15 @@ private void CopyFileInfoItem(FileInfo file, string destinationPath, bool force,
double speed = (double)(_copiedBytes / 1024 / 1024) / _copyStopwatch.Elapsed.TotalSeconds;
var progress = new ProgressRecord(
COPY_FILE_ACTIVITY_ID,
StringUtil.Format(FileSystemProviderStrings.CopyingLocalFileActivity, _totalFiles - _copiedFiles),
StringUtil.Format(FileSystemProviderStrings.CopyingLocalFileActivity, _copiedFiles, _totalFiles),
StringUtil.Format(FileSystemProviderStrings.CopyingLocalBytesStatus, Utils.DisplayHumanReadableFileSize(_copiedBytes), Utils.DisplayHumanReadableFileSize(_totalBytes), speed)
);
progress.PercentComplete = (int)((_copiedBytes * 100) / _totalBytes);
var percentComplete = (int)((_copiedBytes * 100) / _totalBytes);
if (percentComplete > 100)
{
percentComplete = 100;
}
SteveL-MSFT marked this conversation as resolved.
Show resolved Hide resolved
progress.PercentComplete = percentComplete;
progress.RecordType = ProgressRecordType.Processing;
WriteProgress(progress);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@
<value>The target and path cannot be the same.</value>
</data>
<data name="CopyingLocalFileActivity" xml:space="preserve">
<value>Copying {0} files</value>
<value>Copied {0} of {1} files</value>
</data>
<data name="CopyingLocalBytesStatus" xml:space="preserve">
<value>{0} of {1} ({2:0.0} MB/s)</value>
Expand Down