about_IOInfoExtensions.PowerShell_CopyContentTo.help.txt
TOPIC
about_IOInfoExtensions.PowerShell_CopyContentTo SHORT DESCRIPTION Copies the content of the calling [DirectoryInfo] object to the destination directory. SYNTAX CopyContentTo(System.IO.DirectoryInfo destination, [bool copyEmptyDirectories = False], [bool overwrite = False], [bool cleanTarget = False]) LONG DESCRIPTION This method copies all the files and directories within the calling [DirectoryInfo] object to the specified destination directory. PARAMETERS destination <System.IO.DirectoryInfo> The directory to copy all the contents to. Required: True Default Value: Accepts Wildcard Characters: False copyEmptyDirectories <System.Boolean> If set to true empty child directories will be created in the destitnation. Required: False Default Value: False Accepts Wildcard Characters: False overwrite <System.Boolean> Overwrite any conflicting files at the destination. Required: False Default Value: False Accepts Wildcard Characters: False cleanTarget <System.Boolean> Delete all contents of the destination directory before copying. Required: False Default Value: False Accepts Wildcard Characters: False OUTPUTS N/A EXAMPLES --------------------- Example 1 --------------------- Copy files and their directory structures. $source = New-Object System.IO.DirectoryInfo 'C:\Demo' $destination = New-Object System.IO.DirectoryInfo 'C:\DemoCopy' Write-Host "Source Children:" $source.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName | ForEach-Object { Write-Host "`t$_" } Write-Host "Destination Exists:`n`t$($destination.Exists)" Write-Host "Copying content..." $source.CopyContentTo($destination) Write-Host "Destination Exists after copy:`n`t$($destination.Exists)" Write-Host "Destination Children:" $destination.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName | ForEach-Object { Write-Host "`t$_" } <# Output: Source Children: C:\Demo\ChildDir1 C:\Demo\ChildDir2 C:\Demo\ChildFile1.txt C:\Demo\ChildFile2.txt C:\Demo\ChildDir2\ChildFile1.txt C:\Demo\ChildDir2\ChildFile2.txt Destination Exists: False Copying content... Destination Exists after copy: True Destination Children: C:\DemoCopy\ChildDir2 C:\DemoCopy\ChildFile1.txt C:\DemoCopy\ChildFile2.txt C:\DemoCopy\ChildDir2\ChildFile1.txt C:\DemoCopy\ChildDir2\ChildFile2.txt #> This copies all the files and directories within the source directory to the destination directory but does not copy empty directories. Notice: - The empty directory "ChildDir1" is not copied to the destination. --------------------- Example 2 --------------------- Copies all items, including empty directories. $source = New-Object System.IO.DirectoryInfo 'C:\Demo' $destination = New-Object System.IO.DirectoryInfo 'C:\DemoCopy' Write-Host "Source Children:" $source.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName | ForEach-Object { Write-Host "`t$_" } Write-Host "Destination Exists:`n`t$($destination.Exists)" Write-Host "Copying content..." $source.CopyContentTo($destination, $true) Write-Host "Destination Exists after copy:`n`t$($destination.Exists)" Write-Host "Destination Children:" $destination.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName | ForEach-Object { Write-Host "`t$_" } <# Output: Source Children: C:\Demo\ChildDir1 C:\Demo\ChildDir2 C:\Demo\ChildFile1.txt C:\Demo\ChildFile2.txt C:\Demo\ChildDir2\ChildFile1.txt C:\Demo\ChildDir2\ChildFile2.txt Destination Exists: False Copying content... Destination Exists after copy: True Destination Children: C:\DemoCopy\ChildDir1 C:\DemoCopy\ChildDir2 C:\DemoCopy\ChildFile1.txt C:\DemoCopy\ChildFile2.txt C:\DemoCopy\ChildDir2\ChildFile1.txt C:\DemoCopy\ChildDir2\ChildFile2.txt #> This copies all the files and directories within the source directory to the destination directory. Notice: - The empty directory "ChildDir1" is copied to the destination. --------------------- Example 3 --------------------- Copy files and their directory structures and overwrites any pre-existing files. $source = New-Object System.IO.DirectoryInfo 'C:\Demo' $destination = New-Object System.IO.DirectoryInfo 'C:\DemoCopy' Write-Host "Source Children:" $source.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName | ForEach-Object { Write-Host "`t$_" } Write-Host "Destination Children:" $destination.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName | ForEach-Object { Write-Host "`t$_" } Write-Host "File Information:" Get-FileTable -Files ($source.GetFiles() + $destination.GetFiles()) -IncludeHash Write-Host "Copying content..." $source.CopyContentTo($destination, $false, $true) Write-Host "Destination Children:" $destination.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName | ForEach-Object { Write-Host "`t$_" } Write-Host "File Information after copy:" Get-FileTable -Files ($source.GetFiles() + $destination.GetFiles()) -IncludeHash <# Source Children: C:\Demo\ChildDir1 C:\Demo\ChildDir2 C:\Demo\ChildFile1.txt C:\Demo\ChildFile2.txt C:\Demo\ChildDir2\ChildFile1.txt C:\Demo\ChildDir2\ChildFile2.txt Destination Children: C:\DemoCopy\ChildFile1.txt C:\DemoCopy\ChildFile2.txt C:\DemoCopy\ChildFile3.txt File Information: FullName Exists Hash -------- ------ ---- C:\Demo\ChildFile1.txt True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F C:\Demo\ChildFile2.txt True D7BB9BC327821EBD985C30F09F2F5B50B5418C79 C:\DemoCopy\ChildFile1.txt True 41AC012FA1E8DA02511EEFBA8F5A3B4F0BBE334A C:\DemoCopy\ChildFile2.txt True ABB5D4DBD1812F92CF357CA7C0652ED66712232D C:\DemoCopy\ChildFile3.txt True 24164ECDA682E2F8ABEB208672D868A1BA3BCC0E Copying content... Destination Children: C:\DemoCopy\ChildDir2 C:\DemoCopy\ChildFile1.txt C:\DemoCopy\ChildFile2.txt C:\DemoCopy\ChildFile3.txt C:\DemoCopy\ChildDir2\ChildFile1.txt C:\DemoCopy\ChildDir2\ChildFile2.txt File Information after copy: FullName Exists Hash -------- ------ ---- C:\Demo\ChildFile1.txt True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F C:\Demo\ChildFile2.txt True D7BB9BC327821EBD985C30F09F2F5B50B5418C79 C:\DemoCopy\ChildFile1.txt True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F C:\DemoCopy\ChildFile2.txt True D7BB9BC327821EBD985C30F09F2F5B50B5418C79 C:\DemoCopy\ChildFile3.txt True 24164ECDA682E2F8ABEB208672D868A1BA3BCC0E #> This copies all the files and directories within the source directory to the destination directory but does not copy empty directories. It will also forcibly overwrite any files that already exist at the destination. Notice: - The empty directory "ChildDir1" is not copied to the destination. - The hashes of "DemoCopy\ChildFile1.txt" and "DemoCopy\ChildFile2.txt" now match those from "Demo" as they were overwritten. --------------------- Example 4 --------------------- Removes all contents of the destination and copies files and their directory structures. $source = New-Object System.IO.DirectoryInfo 'C:\Demo' $destination = New-Object System.IO.DirectoryInfo 'C:\DemoCopy' Write-Host "Source Children:" $source.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName | ForEach-Object { Write-Host "`t$_" } Write-Host "Destination Children:" $destination.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName | ForEach-Object { Write-Host "`t$_" } Write-Host "File Information:" Get-FileTable -Files ($source.GetFiles() + $destination.GetFiles()) -IncludeHash Write-Host "Copying content..." $source.CopyContentTo($destination, $false, $false, $true) Write-Host "Destination Children:" $destination.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName | ForEach-Object { Write-Host "`t$_" } Write-Host "File Information after copy:" Get-FileTable -Files ($source.GetFiles() + $destination.GetFiles()) -IncludeHash Remove-DemoDirectory -RootPath 'C:\Demo' Remove-DemoDirectory -RootPath 'C:\DemoCopy' <# Source Children: C:\Demo\ChildDir1 C:\Demo\ChildDir2 C:\Demo\ChildFile1.txt C:\Demo\ChildFile2.txt C:\Demo\ChildDir2\ChildFile1.txt C:\Demo\ChildDir2\ChildFile2.txt Destination Children: C:\DemoCopy\ChildFile1.txt C:\DemoCopy\ChildFile2.txt C:\DemoCopy\ChildFile3.txt File Information: FullName Exists Hash -------- ------ ---- C:\Demo\ChildFile1.txt True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F C:\Demo\ChildFile2.txt True D7BB9BC327821EBD985C30F09F2F5B50B5418C79 C:\DemoCopy\ChildFile1.txt True 41AC012FA1E8DA02511EEFBA8F5A3B4F0BBE334A C:\DemoCopy\ChildFile2.txt True ABB5D4DBD1812F92CF357CA7C0652ED66712232D C:\DemoCopy\ChildFile3.txt True 24164ECDA682E2F8ABEB208672D868A1BA3BCC0E Copying content... Destination Children: C:\DemoCopy\ChildDir2 C:\DemoCopy\ChildFile1.txt C:\DemoCopy\ChildFile2.txt C:\DemoCopy\ChildDir2\ChildFile1.txt C:\DemoCopy\ChildDir2\ChildFile2.txt File Information after copy: FullName Exists Hash -------- ------ ---- C:\Demo\ChildFile1.txt True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F C:\Demo\ChildFile2.txt True D7BB9BC327821EBD985C30F09F2F5B50B5418C79 C:\DemoCopy\ChildFile1.txt True 373FEB01CBFDCB5121502AFFD3DB5504DABACC8F C:\DemoCopy\ChildFile2.txt True D7BB9BC327821EBD985C30F09F2F5B50B5418C79 #> This will remove all files and directories from the destination before copying all the files and directories within the source directory to the destination directory, but does not copy empty directories. Notice: - The empty directory "ChildDir1" is not copied to the destination. - The extraneous file "ChildFile3.txt" is removed from the destination. - The use of 'overwrite' would be irrelevant here since all files would be removed prior to the copy starting. --------------------- Example 5 --------------------- Copy the contents to the target with conflicting items while not overwritting or cleaning the target. $source = New-Object System.IO.DirectoryInfo 'C:\Demo' $destination = New-Object System.IO.DirectoryInfo 'C:\DemoCopy' Write-Host "Source Children:" $source.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName | ForEach-Object { Write-Host "`t$_" } Write-Host "Destination Children:" $destination.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories).FullName | ForEach-Object { Write-Host "`t$_" } Write-Host "Copying content..." try { $source.CopyContentTo($destination, $false, $false) } 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() } <# Source Children: C:\Demo\ChildDir1 C:\Demo\ChildDir2 C:\Demo\ChildFile1.txt C:\Demo\ChildFile2.txt C:\Demo\ChildDir2\ChildFile1.txt C:\Demo\ChildDir2\ChildFile2.txt Destination Children: C:\DemoCopy\ChildFile1.txt C:\DemoCopy\ChildFile2.txt C:\DemoCopy\ChildFile3.txt Copying content... Error: System.IO.IOException: The file 'C:\DemoCopy\ChildFile1.txt' already exists. at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite) #> Throws an exception because the destination directory already contains a file with the same name as one of the source files and both 'overwrite' and 'cleanTarget' are set to "false". KEYWORDS IOInfoExtensions, IOInfoExtensions.PowerShell, System.IO.DirectoryInfo, DirectoryInfo, CopyContentTo SEE ALSO about_IOInfoExtensions.PowerShell about_IOInfoExtensions.PowerShell_GetDirectory about_IOInfoExtensions.PowerShell_GetFile about_IOInfoExtensions.PowerShell_DeleteContent about_IOInfoExtensions.PowerShell_CopyContentTo about_IOInfoExtensions.PowerShell_MoveFrom about_IOInfoExtensions.PowerShell_CopyFrom about_IOInfoExtensions.PowerShell_TryDelete |