Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -3295,7 +3295,7 @@ protected override void ProcessRecord()
{
if (base.SuppressWildcardExpansion)
{
MoveItem(path);
MoveItem(path, literalPath: true);
}
else
{
Expand All @@ -3304,173 +3304,165 @@ protected override void ProcessRecord()
foreach (PathInfo resolvedPathInfo in resolvedPaths)
{
string resolvedPath = resolvedPathInfo.Path;
MoveItem(resolvedPath);
MoveItem(resolvedPath, literalPath: true);
}
}
}
} // ProcessRecord
}

private void MoveItem(string path)
private void MoveItem(string path, bool literalPath = false)
{
CmdletProviderContext currentContext = CmdletProviderContext;
currentContext.SuppressWildcardExpansion = literalPath;

do
try
{
try
{
string escapedPath = path;
if (!base.SuppressWildcardExpansion) { escapedPath = WildcardPattern.Escape(path); }
if (!InvokeProvider.Item.Exists(escapedPath, currentContext))
{
PSInvalidOperationException invalidOperation =
(PSInvalidOperationException)
PSTraceSource.NewInvalidOperationException(
NavigationResources.MoveItemDoesntExist,
path);

WriteError(
new ErrorRecord(
invalidOperation.ErrorRecord,
invalidOperation));
continue;
}
}
catch (PSNotSupportedException notSupported)
{
WriteError(
new ErrorRecord(
notSupported.ErrorRecord,
notSupported));
continue;
}
catch (DriveNotFoundException driveNotFound)
{
WriteError(
new ErrorRecord(
driveNotFound.ErrorRecord,
driveNotFound));
continue;
}
catch (ProviderNotFoundException providerNotFound)
{
WriteError(
new ErrorRecord(
providerNotFound.ErrorRecord,
providerNotFound));
continue;
}
catch (ItemNotFoundException pathNotFound)
{
WriteError(
new ErrorRecord(
pathNotFound.ErrorRecord,
pathNotFound));
continue;
}

// See if the item to be moved is in use.
bool isCurrentLocationOrAncestor = false;
try
{
isCurrentLocationOrAncestor = SessionState.Path.IsCurrentLocationOrAncestor(path, currentContext);
}
catch (PSNotSupportedException notSupported)
{
WriteError(
new ErrorRecord(
notSupported.ErrorRecord,
notSupported));
continue;
}
catch (DriveNotFoundException driveNotFound)
{
WriteError(
new ErrorRecord(
driveNotFound.ErrorRecord,
driveNotFound));
continue;
}
catch (ProviderNotFoundException providerNotFound)
{
WriteError(
new ErrorRecord(
providerNotFound.ErrorRecord,
providerNotFound));
continue;
}
catch (ItemNotFoundException pathNotFound)
{
WriteError(
new ErrorRecord(
pathNotFound.ErrorRecord,
pathNotFound));
continue;
}

if (isCurrentLocationOrAncestor)
if (!InvokeProvider.Item.Exists(path, currentContext))
{
PSInvalidOperationException invalidOperation =
(PSInvalidOperationException)
PSTraceSource.NewInvalidOperationException(
NavigationResources.MoveItemInUse,
NavigationResources.MoveItemDoesntExist,
path);

WriteError(
new ErrorRecord(
invalidOperation.ErrorRecord,
invalidOperation));
continue;
return;
}
}
catch (PSNotSupportedException notSupported)
{
WriteError(
new ErrorRecord(
notSupported.ErrorRecord,
notSupported));
return;
}
catch (DriveNotFoundException driveNotFound)
{
WriteError(
new ErrorRecord(
driveNotFound.ErrorRecord,
driveNotFound));
return;
}
catch (ProviderNotFoundException providerNotFound)
{
WriteError(
new ErrorRecord(
providerNotFound.ErrorRecord,
providerNotFound));
return;
}
catch (ItemNotFoundException pathNotFound)
{
WriteError(
new ErrorRecord(
pathNotFound.ErrorRecord,
pathNotFound));
return;
}

// Default to the CmdletProviderContext that will direct output to
// the pipeline.
// See if the item to be moved is in use.
bool isCurrentLocationOrAncestor = false;
try
{
isCurrentLocationOrAncestor = SessionState.Path.IsCurrentLocationOrAncestor(path, currentContext);
}
catch (PSNotSupportedException notSupported)
{
WriteError(
new ErrorRecord(
notSupported.ErrorRecord,
notSupported));
return;
}
catch (DriveNotFoundException driveNotFound)
{
WriteError(
new ErrorRecord(
driveNotFound.ErrorRecord,
driveNotFound));
return;
}
catch (ProviderNotFoundException providerNotFound)
{
WriteError(
new ErrorRecord(
providerNotFound.ErrorRecord,
providerNotFound));
return;
}
catch (ItemNotFoundException pathNotFound)
{
WriteError(
new ErrorRecord(
pathNotFound.ErrorRecord,
pathNotFound));
return;
}

CmdletProviderContext currentCommandContext = currentContext;
currentCommandContext.PassThru = PassThru;
if (isCurrentLocationOrAncestor)
{
PSInvalidOperationException invalidOperation =
(PSInvalidOperationException)
PSTraceSource.NewInvalidOperationException(
NavigationResources.MoveItemInUse,
path);

tracer.WriteLine("Moving {0} to {1}", path, Destination);
WriteError(
new ErrorRecord(
invalidOperation.ErrorRecord,
invalidOperation));
return;
}

try
{
// Now do the move
string escapedPath = path;
if (!base.SuppressWildcardExpansion) { escapedPath = WildcardPattern.Escape(path); }
InvokeProvider.Item.Move(escapedPath, Destination, currentCommandContext);
}
catch (PSNotSupportedException notSupported)
{
WriteError(
new ErrorRecord(
notSupported.ErrorRecord,
notSupported));
continue;
}
catch (DriveNotFoundException driveNotFound)
{
WriteError(
new ErrorRecord(
driveNotFound.ErrorRecord,
driveNotFound));
continue;
}
catch (ProviderNotFoundException providerNotFound)
{
WriteError(
new ErrorRecord(
providerNotFound.ErrorRecord,
providerNotFound));
continue;
}
catch (ItemNotFoundException pathNotFound)
{
WriteError(
new ErrorRecord(
pathNotFound.ErrorRecord,
pathNotFound));
continue;
}
// Default to the CmdletProviderContext that will direct output to
// the pipeline.

currentContext.PassThru = PassThru;

tracer.WriteLine("Moving {0} to {1}", path, Destination);

try
{
// Now do the move
InvokeProvider.Item.Move(path, Destination, currentContext);
}
catch (PSNotSupportedException notSupported)
{
WriteError(
new ErrorRecord(
notSupported.ErrorRecord,
notSupported));
return;
}
catch (DriveNotFoundException driveNotFound)
{
WriteError(
new ErrorRecord(
driveNotFound.ErrorRecord,
driveNotFound));
return;
}
catch (ProviderNotFoundException providerNotFound)
{
WriteError(
new ErrorRecord(
providerNotFound.ErrorRecord,
providerNotFound));
return;
}
catch (ItemNotFoundException pathNotFound)
{
WriteError(
new ErrorRecord(
pathNotFound.ErrorRecord,
pathNotFound));
return;
}
while (false);
}
#endregion Command code

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ Describe "Move-Item tests" -Tag "CI" {
Setup -f originalfile.txt -content "This is content"
$source = "$TESTDRIVE/originalfile.txt"
$target = "$TESTDRIVE/ItemWhichHasBeenMoved.txt"
Setup -f [orig-file].txt -content "This is not content"
$sourceSp = "$TestDrive/``[orig-file``].txt"
$targetSpName = "$TestDrive/ItemWhichHasBeen[Moved].txt"
$targetSp = "$TestDrive/ItemWhichHasBeen``[Moved``].txt"
}
It "Move-Item will move a file" {
Move-Item $source $target
$source | Should -Not -Exist
$target | Should -Exist
"$target" | Should -FileContentMatchExactly "This is content"
}
It "Move-Item will move a file when path contains special char" {
Move-Item $sourceSp $targetSpName
$sourceSp | Should -Not -Exist
$targetSp | Should -Exist
$targetSp | Should -FileContentMatchExactly "This is not content"
}

Context "Move-Item with filters" {
BeforeAll {
Expand Down