Skip to content

Commit

Permalink
Added progress bar for Remove-Item cmdlet (#20778)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArmaanMcleod committed May 13, 2024
1 parent e05542b commit a284fd4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/System.Management.Automation/namespaces/FileSystemProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public sealed partial class FileSystemProvider : NavigationCmdletProvider,
private const int FILETRANSFERSIZE = 4 * 1024 * 1024;

private const int COPY_FILE_ACTIVITY_ID = 0;
private const int REMOVE_FILE_ACTIVITY_ID = 0;

// The name of the key in an exception's Data dictionary when attempting
// to copy an item onto itself.
Expand Down Expand Up @@ -2875,6 +2876,19 @@ protected override void RemoveItem(string path, bool recurse)
return;
}

if (Context != null
&& Context.ExecutionContext.SessionState.PSVariable.Get(SpecialVariables.ProgressPreferenceVarPath.UserPath).Value is ActionPreference progressPreference
&& progressPreference == ActionPreference.Continue)
{
{
Task.Run(() =>
{
GetTotalFiles(path, recurse);
});
_removeStopwatch.Start();
}
}

#if UNIX
if (iscontainer)
{
Expand Down Expand Up @@ -2938,6 +2952,16 @@ protected override void RemoveItem(string path, bool recurse)
RemoveFileInfoItem((FileInfo)fsinfo, Force);
}
}

if (Stopping || _removedFiles == _totalFiles)
{
_removeStopwatch.Stop();
var progress = new ProgressRecord(REMOVE_FILE_ACTIVITY_ID, " ", " ")
{
RecordType = ProgressRecordType.Completed
};
WriteProgress(progress);
}
#endif
}
catch (IOException exception)
Expand Down Expand Up @@ -3072,6 +3096,8 @@ void WriteErrorHelper(Exception exception)

if (file != null)
{
long fileBytesSize = file.Length;

if (recurse)
{
// When recurse is specified we need to confirm each
Expand All @@ -3084,6 +3110,22 @@ void WriteErrorHelper(Exception exception)
// subitems without confirming with the user.
RemoveFileSystemItem(file, force);
}

if (_totalFiles > 0)
{
_removedFiles++;
_removedBytes += fileBytesSize;
double speed = _removedBytes / 1024 / 1024 / _removeStopwatch.Elapsed.TotalSeconds;
var progress = new ProgressRecord(
REMOVE_FILE_ACTIVITY_ID,
StringUtil.Format(FileSystemProviderStrings.RemovingLocalFileActivity, _removedFiles, _totalFiles),
StringUtil.Format(FileSystemProviderStrings.RemovingLocalBytesStatus, Utils.DisplayHumanReadableFileSize(_removedBytes), Utils.DisplayHumanReadableFileSize(_totalBytes), speed)
);
var percentComplete = (int)Math.Min(_removedBytes * 100 / _totalBytes, 100);
progress.PercentComplete = percentComplete;
progress.RecordType = ProgressRecordType.Processing;
WriteProgress(progress);
}
}
}

Expand Down Expand Up @@ -4899,6 +4941,10 @@ private bool PathIsReservedDeviceName(string destinationPath, string errorId)
private long _copiedBytes;
private readonly Stopwatch _copyStopwatch = new Stopwatch();

private long _removedBytes;
private long _removedFiles;
private readonly Stopwatch _removeStopwatch = new();

#endregion CopyItem

#endregion ContainerCmdletProvider members
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,12 @@
<data name="CopyingLocalBytesStatus" xml:space="preserve">
<value>{0} of {1} ({2:0.0} MB/s)</value>
</data>
<data name="RemovingLocalFileActivity" xml:space="preserve">
<value>Removed {0} of {1} files</value>
</data>
<data name="RemovingLocalBytesStatus" xml:space="preserve">
<value>{0} of {1} ({2:0.0} MB/s)</value>
</data>
<data name="JunctionAbsolutePath" xml:space="preserve">
<value>Creating a junction requires an absolute path for the target.</value>
</data>
Expand Down

0 comments on commit a284fd4

Please sign in to comment.