Copy-Item:找不到路径DirectoryNotFoundException Powershell的一部分

时间:2015-12-03 22:11:51

标签: powershell

我正在尝试将文件夹和文件从一个位置复制到同一服务器上的另一个位置。我找到了一些可以完成这项工作的脚本,但前提是我运行了两次。我第一次运行脚本时收到错误:

$From = "C:\b\125\Build\396\src\Integration"
$to = "C:\Build\396\396_20151203.5\Integration\"

$exclude = @("*.cs", "*.csproj*")
$excludeMatch = @("Configurations")

#Copy-Item $source $to -Recurse

    Get-ChildItem -Path $From -Recurse -Exclude $exclude |
        Where-Object {
            $excludeMatch -eq $null -or `
            $_.FullName.Replace($From, "") -notmatch $excludeMatch
        } | Copy-Item -Destination {
            if($_.PSIsContainer) {
                Join-Path $to $_.Parent.FullName.Substring($From.Length)
            } else {
                Join-Path $to $_.FullName.Substring($From.Length)
            }
        } -Force -Exclude $exclude

当我第二次运行代码时,会复制所有后续文件夹和文件。

以下是代码:

struct Range
{
    public int Start, End;
    public int Length { get { return End - Start; } }
    public Range(int start, int length) { Start = start; End = start + length; }
}

static IEnumerable<Range[]> GetPalindromeBlocks(string input)
{
    int maxLength = input.Length / 2;
    var ranges = new Range[maxLength];
    int count = 0;
    for (var range = new Range(0, 1); ; range.End++)
    {
        if (range.End <= maxLength)
        {
            if (!IsPalindromeBlock(input, range)) continue;
            ranges[count++] = range;
            range.Start = range.End;
        }
        else
        {
            if (count == 0) break;
            yield return GenerateResult(input, ranges, count);
            range = ranges[--count];
        }
    }
}

static bool IsPalindromeBlock(string input, Range range)
{
    return string.Compare(input, range.Start, input, input.Length - range.End, range.Length) == 0;
}

static Range[] GenerateResult(string input, Range[] ranges, int count)
{
    var last = ranges[count - 1];
    int midLength = input.Length - 2 * last.End;
    var result = new Range[2 * count + (midLength > 0 ? 1 : 0)];
    for (int i = 0; i < count; i++)
    {
        var range = result[i] = ranges[i];
        result[result.Length - 1 - i] = new Range(input.Length - range.End, range.Length);
    }
    if (midLength > 0)
        result[count] = new Range(last.End, midLength);
    return result;
}

1 个答案:

答案 0 :(得分:0)

这似乎有点过于复杂。

为什么不尝试这个?

$from = "C:\b\125\Build\396\src\Integration"
$to = "C:\Build\396\396_20151203.5\Integration\"

$exclude = @("*.cs", "*.csproj*", "Configurations")

Get-ChildItem $from -Exclude $exclude -Recurse -Force |
    Copy-Item -Destination {
        Join-Path $to $_.FullName.Substring($from.Length)
    }