How handle errors with copy-item
I have a backup script that copies several directories of files to a backup location. Unfortunately, not all of the files in the folder are accessible. What the history is, is that they were archived, and what's left is a filename with a grey x on it. When I try to copy it, I get the following message:
Copy-Item : Access to the path 'E:BackupLocDOzOLD_Under Review for NOT USED_keep for now2006-06N.doc' is denied.
At C:UsersmeDocumentspowershellFilesBackup.ps1:13 char:4
+ Copy-Item -Path $SourcePath -Destination $DestinationPath -Force - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (N.doc:FileInfo) [Copy-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : CopyDirectoryInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand
Yes, it's complaining at the To Location, not the From Location. I have opened up the directory/files so it's not read only, but still get this.
Also, getting through these copy errors takes a really long time. If I could avoid trying to copy these files in the first place, it would be much quicker. There's probably 200 of these files that are archived.
However, I'm copying the folder, not the filenames individually. There are ones that aren't archived in that folder. There isn't a plan to clean them up. I'm trying to identify when an error occurs, but it's only hitting my breakpoint if $error.Exception -ne $null statement after it writes the errors to screen and takes forever failing (see comment).
Any idea how I can either filter out the ones that are archived, or grab them and check them against an array or list so I don't get an error message? I haven't figured out how to find them as they happen since it's copying the entire folder.
I was looking at error checking during copy-item but I don't see how to apply that to my issue.
This is the copy method:
function CopyFileToFolderUNC($SourcePath, $DestinationPath){
if(-Not (Test-Path $SourcePath))
{
$global:ErrorStrings.Add("Exception: No such path, $SourcePath;; ")
write-output "++ Error: An error occured during copy operation. No such path, $SourcePath ++"
}
Copy-Item -Path $SourcePath -Destination $DestinationPath -Force -Recurse -errorVariable errors
foreach($error in $errors)
{
if ($error.Exception -ne $null)
{
$global:ErrorStrings.Add("Exception: $($error.Exception);; ")
write-output "++ Error: An error occured during copy operation. Exception: $($error.Exception) ++" #this breakpoint is hit after giving errors on screen and taking a long time/failing to copy files it can't reach
}
write-output "Error: An error occured during copy operation. Exception: $($error.Exception)"
}
}
powershell error-handling copy-item
add a comment |
I have a backup script that copies several directories of files to a backup location. Unfortunately, not all of the files in the folder are accessible. What the history is, is that they were archived, and what's left is a filename with a grey x on it. When I try to copy it, I get the following message:
Copy-Item : Access to the path 'E:BackupLocDOzOLD_Under Review for NOT USED_keep for now2006-06N.doc' is denied.
At C:UsersmeDocumentspowershellFilesBackup.ps1:13 char:4
+ Copy-Item -Path $SourcePath -Destination $DestinationPath -Force - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (N.doc:FileInfo) [Copy-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : CopyDirectoryInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand
Yes, it's complaining at the To Location, not the From Location. I have opened up the directory/files so it's not read only, but still get this.
Also, getting through these copy errors takes a really long time. If I could avoid trying to copy these files in the first place, it would be much quicker. There's probably 200 of these files that are archived.
However, I'm copying the folder, not the filenames individually. There are ones that aren't archived in that folder. There isn't a plan to clean them up. I'm trying to identify when an error occurs, but it's only hitting my breakpoint if $error.Exception -ne $null statement after it writes the errors to screen and takes forever failing (see comment).
Any idea how I can either filter out the ones that are archived, or grab them and check them against an array or list so I don't get an error message? I haven't figured out how to find them as they happen since it's copying the entire folder.
I was looking at error checking during copy-item but I don't see how to apply that to my issue.
This is the copy method:
function CopyFileToFolderUNC($SourcePath, $DestinationPath){
if(-Not (Test-Path $SourcePath))
{
$global:ErrorStrings.Add("Exception: No such path, $SourcePath;; ")
write-output "++ Error: An error occured during copy operation. No such path, $SourcePath ++"
}
Copy-Item -Path $SourcePath -Destination $DestinationPath -Force -Recurse -errorVariable errors
foreach($error in $errors)
{
if ($error.Exception -ne $null)
{
$global:ErrorStrings.Add("Exception: $($error.Exception);; ")
write-output "++ Error: An error occured during copy operation. Exception: $($error.Exception) ++" #this breakpoint is hit after giving errors on screen and taking a long time/failing to copy files it can't reach
}
write-output "Error: An error occured during copy operation. Exception: $($error.Exception)"
}
}
powershell error-handling copy-item
that makes me think that perhaps the archived file in the source is not a file ... instead, it may be a symlink. i don't know how to tellCopy-Item
to ignore them directly. however, you can run the command, collect the error file names, save them to a file for later use. if you useGet-ChildItem
to build a list to send toCopy-Item
, you can use the saved list of files to filter them out of the items sent to the copy command.
– Lee_Dailey
Dec 27 '18 at 16:10
I'm thinking this might be offline files. Explorer displays these as dimmed items with a gray X overlay
– Theo
Dec 28 '18 at 9:21
add a comment |
I have a backup script that copies several directories of files to a backup location. Unfortunately, not all of the files in the folder are accessible. What the history is, is that they were archived, and what's left is a filename with a grey x on it. When I try to copy it, I get the following message:
Copy-Item : Access to the path 'E:BackupLocDOzOLD_Under Review for NOT USED_keep for now2006-06N.doc' is denied.
At C:UsersmeDocumentspowershellFilesBackup.ps1:13 char:4
+ Copy-Item -Path $SourcePath -Destination $DestinationPath -Force - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (N.doc:FileInfo) [Copy-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : CopyDirectoryInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand
Yes, it's complaining at the To Location, not the From Location. I have opened up the directory/files so it's not read only, but still get this.
Also, getting through these copy errors takes a really long time. If I could avoid trying to copy these files in the first place, it would be much quicker. There's probably 200 of these files that are archived.
However, I'm copying the folder, not the filenames individually. There are ones that aren't archived in that folder. There isn't a plan to clean them up. I'm trying to identify when an error occurs, but it's only hitting my breakpoint if $error.Exception -ne $null statement after it writes the errors to screen and takes forever failing (see comment).
Any idea how I can either filter out the ones that are archived, or grab them and check them against an array or list so I don't get an error message? I haven't figured out how to find them as they happen since it's copying the entire folder.
I was looking at error checking during copy-item but I don't see how to apply that to my issue.
This is the copy method:
function CopyFileToFolderUNC($SourcePath, $DestinationPath){
if(-Not (Test-Path $SourcePath))
{
$global:ErrorStrings.Add("Exception: No such path, $SourcePath;; ")
write-output "++ Error: An error occured during copy operation. No such path, $SourcePath ++"
}
Copy-Item -Path $SourcePath -Destination $DestinationPath -Force -Recurse -errorVariable errors
foreach($error in $errors)
{
if ($error.Exception -ne $null)
{
$global:ErrorStrings.Add("Exception: $($error.Exception);; ")
write-output "++ Error: An error occured during copy operation. Exception: $($error.Exception) ++" #this breakpoint is hit after giving errors on screen and taking a long time/failing to copy files it can't reach
}
write-output "Error: An error occured during copy operation. Exception: $($error.Exception)"
}
}
powershell error-handling copy-item
I have a backup script that copies several directories of files to a backup location. Unfortunately, not all of the files in the folder are accessible. What the history is, is that they were archived, and what's left is a filename with a grey x on it. When I try to copy it, I get the following message:
Copy-Item : Access to the path 'E:BackupLocDOzOLD_Under Review for NOT USED_keep for now2006-06N.doc' is denied.
At C:UsersmeDocumentspowershellFilesBackup.ps1:13 char:4
+ Copy-Item -Path $SourcePath -Destination $DestinationPath -Force - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (N.doc:FileInfo) [Copy-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : CopyDirectoryInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand
Yes, it's complaining at the To Location, not the From Location. I have opened up the directory/files so it's not read only, but still get this.
Also, getting through these copy errors takes a really long time. If I could avoid trying to copy these files in the first place, it would be much quicker. There's probably 200 of these files that are archived.
However, I'm copying the folder, not the filenames individually. There are ones that aren't archived in that folder. There isn't a plan to clean them up. I'm trying to identify when an error occurs, but it's only hitting my breakpoint if $error.Exception -ne $null statement after it writes the errors to screen and takes forever failing (see comment).
Any idea how I can either filter out the ones that are archived, or grab them and check them against an array or list so I don't get an error message? I haven't figured out how to find them as they happen since it's copying the entire folder.
I was looking at error checking during copy-item but I don't see how to apply that to my issue.
This is the copy method:
function CopyFileToFolderUNC($SourcePath, $DestinationPath){
if(-Not (Test-Path $SourcePath))
{
$global:ErrorStrings.Add("Exception: No such path, $SourcePath;; ")
write-output "++ Error: An error occured during copy operation. No such path, $SourcePath ++"
}
Copy-Item -Path $SourcePath -Destination $DestinationPath -Force -Recurse -errorVariable errors
foreach($error in $errors)
{
if ($error.Exception -ne $null)
{
$global:ErrorStrings.Add("Exception: $($error.Exception);; ")
write-output "++ Error: An error occured during copy operation. Exception: $($error.Exception) ++" #this breakpoint is hit after giving errors on screen and taking a long time/failing to copy files it can't reach
}
write-output "Error: An error occured during copy operation. Exception: $($error.Exception)"
}
}
powershell error-handling copy-item
powershell error-handling copy-item
edited Dec 27 '18 at 15:30
asked Dec 27 '18 at 14:51
Michele
1,39952645
1,39952645
that makes me think that perhaps the archived file in the source is not a file ... instead, it may be a symlink. i don't know how to tellCopy-Item
to ignore them directly. however, you can run the command, collect the error file names, save them to a file for later use. if you useGet-ChildItem
to build a list to send toCopy-Item
, you can use the saved list of files to filter them out of the items sent to the copy command.
– Lee_Dailey
Dec 27 '18 at 16:10
I'm thinking this might be offline files. Explorer displays these as dimmed items with a gray X overlay
– Theo
Dec 28 '18 at 9:21
add a comment |
that makes me think that perhaps the archived file in the source is not a file ... instead, it may be a symlink. i don't know how to tellCopy-Item
to ignore them directly. however, you can run the command, collect the error file names, save them to a file for later use. if you useGet-ChildItem
to build a list to send toCopy-Item
, you can use the saved list of files to filter them out of the items sent to the copy command.
– Lee_Dailey
Dec 27 '18 at 16:10
I'm thinking this might be offline files. Explorer displays these as dimmed items with a gray X overlay
– Theo
Dec 28 '18 at 9:21
that makes me think that perhaps the archived file in the source is not a file ... instead, it may be a symlink. i don't know how to tell
Copy-Item
to ignore them directly. however, you can run the command, collect the error file names, save them to a file for later use. if you use Get-ChildItem
to build a list to send to Copy-Item
, you can use the saved list of files to filter them out of the items sent to the copy command.– Lee_Dailey
Dec 27 '18 at 16:10
that makes me think that perhaps the archived file in the source is not a file ... instead, it may be a symlink. i don't know how to tell
Copy-Item
to ignore them directly. however, you can run the command, collect the error file names, save them to a file for later use. if you use Get-ChildItem
to build a list to send to Copy-Item
, you can use the saved list of files to filter them out of the items sent to the copy command.– Lee_Dailey
Dec 27 '18 at 16:10
I'm thinking this might be offline files. Explorer displays these as dimmed items with a gray X overlay
– Theo
Dec 28 '18 at 9:21
I'm thinking this might be offline files. Explorer displays these as dimmed items with a gray X overlay
– Theo
Dec 28 '18 at 9:21
add a comment |
1 Answer
1
active
oldest
votes
To me it looks like the files you describe are Off-line files.
In your function you can test if that is the case using something like this:
function CopyFileToFolderUNC($SourcePath, $DestinationPath){
if(-Not (Test-Path $SourcePath)) {
$global:ErrorStrings.Add("Exception: No such path, $SourcePath;; ")
Write-Output "++ Error: An error occured during copy operation. No such path, $SourcePath ++"
}
# test if maybe we are dealing with an off-line file here
if ((Get-Item -Path $SourcePath).Attributes -band 4096) { # or use the enum name 'Offline'
# or use .Net:
# [System.IO.File]::GetAttributes($SourcePath) -band 4096
Write-Output "Did not copy: File '$SourcePath' is currently off-line. "
}
else {
# the file is not off-line, so proceed with the copy
Copy-Item -Path $SourcePath -Destination $DestinationPath -Force -Recurse -errorVariable errors
foreach($error in $errors) {
if ($error.Exception) {
$global:ErrorStrings.Add("Exception: $($error.Exception);; ")
Write-Output "++ Error: An error occured during copy operation. Exception: $($error.Exception) ++" #this breakpoint is hit after giving errors on screen and taking a long time/failing to copy files it can't reach
}
Write-Output "Error: An error occured during copy operation. Exception: $($error.Exception)"
}
}
}
See FileAttributes Enum for all possible attribute values.
Hope that helps
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53946910%2fhow-handle-errors-with-copy-item%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To me it looks like the files you describe are Off-line files.
In your function you can test if that is the case using something like this:
function CopyFileToFolderUNC($SourcePath, $DestinationPath){
if(-Not (Test-Path $SourcePath)) {
$global:ErrorStrings.Add("Exception: No such path, $SourcePath;; ")
Write-Output "++ Error: An error occured during copy operation. No such path, $SourcePath ++"
}
# test if maybe we are dealing with an off-line file here
if ((Get-Item -Path $SourcePath).Attributes -band 4096) { # or use the enum name 'Offline'
# or use .Net:
# [System.IO.File]::GetAttributes($SourcePath) -band 4096
Write-Output "Did not copy: File '$SourcePath' is currently off-line. "
}
else {
# the file is not off-line, so proceed with the copy
Copy-Item -Path $SourcePath -Destination $DestinationPath -Force -Recurse -errorVariable errors
foreach($error in $errors) {
if ($error.Exception) {
$global:ErrorStrings.Add("Exception: $($error.Exception);; ")
Write-Output "++ Error: An error occured during copy operation. Exception: $($error.Exception) ++" #this breakpoint is hit after giving errors on screen and taking a long time/failing to copy files it can't reach
}
Write-Output "Error: An error occured during copy operation. Exception: $($error.Exception)"
}
}
}
See FileAttributes Enum for all possible attribute values.
Hope that helps
add a comment |
To me it looks like the files you describe are Off-line files.
In your function you can test if that is the case using something like this:
function CopyFileToFolderUNC($SourcePath, $DestinationPath){
if(-Not (Test-Path $SourcePath)) {
$global:ErrorStrings.Add("Exception: No such path, $SourcePath;; ")
Write-Output "++ Error: An error occured during copy operation. No such path, $SourcePath ++"
}
# test if maybe we are dealing with an off-line file here
if ((Get-Item -Path $SourcePath).Attributes -band 4096) { # or use the enum name 'Offline'
# or use .Net:
# [System.IO.File]::GetAttributes($SourcePath) -band 4096
Write-Output "Did not copy: File '$SourcePath' is currently off-line. "
}
else {
# the file is not off-line, so proceed with the copy
Copy-Item -Path $SourcePath -Destination $DestinationPath -Force -Recurse -errorVariable errors
foreach($error in $errors) {
if ($error.Exception) {
$global:ErrorStrings.Add("Exception: $($error.Exception);; ")
Write-Output "++ Error: An error occured during copy operation. Exception: $($error.Exception) ++" #this breakpoint is hit after giving errors on screen and taking a long time/failing to copy files it can't reach
}
Write-Output "Error: An error occured during copy operation. Exception: $($error.Exception)"
}
}
}
See FileAttributes Enum for all possible attribute values.
Hope that helps
add a comment |
To me it looks like the files you describe are Off-line files.
In your function you can test if that is the case using something like this:
function CopyFileToFolderUNC($SourcePath, $DestinationPath){
if(-Not (Test-Path $SourcePath)) {
$global:ErrorStrings.Add("Exception: No such path, $SourcePath;; ")
Write-Output "++ Error: An error occured during copy operation. No such path, $SourcePath ++"
}
# test if maybe we are dealing with an off-line file here
if ((Get-Item -Path $SourcePath).Attributes -band 4096) { # or use the enum name 'Offline'
# or use .Net:
# [System.IO.File]::GetAttributes($SourcePath) -band 4096
Write-Output "Did not copy: File '$SourcePath' is currently off-line. "
}
else {
# the file is not off-line, so proceed with the copy
Copy-Item -Path $SourcePath -Destination $DestinationPath -Force -Recurse -errorVariable errors
foreach($error in $errors) {
if ($error.Exception) {
$global:ErrorStrings.Add("Exception: $($error.Exception);; ")
Write-Output "++ Error: An error occured during copy operation. Exception: $($error.Exception) ++" #this breakpoint is hit after giving errors on screen and taking a long time/failing to copy files it can't reach
}
Write-Output "Error: An error occured during copy operation. Exception: $($error.Exception)"
}
}
}
See FileAttributes Enum for all possible attribute values.
Hope that helps
To me it looks like the files you describe are Off-line files.
In your function you can test if that is the case using something like this:
function CopyFileToFolderUNC($SourcePath, $DestinationPath){
if(-Not (Test-Path $SourcePath)) {
$global:ErrorStrings.Add("Exception: No such path, $SourcePath;; ")
Write-Output "++ Error: An error occured during copy operation. No such path, $SourcePath ++"
}
# test if maybe we are dealing with an off-line file here
if ((Get-Item -Path $SourcePath).Attributes -band 4096) { # or use the enum name 'Offline'
# or use .Net:
# [System.IO.File]::GetAttributes($SourcePath) -band 4096
Write-Output "Did not copy: File '$SourcePath' is currently off-line. "
}
else {
# the file is not off-line, so proceed with the copy
Copy-Item -Path $SourcePath -Destination $DestinationPath -Force -Recurse -errorVariable errors
foreach($error in $errors) {
if ($error.Exception) {
$global:ErrorStrings.Add("Exception: $($error.Exception);; ")
Write-Output "++ Error: An error occured during copy operation. Exception: $($error.Exception) ++" #this breakpoint is hit after giving errors on screen and taking a long time/failing to copy files it can't reach
}
Write-Output "Error: An error occured during copy operation. Exception: $($error.Exception)"
}
}
}
See FileAttributes Enum for all possible attribute values.
Hope that helps
answered Dec 28 '18 at 9:46
Theo
3,8251520
3,8251520
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53946910%2fhow-handle-errors-with-copy-item%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
that makes me think that perhaps the archived file in the source is not a file ... instead, it may be a symlink. i don't know how to tell
Copy-Item
to ignore them directly. however, you can run the command, collect the error file names, save them to a file for later use. if you useGet-ChildItem
to build a list to send toCopy-Item
, you can use the saved list of files to filter them out of the items sent to the copy command.– Lee_Dailey
Dec 27 '18 at 16:10
I'm thinking this might be offline files. Explorer displays these as dimmed items with a gray X overlay
– Theo
Dec 28 '18 at 9:21