Skip to content

Fix Rename-Item -Path with wildcard char - #7398

Merged
Aditya Patwardhan (adityapatwardhan) merged 16 commits into
PowerShell:masterfrom
kwkam:wc-renameitem
Nov 9, 2018
Merged

Aditya Patwardhan (adityapatwardhan) merged 16 commits into
PowerShell:masterfrom
kwkam:wc-renameitem

Conversation

@kwkam

@kwkam kwkam commented Jul 29, 2018 •

Copy link
Copy Markdown
Contributor

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.

@iSazonov Ilya (iSazonov) left a comment

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Collaborator

kwkam Please rebase to fix CI Travis failure.

@iSazonov Ilya (iSazonov) left a comment

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed.

@kwkam

kwkam commented Aug 12, 2018

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov) Should I run git rebase master?

@iSazonov

Copy link
Copy Markdown
Collaborator

Why do you need to rebase?

@kwkam

kwkam commented Aug 12, 2018

Copy link
Copy Markdown
Contributor Author

@iSazonov

Copy link
Copy Markdown
Collaborator

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

@kwkam

kwkam commented Aug 13, 2018

Copy link
Copy Markdown
Contributor Author

Ilya (@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

stale Bot commented Sep 30, 2018

Copy link
Copy Markdown

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
Copy Markdown
Collaborator

Steve Lee (@SteveL-MSFT) do we have interest in the change?

@stale stale Bot removed the Stale label Oct 2, 2018
@SteveL-MSFT

Copy link
Copy Markdown
Member

Ilya (@iSazonov) yes, interested in this change

@iSazonov

Copy link
Copy Markdown
Collaborator

kwkam Please rebase to pass CIs.

@kwkam

kwkam commented Oct 6, 2018

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov) OK, rebased to latest master.

@stale

stale Bot commented Nov 5, 2018

Copy link
Copy Markdown

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
@stale stale Bot removed the Stale label Nov 5, 2018

@SteveL-MSFT Steve Lee (SteveL-MSFT) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@adityapatwardhan
Aditya Patwardhan (adityapatwardhan) merged commit caad7ed into PowerShell:master Nov 9, 2018
@adityapatwardhan

Copy link
Copy Markdown
Member

kwkam Thank you for your contribution!

@kwkam
kwkam deleted the wc-renameitem branch January 11, 2019 15:05
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
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