Method PSMoveFrom
- Namespace
- IOInfoExtensions.PowerShell
- Assembly
- IOInfoExtensions.PowerShell.dll
PSMoveFrom(PSObject, PSObject, bool)
PowerShell wrapper for the FileInfo.MoveFrom method.
public static object PSMoveFrom(PSObject destination, PSObject source, bool overwrite = false)
Remarks
Moves the source file to the calling destination. If the destination exists and overwrite is false, an exception will be thrown.
System.IO.FileInfo
has a MoveTo method, but it changes the source
FileInfo
object information to reference the destination file. MoveFrom
will allow you to call the move from the destination, leaving the source object still
referencing where the file was.
Parameters
destination
PSObject- The calling
FileInfo
object. source
PSObject- The source
FileInfo
object to be moved. overwrite
bool- Indicates if the destination file should be overwritten if it exists.
Returns
- object
- N/A
Examples
Example 1
Moves and renames a file.
var sourceFile = new FileInfo("C:\\Demo\\ChildFile1.txt");
var destinationFile = new FileInfo("C:\\DemoCopy\\ChildDir3\\ChildFile3.txt");
Console.WriteLine("File Information:");
Console.WriteLine(GetFileTable(new[] { sourceFile, destinationFile }, includeDirectory: true));
Console.WriteLine("Moving file...");
destinationFile.MoveFrom(sourceFile);
Console.WriteLine("File Information after move:");
Console.WriteLine(GetFileTable(new[] { sourceFile, destinationFile }, includeDirectory: true));
/*
File Information:
Directory Name DirectoryExists Exists
--------- ---- --------------- ------
C:\Demo ChildFile1.txt True True
C:\DemoCopy\ChildDir3 ChildFile3.txt False False
Moving file...
File Information after move:
Directory Name DirectoryExists Exists
--------- ---- --------------- ------
C:\Demo ChildFile1.txt True False
C:\DemoCopy\ChildDir3 ChildFile3.txt True True
*/
$sourceFile = New-Object System.IO.FileInfo 'C:\Demo\ChildFile1.txt'
$destinationFile = New-Object System.IO.FileInfo 'C:\DemoCopy\ChildDir3\ChildFile3.txt'
Write-Host "File Information:"
Write-Host (Get-FileTable -Files $sourceFile, $destinationFile -IncludeDirectory)
Write-Host "Moving file..."
$destinationFile.MoveFrom($sourceFile)
Write-Host "File Information after move:"
Write-Host (Get-FileTable -Files $sourceFile, $destinationFile -IncludeDirectory)
<#
Output:
File Information:
Directory Name DirectoryExists Exists
--------- ---- --------------- ------
C:\Demo ChildFile1.txt True True
C:\DemoCopy\ChildDir3 ChildFile3.txt False False
Moving file...
File Information after move:
Directory Name DirectoryExists Exists
--------- ---- --------------- ------
C:\Demo ChildFile1.txt True False
C:\DemoCopy\ChildDir3 ChildFile3.txt True True
#>
Example 2
Moves and overwrites a file.
var sourceFile = new FileInfo("C:\\Demo\\ChildFile1.txt");
var destinationFile = new FileInfo("C:\\DemoCopy\\ChildDir3\\ChildFile3.txt");
Console.WriteLine("File Information:");
Console.WriteLine(GetFileTable(new[] { sourceFile, destinationFile }, includeDirectory: true, includeHash: true));
Console.WriteLine("Moving file...");
destinationFile.MoveFrom(sourceFile, true);
Console.WriteLine("File Information after move:");
Console.WriteLine(GetFileTable(new[] { sourceFile, destinationFile }, includeDirectory: true, includeHash: true));
/*
Output:
File Information:
Directory Name DirectoryExists Exists Hash
--------- ---- --------------- ------ ----
C:\Demo ChildFile1.txt True True AE7C049181E275A7AAF1E365339DBE888309C7BE
C:\DemoCopy\ChildDir3 ChildFile3.txt True True 6633A8AA22899FBB94984085B76796D33A890836
Moving file...
File Information after move:
Directory Name DirectoryExists Exists Hash
--------- ---- --------------- ------ ----
C:\Demo ChildFile1.txt True False
C:\DemoCopy\ChildDir3 ChildFile3.txt True True AE7C049181E275A7AAF1E365339DBE888309C7BE
*/
$sourceFile = New-Object System.IO.FileInfo 'C:\Demo\ChildFile1.txt'
$destinationFile = New-Object System.IO.FileInfo 'C:\DemoCopy\ChildDir3\ChildFile3.txt'
Write-Host "File Information:"
Write-Host (Get-FileTable -Files $sourceFile, $destinationFile -IncludeDirectory -IncludeHash)
Write-Host "Moving file..."
$destinationFile.MoveFrom($sourceFile, $true)
Write-Host "File Information after move:"
Write-Host (Get-FileTable -Files $sourceFile, $destinationFile -IncludeDirectory -IncludeHash)
<#
Output:
File Information:
Directory Name DirectoryExists Exists Hash
--------- ---- --------------- ------ ----
C:\Demo ChildFile1.txt True True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F
C:\DemoCopy\ChildDir3 ChildFile3.txt True True 456012073991C95AF2F3628D2C28595A85E80653
Moving file...
File Information after move:
Directory Name DirectoryExists Exists Hash
--------- ---- --------------- ------ ----
C:\Demo ChildFile1.txt True False
C:\DemoCopy\ChildDir3 ChildFile3.txt True True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F
#>
Example 3
Throws an error because the destination already exists.
var sourceFile = new FileInfo("C:\\Demo\\ChildFile1.txt");
var destinationFile = new FileInfo("C:\\DemoCopy\\ChildDir3\\ChildFile3.txt");
Console.WriteLine("File Information:");
Console.WriteLine(GetFileTable(new[] { sourceFile, destinationFile }, includeDirectory: true, includeHash: true));
Console.WriteLine("Moving file...");
try
{
destinationFile.MoveFrom(sourceFile);
}
catch (Exception ex)
{
var stringArgs = new[] {
ex.GetType().ToString(),
ex.Message,
ex.StackTrace.Substring(0, ex.StackTrace.IndexOf(Environment.NewLine))
};
Console.Error.WriteLine(string.Format("\nError: {0}: {1}\n{2}\n", stringArgs));
}
Console.WriteLine("File Information after move:");
Console.WriteLine(GetFileTable(new[] { sourceFile, destinationFile }, includeDirectory: true, includeHash: true));
/*
Output:
File Information:
Directory Name DirectoryExists Exists Hash
--------- ---- --------------- ------ ----
C:\Demo ChildFile1.txt True True AE7C049181E275A7AAF1E365339DBE888309C7BE
C:\DemoCopy\ChildDir3 ChildFile3.txt True True 6633A8AA22899FBB94984085B76796D33A890836
Moving file...
Error: System.IO.IOException: The destination file 'C:\DemoCopy\ChildDir3\ChildFile3.txt' already exists and overwrite is not set to true.
at IOInfoExtensions.FileInfoExtensions.MoveFrom(FileInfo destination, FileInfo source, Boolean overwrite)
File Information after move:
Directory Name DirectoryExists Exists Hash
--------- ---- --------------- ------ ----
C:\Demo ChildFile1.txt True True AE7C049181E275A7AAF1E365339DBE888309C7BE
C:\DemoCopy\ChildDir3 ChildFile3.txt True True 6633A8AA22899FBB94984085B76796D33A890836
*/
$sourceFile = New-Object System.IO.FileInfo 'C:\Demo\ChildFile1.txt'
$destinationFile = New-Object System.IO.FileInfo 'C:\DemoCopy\ChildDir3\ChildFile3.txt'
Write-Host "File Information:"
Write-Host (Get-FileTable -Files $sourceFile, $destinationFile -IncludeDirectory -IncludeHash)
Write-Host "Moving file..."
try
{
$destinationFile.MoveFrom($sourceFile)
}
catch
{
$stringArgs = @(
$_.Exception.GetType().ToString(),
$_.Exception.Message,
$_.Exception.StackTrace.Substring(0, $_.Exception.StackTrace.IndexOf([Environment]::NewLine))
)
Write-Host ("`nError: {0}: {1}`n{2}`n" -f $stringArgs)
$error.Clear()
}
Write-Host "File Information after move:"
Write-Host (Get-FileTable -Files $sourceFile, $destinationFile -IncludeDirectory -IncludeHash)
<#
Output:
File Information:
Directory Name DirectoryExists Exists Hash
--------- ---- --------------- ------ ----
C:\Demo ChildFile1.txt True True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F
C:\DemoCopy\ChildDir3 ChildFile3.txt True True 456012073991C95AF2F3628D2C28595A85E80653
Moving file...
Error: System.IO.IOException: The destination file 'C:\DemoCopy\ChildDir3\ChildFile3.txt' already exists and overwrite is not set to true.
at IOInfoExtensions.FileInfoExtensions.MoveFrom(FileInfo destination, FileInfo source, Boolean overwrite)
File Information after move:
Directory Name DirectoryExists Exists Hash
--------- ---- --------------- ------ ----
C:\Demo ChildFile1.txt True True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F
C:\DemoCopy\ChildDir3 ChildFile3.txt True True 456012073991C95AF2F3628D2C28595A85E80653
#>
Exceptions
- FileNotFoundException
- If the source file does not exist.
- IOException
- If the destination file exists but overwrite is not set to true.
- PSInvalidOperationException