递归复制文件期间的powershell错误检查 [英] powershell error checking during file copy with recursion

查看:106
本文介绍了递归复制文件期间的powershell错误检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以递归复制文件夹和文件。
示例:

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?

推荐答案

可以。例如,以下代码片段实际上将复制并检查每个文件是否存在错误。您还可以将自定义代码放在开头,以检查某些先决条件:

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."
    }
}

这篇关于递归复制文件期间的powershell错误检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆