Skip to content

Fix Rename-Item -Path with wildcard char#7398

Merged
adityapatwardhan merged 16 commits intoPowerShell:masterfrom
kwkam:wc-renameitem
Nov 9, 2018
Merged

Fix Rename-Item -Path with wildcard char#7398
adityapatwardhan merged 16 commits intoPowerShell:masterfrom
kwkam:wc-renameitem

Conversation

@kwkam
Copy link
Contributor

@kwkam kwkam commented Jul 29, 2018

PR Summary

*Microsoft.PowerShell.Commands.Management/commands/management/Navigation.cs:
Unescape non-literal, non-glob path in ProccessRecord and
set the context.SuppressWildcardExpansion in RenameItem.
This solve the issue where Rename-Item complains -Path does not exist
when both -Path and CWD contains special characters.

*System.Management.Automation/namespaces/LocationGlobber.cs:
Do not escape CWD when LiteralPath is used.
This causes issues when both -LiteralPath and CWD contains special characters. For example,

Rename-Item -LiteralPath [test]file.txt [test]file2.txt

will complain that [test]file.txt does not exist when CWD contains special characters.

PR Checklist

Set the context.SuppressWildcardExpansion in MoveItem instead of
escaping the path every time. Also solve the issue where Move-Item
complains -Path wildcard pattern is not valid when -Path contains
special characters that forms an invalid pattern.
Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave a comment

/// </summary>
protected override void ProcessRecord()
{
do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we use cycle here? We have single input string.
I would try to make things easier - just try to rename in try-catch.

<value>Cannot move item because the item at '{0}' is in use.</value>
</data>
<data name="RenameMultipleItemError" xml:space="preserve">
<value>Cannot rename item because the path '{0}' resolved to multiple items. Only one item can be renamed at a time.</value>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes no sense to tell the user that we have tried to resolve wildcards - in fact, we consider the Path parameter as literal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we would add support for renaming multiple items like the ren command in cmd?
eg. Rename-Item -Path '*.txt' -NewExtension '.bak'
or, Rename-Item -Path '*.txt' -Format 'renamed_{0}'

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the enhancement is out of the PR and we should discuss it in a new tracking Issue (please open).

}
}
finally {
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove the empty finally block.

@kwkam kwkam changed the title [Feature] Fix Rename-Item -Path with wildcard char Fix Rename-Item -Path with wildcard char Jul 31, 2018
@kwkam kwkam changed the title Fix Rename-Item -Path with wildcard char Fix Rename-Item -Path with wildcard char Jul 31, 2018
protected override void ProcessRecord()
{
try
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seem all used methods use try-catch and we can remove the try.

@iSazonov
Copy link
Collaborator

iSazonov commented Aug 2, 2018

@kwkam Please rebase to fix CI Travis failure.

Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please look tests - I don't see test related the PR.


private Collection<PathInfo> GetResolvedPaths(string path)
{
Collection<PathInfo> results = new Collection<PathInfo>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is extra allocation. I think we should remove this and check null in calling code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated test.

break;

default:
RenameItem(Path, literalPath: true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really working?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect that we unescape path, check existance (or try rename), if fail then resolve globs (write an error if cpunt > 1) and try rename again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe -Path should try to resolve globs first, since it's the expected behaviour, and users can escape the wildcard character, for example

Rename-Item -Path '`[A-B`]' -NewName 'A-B'

will prevent globbing.

/// </summary>
protected override void ProcessRecord()
{
if (base.SuppressWildcardExpansion)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need base prefix?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

@kwkam
Copy link
Contributor Author

kwkam commented Aug 12, 2018

@iSazonov Should I run git rebase master?

@iSazonov
Copy link
Collaborator

Why do you need to rebase?

@kwkam
Copy link
Contributor Author

kwkam commented Aug 12, 2018

@iSazonov #7398 (comment)

@iSazonov
Copy link
Collaborator

@kwkam Ok, I understand. You can rebase to temporary skip broken tests.

@kwkam
Copy link
Contributor Author

kwkam commented Aug 13, 2018

@iSazonov I see. I would not rebase if it is not necessary.

kwkam added 2 commits August 13, 2018 21:23
Do not escape CWD when LiteralPath is used.
This causes issues when both -LiteralPath and CWD contains special characters.
@stale
Copy link

stale bot commented Sep 30, 2018

This PR has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs within 10 days.
Thank you for your contributions.
Community members are welcome to grab these works.

@stale stale bot added the Stale label Sep 30, 2018
@iSazonov
Copy link
Collaborator

iSazonov commented Oct 2, 2018

@SteveL-MSFT do we have interest in the change?

@stale stale bot removed the Stale label Oct 2, 2018
@SteveL-MSFT
Copy link
Member

@iSazonov yes, interested in this change

@iSazonov
Copy link
Collaborator

iSazonov commented Oct 5, 2018

@kwkam Please rebase to pass CIs.

@kwkam
Copy link
Contributor Author

kwkam commented Oct 6, 2018

@iSazonov OK, rebased to latest master.

@stale
Copy link

stale bot commented Nov 5, 2018

This PR has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs within 10 days.
Thank you for your contributions.
Community members are welcome to grab these works.

@stale stale bot added the Stale label Nov 5, 2018
@iSazonov iSazonov requested a review from SteveL-MSFT November 5, 2018 10:48
@stale stale bot removed the Stale label Nov 5, 2018
Copy link
Member

@SteveL-MSFT SteveL-MSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@adityapatwardhan adityapatwardhan merged commit caad7ed into PowerShell:master Nov 9, 2018
@adityapatwardhan
Copy link
Member

@kwkam Thank you for your contribution!

@kwkam kwkam deleted the wc-renameitem branch January 11, 2019 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants