-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Fix Rename-Item -Path with wildcard char #7398
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0be2d49
[Feature] Fix Rename-Item -Path with wildcard char
kwkam 70327e0
[Feature] Fix FunctionProvider test
kwkam 61d10dd
fixup! [Feature] Fix FunctionProvider test
kwkam 09b5d78
[Feature] Treat Path as literal
kwkam be59b3d
[Feature] Remove unnecessary finally
kwkam ba96ba3
Revert "[Feature] Remove unnecessary finally"
kwkam a2c29ad
[Feature] Remove try-finally
kwkam 3c9b0a1
Move Remove-Item test to #7404
kwkam 2e0615f
Fix commented items
kwkam 8ac5d9e
[Feature] Add test
kwkam 5a019f1
fixup! Fix commented items
kwkam 4bed5fe
fixup! [Feature] Add test
kwkam b3adcf1
fixup! fixup! [Feature] Add test
kwkam 431f015
LocationGlobber: Fix issue when CWD contains wildcard char
kwkam 3356c91
[Feature] Add test for -LiteralPath
kwkam 68ff0df
Merge branch 'master' into wc-renameitem
kwkam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3519,22 +3519,87 @@ protected override bool ProviderSupportsShouldProcess | |
|
|
||
| #region Command code | ||
|
|
||
| private Collection<PathInfo> GetResolvedPaths(string path) | ||
| { | ||
| Collection<PathInfo> results = null; | ||
| try | ||
| { | ||
| results = SessionState.Path.GetResolvedPSPathFromPSPath(path, CmdletProviderContext); | ||
| } | ||
| catch (PSNotSupportedException notSupported) | ||
| { | ||
| WriteError( | ||
| new ErrorRecord( | ||
| notSupported.ErrorRecord, | ||
| notSupported)); | ||
| } | ||
| catch (DriveNotFoundException driveNotFound) | ||
| { | ||
| WriteError( | ||
| new ErrorRecord( | ||
| driveNotFound.ErrorRecord, | ||
| driveNotFound)); | ||
| } | ||
| catch (ProviderNotFoundException providerNotFound) | ||
| { | ||
| WriteError( | ||
| new ErrorRecord( | ||
| providerNotFound.ErrorRecord, | ||
| providerNotFound)); | ||
| } | ||
| catch (ItemNotFoundException pathNotFound) | ||
| { | ||
| WriteError( | ||
| new ErrorRecord( | ||
| pathNotFound.ErrorRecord, | ||
| pathNotFound)); | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Moves the specified item to the specified destination | ||
| /// </summary> | ||
| protected override void ProcessRecord() | ||
| { | ||
| if (SuppressWildcardExpansion) | ||
| { | ||
| RenameItem(Path, literalPath: true); | ||
| return; | ||
| } | ||
|
|
||
| Collection<PathInfo> resolvedPaths = GetResolvedPaths(Path); | ||
|
|
||
| if (resolvedPaths == null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (resolvedPaths.Count == 1) | ||
| { | ||
| RenameItem(resolvedPaths[0].Path, literalPath: true); | ||
| } | ||
| else | ||
| { | ||
| RenameItem(WildcardPattern.Unescape(Path), literalPath: true); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove the empty finally block. |
||
| } | ||
|
|
||
| private void RenameItem(string path, bool literalPath = false) | ||
| { | ||
| CmdletProviderContext currentContext = CmdletProviderContext; | ||
| currentContext.SuppressWildcardExpansion = literalPath; | ||
|
SteveL-MSFT marked this conversation as resolved.
|
||
|
|
||
| try | ||
| { | ||
| if (!InvokeProvider.Item.Exists(Path, currentContext)) | ||
| if (!InvokeProvider.Item.Exists(path, currentContext)) | ||
| { | ||
| PSInvalidOperationException invalidOperation = | ||
| (PSInvalidOperationException) | ||
| PSTraceSource.NewInvalidOperationException( | ||
| NavigationResources.RenameItemDoesntExist, | ||
| Path); | ||
| path); | ||
|
|
||
| WriteError( | ||
| new ErrorRecord( | ||
|
|
@@ -3580,7 +3645,7 @@ protected override void ProcessRecord() | |
| bool isCurrentLocationOrAncestor = false; | ||
| try | ||
| { | ||
| isCurrentLocationOrAncestor = SessionState.Path.IsCurrentLocationOrAncestor(_path, currentContext); | ||
| isCurrentLocationOrAncestor = SessionState.Path.IsCurrentLocationOrAncestor(path, currentContext); | ||
| } | ||
| catch (PSNotSupportedException notSupported) | ||
| { | ||
|
|
@@ -3621,7 +3686,7 @@ protected override void ProcessRecord() | |
| (PSInvalidOperationException) | ||
| PSTraceSource.NewInvalidOperationException( | ||
| NavigationResources.RenamedItemInUse, | ||
| Path); | ||
| path); | ||
|
|
||
| WriteError( | ||
| new ErrorRecord( | ||
|
|
@@ -3635,12 +3700,12 @@ protected override void ProcessRecord() | |
|
|
||
| currentContext.PassThru = PassThru; | ||
|
|
||
| tracer.WriteLine("Rename {0} to {1}", Path, NewName); | ||
| tracer.WriteLine("Rename {0} to {1}", path, NewName); | ||
|
|
||
| try | ||
| { | ||
| // Now do the rename | ||
| InvokeProvider.Item.Rename(Path, NewName, currentContext); | ||
| InvokeProvider.Item.Rename(path, NewName, currentContext); | ||
| } | ||
| catch (PSNotSupportedException notSupported) | ||
| { | ||
|
|
@@ -3674,7 +3739,7 @@ protected override void ProcessRecord() | |
| pathNotFound)); | ||
| return; | ||
| } | ||
| } // ProcessRecord | ||
| } | ||
| #endregion Command code | ||
|
|
||
| } // RenameItemCommand | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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-catchand we can remove thetry.