powershell error checking during file copy with recursion

梦想与她 提交于 2019-12-11 02:25:09

问题


I have a program that copies folders and files recursively. example:

Copy-Item -path "$folderA" -destination "$folderB" -recurse 

Sometimes the files do not copy. Is there a way to "step inside the recursion" or a better way to do it, so I can enable some kind of error checking during the process rather than after wards. Possibly even do a Test-Path and prompt for a recopy?


回答1:


You can. For example the following code snippet will actually copy and check each file for possible errors. You can also put your custom code at the beginning to check for some prerequisites:

get-childItem $source -filter *.* | foreach-object {
    # here you can put your pre-copy tests...

    copy-item $_.FullName -destination $target -errorAction SilentlyContinue -errorVariable errors
    foreach($error in $errors)
    {
        if ($error.Exception -ne $null)
        {
            write-host -foregroundColor Red "Exception: $($error.Exception)"
        }
        write-host -foregroundColor Red "Error: An error occured during copy operation."
    }
}


来源:https://stackoverflow.com/questions/3809199/powershell-error-checking-during-file-copy-with-recursion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!